nginx配置根據url的參數值進行轉發


    server {
        listen          8081;

        location / {
            set $tag "";
            set $cs "/index/test/test";
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Host $http_host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;

            client_max_body_size 5m;

       //獲取code的值,賦值給tag if ($query_string ~ "code=(\d+)"){ set $tag "$1"; }
       //判斷tag的值 # if ($tag ~ "200")  

       if ($tag ~ "200"){
         proxy_pass http://xxx:1234$cs;
       }
 proxy_pass http://xxxx; } } 

  訪問的話類似:http://xxx:1234/?code=200&name=test,判斷code的值是200的話,轉發到http://xxx:1234$cs地址,proxy_pass后面只能跟ip+port,所以定義了一個cs變量放置模塊,controller和方法的字符串.

  還有個bug, $tag ~ "200"這樣寫的話,code傳入2000000照樣轉發,所以應該在后面加一個$.

  暫時只會這么多了...謝謝


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM