Nginx rewrite post请求变get请求解决


       笔者最近碰到一个nginx问题,我们需要将http://t16.niiu.com/gateway/abtest-service/api/shenzhen/shoulei/7(该方法是POST) rewrite成http://t16.niiu.com//api/shenzhen/shoulei/7 , 因为微服务提供的请求地址就是:/api/shenzhen/shoulei/7 。

其中,/api后面的shenzhen、shoulei都是可变参数,有可能会变成/api/beijing/pc/18这种url请求。通过如下配置解决问题:

upstream ab {
        server 10.1.1.16:7000 max_fails=3 fail_timeout=3s weight=300;
        keepalive 20;
}


location ~ ^/gateway/abtest-service/api/ {
             rewrite '^/gateway/abtest-service/api/([a-z]{2,})/([a-z]{2,})/(\d{1,})' /api/$1/$2/$3  break; 
             proxy_pass http://ab;
        }        


        location ~ ^/api/ {
            proxy_pass  http://ab;
            proxy_connect_timeout    600;
            proxy_read_timeout       600;
            proxy_send_timeout       1200;
            proxy_set_header Host $remote_addr;
            proxy_http_version 1.1;
            proxy_set_header Connection "keep-alive";
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_ignore_client_abort on;
        }

你可能感兴趣的:(Nginx rewrite post请求变get请求解决)