nginx根據訪問的url參數或者是請求 頭部作判斷轉發

location /ws/ {
        add_header 'Access-Control-Allow-Origin' '*'; 
        add_header 'Access-Control-Allow-Credentials' 'true'; 
        add_header 'Access-Control-Allow-Headers' 'Content-Type, x-csrf-token, X-Access-Token, Accept';
        add_header 'Access-Control-Allow-Methods' 'GET,HEAD,PUT,POST,DELETE,PATCH';
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Nginx-Proxy true;
        if ($arg_token = '') {
         #return 401;
         set $token  para; 
        }
        if ($http_token = '') {
            #return 401;
             set $token  "${token}header"; 
        }
         if ($token = paraheader){
         return 401;
         }
         
        proxy_pass http://127.0.0.1:8080/app/;
        proxy_redirect off;
        }html

簡要說明if ($arg_token = '')表參數中沒有名爲token的參數,  if ($http_token = '')表示沒有token這個請求頭。nginx

特別注意if is evil,nginx不提供多if判斷,沒有or這樣的判斷,因此只能用上文這種方式web

 

對於須要指定代理到那一臺服務器的場景能夠採用這種模式,例如webSocket長鏈接,對應分佈式多個區域的服務器,指定的用戶只能鏈接到對應的服務器,能夠採用這種方式,nginx支持正則匹配服務器

https://www.cnblogs.com/mafeng/p/10922707.htmlapp

https://blog.csdn.net/u012687769/article/details/78606202/分佈式

nginx location正則寫法

http://www.javashuo.com/article/p-qusigjmv-gg.htmlspa

相關文章
相關標籤/搜索