删除 Nginx proxy_pass 中的查询字符串

mys*_*ers 7 nginx

是否可以在 Nginx 中使用 proxy_pass 删除查询字符串?例如,我将我的 nginx 称为:

http://nginxproxy.com/api/v1/logout?session=123
Run Code Online (Sandbox Code Playgroud)

并希望将其代理到:

http://example.com/api/sessions/?_action=logout
Run Code Online (Sandbox Code Playgroud)

没有查询字符串“session=123”。

目前我的设置只是添加我传递给 proxy_pass URL 的任何查询字符串。

location /api/v2/logout {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Session $arg_token;
        proxy_pass http://example.com/api/sessions/?_action=logout;
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*han 6

如果您要删除 上指定的任何查询字符串/api/v2/logout,添加set $args "";应该可以:

location /api/v2/logout {
    set $args "";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Session $arg_token;
    proxy_pass http://example.com/api/sessions/?_action=logout;
}
Run Code Online (Sandbox Code Playgroud)


Thi*_*rry 1

我相信你可以使用如下重写规则来做到这一点:

rewrite ^(.*)$ $1?;
Run Code Online (Sandbox Code Playgroud)