Delrog
Delrog

Reputation: 765

Nginx accept trailing slash or no trailing slash in proxy_pass

I'm running into the following issue where I would like to be able to access a proxy passed location (React/NextJs webApp in a hosted docker container) from a home website with a trailing slash and without a trailing slash.

Currently, when I hit:

http://my-website.com/test # this works

But when I hit:

http://my-website.com/test/ # this fails with a 404

I'd like to be able to hit both of these urls. What am I missing?

   ### 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;
        }
    }

Upvotes: 5

Views: 3472

Answers (1)

num8er
num8er

Reputation: 19372

After long experiments we came to this solution:

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

needed to pass everything after /test to app, with or without trailing slash it should be handled correctly

Upvotes: 7

Related Questions