Score:0

Nginx reverse proxy returning internal address to client on page refresh

cl flag

I have a reactjs app being served by a container running NGINX with following configuration:

server {
listen       ${NGINX_PORT};
access_log  /var/log/nginx/access.log  main;
location / {
    root   /usr/share/nginx/html;
    try_files $uri $uri/ /index.html?$args;        
}
}

Then I put a reverse proxy in front of it with the following configuration:

upstream internal {
    server X.X.X.X:3100;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name mysite.com;
    port_in_redirect off;

    ssl_certificate /opt/nginx/certs/cert.cer;
    ssl_certificate_key /opt/nginx/certs/cert.key;

    location / {
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://internal;
            proxy_http_version 1.1;
            proxy_redirect off;
    }
  }

It works fine for navigation, I can jump to one page to another without any issue. However, when I hit refresh, it redirects the browser to the internal address.

http://internal:3100/docs/intro/

EDIT: The root address, or mysite.com can be refreshed with no issues. The refresh issue occurs only when inside some topic tree like example above.

How can I prevent this ?

Jaromanda X avatar
ru flag
is `http://internal:3100/docs/intro/` the exact address you're redirected to, or is it the actual `X.X.X.X:3100` address (presumably X.X.X.X is not a public LAN address so doesn't need to be obfuscated since it's only internally accessible)
ws flag
The fault is in your origin server - the nginx config is fine. The origin server must be hard-wired to use its local name. Learn how to read your HTTP headers using web developer or similar.
GIJOW avatar
cl flag
@JaromandaX ``http://internal:3100/docs/intro/`` this is the exact address I get redirected. My bacend (origin server) don't make any reference to this address at all. It exists only on my nginx config
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.