【问题标题】:Nginx accept trailing slash or no trailing slash in proxy_passNginx 在 proxy_pass 中接受斜杠或没有斜杠
【发布时间】:2020-08-03 15:47:14
【问题描述】:

我遇到了以下问题,我希望能够从带有斜杠的主网站访问代理传递的位置(托管 docker 容器中的 React/NextJs webApp)和 没有尾部斜杠。

目前,当我点击:

http://my-website.com/test # 这行得通

但是当我击中时:

http://my-website.com/test/ # 失败并返回 404

我希望能够同时访问这两个网址。我错过了什么?

   ### Default Server ###
    server {
        listen 80;
        root /usr/site;
        if ($http_x_forwarded_proto = "http") {
            return 301 https://$host$request_uri;
        }
        location ~/test(.*)$ {
                set $upstream_endpoint http://$docker_container_url;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_pass $upstream_endpoint$1/;
                proxy_set_header    Host    $host;
                proxy_cache_bypass  $http_upgrade;
        }
    }

【问题讨论】:

  • 试试这个:location /test { 定义严格的路由没有 - tilda(正则表达式)
  • 我也无法理解您想要实现的目标。您正在重定向到 https 版本,但在 /test 您正在请求上游的 http 版本,它将再次重定向(因为 http => https)
  • my-website.com 的配置在哪里?
  • my-website.com 是一个用 React 编写的 NextJS 配置应用程序。该应用使用“/test”的 AssetPrefix。
  • 这个 nginx 配置是我家网站上的反向代理设置,我正在尝试连接到作为 upstream_endpoint 传入的 docker 容器。我试图让场景更简单一些,因为我相信它与斜杠问题无关。

标签: nginx nginx-reverse-proxy nginx-config


【解决方案1】:

经过长时间的实验,我们得出了这个解决方案:

  location ~ ^/test(?:/(.*))?$ {
    # some directives here
    proxy_pass http://nginx_docker_container_url/$1;
    # some directives here
  }

需要将/test 之后的所有内容传递给应用程序,无论是否带有斜杠,都应该正确处理

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    相关资源
    最近更新 更多