Nginx proxy_pass所有url参数

时间:2016-07-26 16:02:51

标签: nginx

我想代理这样的请求: http://myproxy.com/api/folder1/result1?test=1
http://myproxy.com/api/folder3447/something?var=one

到等效目的地:http://destination.com/folder1/result1?test=1http://destination.com/folder3447/something?var=one,几乎只有域名更改,所有子文件夹和参数都被保留

配置中的

位置如下:

location ~* ^/api/(.*) {
    proxy_pass http://destination.com/$1$is_args$args;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    #proxy_set_header  Host $http_host;
  }

1 个答案:

答案 0 :(得分:4)

您应该可以稍微简化配置:

location /api/ {
    // Note the slash at end, which with the above location block
    // will replace "/api/" with "/". This will not work with regex
    // locations
    proxy_pass http://destination.com/;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
}