代理传递 url 中的 NginX 尾部斜杠

编程入门 行业动态 更新时间:2024-10-26 20:31:01
本文介绍了代理传递 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 example 代理将请求传递给 example:8080/?

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

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

Requests (from a mobile app) are requesting example// which nginx is making example: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 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 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 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 backend/;

更新

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

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

location = /a { proxy_pass 127.0.0.1:8080/a; ... } location ^~ /a/ { proxy_pass 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 127.0.0.1:8080/two; ... }

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

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

更多推荐

代理传递 url 中的 NginX 尾部斜杠

本文发布于:2023-11-01 11:22:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   尾部   url   NginX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!