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照样转发,所以应该在后面加一个$.

  暂时只会这么多了...谢谢

posted on 2020-07-01 15:19  EchoBao  阅读(11065)  评论(0编辑  收藏  举报