nginx根据url参数做转发

个人学习笔记,谢绝转载!!!
原文:https://www.cnblogs.com/wshenjin/p/13071392.html


线上一个需求,需要根据url参数的特定值做固定的转发处理。

例如 http://api.example.com/?boole=1234&ment=abcd&plat=mix_a&dupl=1234
plat=mix_[a|b|c|d]时,转发到http://api-bgp.example.com

    location / {
         proxy_redirect off;
         proxy_set_header   Host             api-bgp.example.com;
         proxy_set_header   X-Real-IP        $remote_addr;
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
         if ( $query_string ~  "plat=mix_(a|b|c|d)" ){
             proxy_pass $scheme://1.1.1.1:$server_port;
         }
         index index.php index.html index.htm;
    }

另外一个需求,根据url参数的内容重写
http://api.example.com/ooo/xxx/index.php?version=1s1234&boole=1234&ment=abcd ---> http://api.example.com/ooo/xxx/rule.php?version=1s1234&boole=1234&ment=abcd
version=1sxxxx是1s后面跟上时间戳,例如:version=1s1598521028

    location = /ooo/xxx/index.php {
        if ( $query_string ~ "version=1s(d+)" ) {
            rewrite /ooo/xxx/index.php /ooo/xxx/rule.php  last;
        }
        include fcgi.conf;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        expires off;
    }
原文地址:https://www.cnblogs.com/wshenjin/p/13071392.html