代理传递 url 中的 NginX 尾部斜杠 [英] NginX trailing slash in proxy pass url

查看:66
本文介绍了代理传递 url 中的 NginX 尾部斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过很多次了,但在尝试了很多解决方案后,我仍然被卡住了.

I know that this question has been asked multiple times but after a trying a lot of solutions I am still stuck.

我使用 NginX 代理传递给 NodeJs 应用程序.我正在尝试让 url https://example.com 代理将请求传递给 http://example.com:8080/?

I am using NginX to proxy pass to a NodeJs application. I'm trying to make url https://example.com proxy pass the request to http://example.com:8080/?

请求(来自移动应用程序)正在请求 https://example.com// 哪个 nginx 正在制作 http://example.com:8080//? 这不起作用.

Requests (from a mobile app) are requesting https://example.com// which nginx is making http://example.com:8080//? which is not working.

我尝试过但没有奏效的事情:

Things I have tried which haven't worked:

  • merge_slashes on;(默认开启)
  • rewrite ^/(.*)/$/$1 永久;
  • port_in_redirect 关闭;
  • merge_slashes on; (which is on by default)
  • rewrite ^/(.*)/$ /$1 permanent;
  • port_in_redirect off;

我的 nginx 配置:

My nginx config:

location = /a {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_read_timeout 300;
}

location ^~ /a/ {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_read_timeout 300;
}

推荐答案

我猜你使用了没有 URI 的 proxy_pass.在这种情况下,nginx 会按原样传递原始 URI,而不是标准化的 URI.所以你应该使用 proxy_pass http://backend/;

I guess you use proxy_pass without URI. In that case nginx pass original URI as it was, not normalized one. So you should use proxy_pass http://backend/;

更新

所以我是对的.只需像这样将 URI 部分添加到 proxy_pass:

So I was right. Just add URI part to proxy_pass like this:

location = /a {
    proxy_pass http://127.0.0.1:8080/a;
    ...
}

location ^~ /a/ {
    proxy_pass http://127.0.0.1:8080/a/;
    ...
}

nginx 文档 中所述,如果 proxy_pass 在没有 URI 的情况下使用(即没有server:port 之后的路径)nginx 会将原始请求中的 URI 与所有双斜杠、../ 等完全一样.

As stated in nginx documentation if proxy_pass used without URI (i.e. without path after server:port) nginx will put URI from original request exactly as it was with all double slashes, ../ and so on.

另一方面,proxy_pass 中的 URI 的作用类似于 alias> 指令,意味着 nginx 将替换匹配位置前缀的部分(在第一个位置是 /a 并且 /a/ 是第二个)中的 URIproxy_pass 指令(我有意将其与位置前缀相同),因此 URI 将与请求的相同,但已标准化(没有双斜线和所有人员).小心尾部斜线.Nginx 从字面上替换部分,你最终可能会得到一些奇怪的 url.

On the other side, URI in proxy_pass acts like alias directive, means nginx will replace part that matches location prefix (in out case it's /a in first location and /a/ is second) with URI in proxy_pass directive (which I intentionally made the same as location prefix) so URI will be the same as requested but normalized (without doule slashes and all that staff). Be careful with trailing slashes. Nginx replaces part literally and you could end up with some strange url.

这是一个示例,location 中有斜杠,但 proxy_pass 中没有斜杠.

Here is an example with trailing slash in location, but no trailig slash in proxy_pass.

location /one/ {
    proxy_pass http://127.0.0.1:8080/two;
    ...
}

如果访问地址 http://yourserver.com/one/path/here?param=1 nginx 会将请求代理到 http://127.0.0.1/twopath/这里?param=1.看看 twopath 如何连接.

if one go to address http://yourserver.com/one/path/here?param=1 nginx will proxy request to http://127.0.0.1/twopath/here?param=1. See how two and path concatenates.

这篇关于代理传递 url 中的 NginX 尾部斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆