1

I have a simple web application running in a docker container and exposed on port 81.
curl http://127.0.0.1:81 returns the index.html of that web app. So it is running and is reachable. Also localhost or using the browser it works.

Now I added a container with Nginx.
I want to redirect the calls to port 80 to the web app. This is my simplest /etc/nginx/nginx.conf file:

events {}
http {
    server {
        listen 80;

        location / {
            proxy_pass http://127.0.0.1:81;            
            #proxy_pass http://google.com;
        }
    }
}

Switching the proxy_pass to the one pointing to Google it works, so I assume the error is because nginx is not able to "communicate" with the web app (also that is what I read about nginx and 502 error).

I access to the nginx instance with docker -it <container-name> /bin/bash but then cat /var/log/nginx/error.log or cat /var/log/nginx/access.log or tail doesn't work (kind of enter in edit mode but there is no content).

After this simple step is done I'd like to use upstream to ahave a load balancer over 2 web app instances, so something like this:

events {}

http {
    upstream api {
        server 127.0.0.1:81;
    }

    server {
        listen 80 default;
        #listen [::]:80;
        #access_log /dev/stdout;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
            #proxy_pass http://127.0.0.1:81;            
            #proxy_pass http://google.com;
            proxy_pass http://api;                       
            
            proxy_redirect     off;
        }
    }
}

Again, this does not work.

Alex 75
  • 133
  • 7
  • 1
    At first glance your problem appears that for an application running inside a container the localhost/127.0.0.1 address effectively means *"this container"* and not *"this server"*. For your nginx container to connect to another container (running on the same server) you need to connect to either the public/external IP-address where the port is exposed of you set up a container network where the nginx can connect directly to the web application. – Rob Jul 25 '22 at 12:57

0 Answers0