4

I have docker stack running 2 containers, first is Nginx, second - application.

The problem is that nginx shows Bad Gateway error:

Here is nginx conf:

upstream example {
  server mystack_app1;  
  # Also tried with just 'app1'
  # server mystack_app2;

  keepalive 32;
}

server {
    listen 80;
    server_name example;

    location / {
                proxy_pass http://example;
                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_connect_timeout 150;
                proxy_send_timeout 100;
                proxy_read_timeout 100;
                proxy_buffers 4 32k;
                client_max_body_size 8m;
                client_body_buffer_size 128k;
        }
}

Here is docker-compose.yml

version: "3"
services: 

  app1:
    image: my-app:latest    
    ports:
      - "9000:9000"
    networks:
      - webnet  

  web:
    image: my-web:latest    
    ports:
      - "81:80"
    networks:
      - webnet
    deploy:      
      restart_policy:
        condition: on-failure        

networks:
  webnet:

I use following command to deploy docker stack:

docker stack deploy -c docker-compose.yml mystack

So I can access application from host's browser by localhost:9000 - it works ok.

Also, from the nginx container, I can ping mystack_app1.

But when accessing localhost:81, nginx shows 502 Bad Gateway

Please help.

1 Answer 1

3

It looks like your upstream definition is not correct. It's trying to connect to port 80 instead of port 9000.

Try

upstream example {
  server mystack_app1:9000;  
  # Also tried with just 'app1'
  # server mystack_app2;

  keepalive 32;
}

Btw, I suggest you to use the container_name in your docker-compose file.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried with port before, didn't work, but probably because of other incorrect config. Now works, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.