Nginx 接受 proxy_pass 中的尾部斜杠或不接受尾部斜杠

Del*_*rog 5 nginx nginx-reverse-proxy nginx-config

我遇到了以下问题,我希望能够从带有尾部斜杠不带尾部斜杠的主网站访问代理传递的位置(托管 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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

num*_*8er 6

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

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

需要将所有内容传递/test给应用程序,无论有或没有尾随斜杠,都应该正确处理