Relative link not working with nginx reverse proxy Relative link not working with nginx reverse proxy nginx nginx

Relative link not working with nginx reverse proxy


Imaging like that:

1/ If my link is: http://localhost:8080/us/about-us

2/ It's going to your proxy and meet first condition:

location / {    proxy_pass http://nextjs_upstream/us/;}

3/ After that, your server will add us path to your link and that the reason why it lead to: http://localhost:3000/us/us/about-us/

My suggestion is add if clause for remove /us in your reverse proxy config file:

if ($request_uri ~ ^(.*)/us/) {    rewrite (.*)/us/(.*)$ $1/$2;}

Your config file look like this:

events {}http {    upstream nextjs_upstream {        server localhost:3000;    }    server {        listen 8080 default_server;        server_name _;        server_tokens off;                if ($request_uri ~ ^(.*)/us/){            rewrite (.*)/us/(.*)$ $1/$2;        }        location / {            proxy_pass http://nextjs_upstream/us/;        }        location /_next/static {            proxy_pass http://nextjs_upstream/_next/static;        }    }}