Score:0

Nginx reverse proxy automatic redirects go to wrong port

us flag

I am trying to set up a reverse proxy to a docker container using nginx.

The docker container is available and working on port 8000.

I would like to be able to reach the container through the address mydomain.com:80/mycontainer.

The reverse proxy seems to succeed in talking to the container but when the container requests a redirect to its login page nginx tries to load the login page on port 80 instead of 8000 which fails.

this is my nginx config so far:

    upstream docker-container {
        server 127.0.0.1:8000;
    }

    server {
        listen 80;

        location /mycontainer {
            proxy_redirect     off;
            proxy_set_header   Host $http_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;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection "upgrade";
            proxy_pass         http://docker-container;

        }

    }

This is what I get when I run wget -S 127.0.0.1/mycontainer:

wget -S 127.0.0.1/mycontainer
--2021-08-29 20:30:12--  http://127.0.0.1/mycontainer
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response...
  HTTP/1.1 302 Found
  Server: nginx/1.18.0 (Ubuntu)
  Date: Sun, 29 Aug 2021 20:30:12 GMT
  Content-Type: text/plain; charset=utf-8
  Content-Length: 28
  Connection: keep-alive
  Content-Language: en
  X-Frame-Options: SAMEORIGIN
  X-Download-Options: noopen
  X-Content-Type-Options: nosniff
  Referrer-Policy: origin-when-cross-origin
  X-XSS-Protection: 1; mode=block
  Location: /login
  Vary: Accept
  Set-Cookie: some cookie
Location: /login [following]
--2021-08-29 20:30:12--  http://127.0.0.1/login   <--- HERE IS THE PROBLEM. Should be 127.0.0.1:8000/login
Reusing existing connection to 127.0.0.1:80.
HTTP request sent, awaiting response...
  HTTP/1.1 404 Not Found
  Server: nginx/1.18.0 (Ubuntu)
  Date: Sun, 29 Aug 2021 20:30:12 GMT
  Content-Type: text/html
  Content-Length: 162
  Connection: keep-alive
2021-08-29 20:30:12 ERROR 404: Not Found.

I am by no means a professionnal at this so I am sure there are a lot of mistakes.

How can I get the automatic redirects to point to the correct port (8000) and not to port 80?

Michael Hampton avatar
cz flag
This redirect comes from your application, not from nginx.
us flag
You need to set your application root URL in the application's settings.
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.