Nginx accept trailing slash or no trailing slash in proxy_pass
Asked Answered
P

1

5

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;
        }
    }
Petulance answered 21/4, 2020 at 0:56 Comment(13)
try this one: location /test { define strict routing without - tilda (regular expression)Libelant
also I cannot understand what You're trying to achieve. You're redirecting to https version, but in /test You're requesting http version of upstream which will do redirect again (since http => https)Libelant
also where is my-website.com configuration?Libelant
my-website.com is a NextJS configured app written in React. The app uses an AssetPrefix of '/test'.Petulance
This nginx config is a reverse proxy setup on my home website and I'm trying to connect to docker container that I pass in as the upstream_endpoint. I was trying to make the scenario a little simpler because I believe it'd be irrelevant for the trailing slash problem.Petulance
ok, so Your app runs inside docker and which port it exposes to host machine?Libelant
Port 8080 which I include in the $docker_container_url variable in the $upstream_endpoint. I tried your first suggestion but I got a 502.Petulance
If I set write 'proxy_pass $upstream_endpoint$1/;' as proxy_pass $upstream_endpoint$1; then the trailing slash works but the non-trailing slash fails.Petulance
try my answer, make sure Your app is runningLibelant
No need to worry about trailing slashes, do one-to-one route passing as in my answer and let next.js worry about trailing or non-trailing slashes. the tilda and regular expression has low priority in nginx, if You want to say "starting with /test" so no need for regular expression. also upstream is special structure which keeps group of one and many apps under one name (for load balancing).Libelant
I tried by simply changing to /test. However left proxy_pass $upstream_endpoint$1 and it fails for .../test/. I'll try with explicit values. What does $1and $request_uri denote?Petulance
$request_uri is full uri without hostname part /test/something/blabla, $1 is first element of regular expression match. I removed $1 because of location /test which does not have any regular expression to match. have You checked my answer below? so please check it (i've checked locally and it works)Libelant
I tried without the $1 and without the regular expression in the location and I receive a 404 for all assets and the test, test/ pagesPetulance
L
7

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

Libelant answered 21/4, 2020 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.