Nginx 不区分大小写的 proxy_pass

Jon*_*uca 6 nginx nginx-location nginx-reverse-proxy

我有一个名为 的站点http://example.com,其中运行着一个可以在http://example.com/app1. app1 位于 nginx 反向代理后面,如下所示:

location /app1/ {
    proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
Run Code Online (Sandbox Code Playgroud)

将尾部斜杠添加到该proxy_pass字段可以让我“删除” URL 的 /app1/ 部分,至少就应用程序而言。所以 app1 认为它正在获取对根 url 的请求(例如,我在 app1 中有一个路由位于'/',而不是'/app1')。

但是,我想让 nginx 不区分大小写。所以无论我去http://example.com/App1还是http://example.com/APP1,它仍然应该将请求转发到app1,删除url的/app1/部分。

当我尝试使用 nginx 不区分大小写的规则时,它不允许将 URI 的其余部分转发到 app1。

location ~* /app1/ {
    proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个 nginx 配置错误。

我的目标有两个:

  • 匹配/app1/的情况下不区分大小写
  • /app1/网址“传递”给应用程序时删除网址的一部分

我试过重写 url,但它不会让我将 URI 的其余部分添加到 proxy_pass。

任何帮助,将不胜感激!

Tar*_*ani 9

您应该捕获 url 的其余部分,然后使用它

location ~* /app1/(.*) {
    proxy_pass http://localhost:8080/$1$is_args$args;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
Run Code Online (Sandbox Code Playgroud)

  • 实际上这是我的问题,nginx 错误显示“*502 没有定义解析器来解析本地主机,客户端:”。我将 localhost 更改为 127.0.0.1 并且它有效。谢谢! (2认同)