'Nginx not redirecting on named route

I'm trying to setup a reverse proxy to a sentry relay using Nginx. Config file as follows:

events {
  worker_connections 768;
}

http {
    server {
        listen 0.0.0.0:80;

        location /sentry-relay {
            proxy_pass         http://127.0.0.1:3001;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}

Browsing directly to the relay server on port 3001 works fine:

enter image description here

However using the location path set in Nginx fails:

enter image description here

I've also put the redirect onto the default path: location / and it works fine. Why won't this custom path redirect properly?



Solution 1:[1]

I worked it out.

Nginx will append the location prefix to the proxy server request unless this prefix is replaced.

Thus to fix I changed:

proxy_pass http://127.0.0.1:3001;

to

proxy_pass http://127.0.0.1:3001/;

The extra slash is used to replace the sentry-relay path.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 desiguel