Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

253
Vistas
Docker nginx can't connect to upstream gunicorn running in separate container

I have tried various options e.g. expose, bridge, networks options of docker-compose but can't get it to work with the nginx connection to upstream gunicorn running in separate container, I am receiving 502 Bad Gateway error from nginx. I am not sure as to what i am missing exactly. Below is my docker-compose file:

version: "3"

services:
  web:
    build: .
    container_name: web
    command: bash -c "/start_web.sh"
    restart: always
    depends_on:
      - worker
    ports:
      - "80:80"
      - "443:443"

  worker:
    build: .
    container_name: worker
    command: bash -c "/start_worker.sh"
    restart: always
    ports:
      - "8000:8000"

nginx conf:

upstream worker {
    server 127.0.0.1:8000;
}

server {
    listen 80 default_server;

    location / {
        proxy_http_version 1.1;
        proxy_buffering 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-Proto $scheme;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_redirect off;
        # Mitigate httpoxy attack
        proxy_set_header Proxy "";

        proxy_pass http://worker;
    }
}

Gunicorn config:

import multiprocessing
import os

bind = '127.0.0.1:8000'

default_workers = multiprocessing.cpu_count() * 2 + 1
workers = os.getenv('GUNICORN_WORKERS', os.getenv('WEB_CONCURRENCY', default_workers))
worker_class = 'tornado'

# This is to fix issues with compressor package: broken offline manifest for
# custom domain. It randomly breaks, I think because of global variable inside.
preload_app = True

timeout = 200
graceful_timeout = 60
max_requests = 250
max_requests_jitter = max_requests
accesslog = '/tmp/gunicorn_access.log'
errorlog = '/tmp/gunicorn_error.log'

Circus ini files:

web.ini

[watcher:nginx]
cmd = /usr/sbin/nginx
stop_signal = QUIT

worker.ini

[watcher:gunicorn]
cmd = /usr/local/bin/gunicorn test:app -c /etc/gunicorn/app.py
working_dir = /opt/app
copy_env = True
uid = www-data

The whole code is available on github as well at repository docker_test for your ease to test it.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Gunicon config:

bind = '127.0.0.1:8000'

This will bind to loopback interface (localhost only), change it to 0.0.0.0 to bind to every available interface in the container. This will make it reachable from nginx.

Nginx config:

upstream worker {
    server 127.0.0.1:8000;
}

You need to change loopback ip to DNSname/IP of worker container. I recommend creating an user-defined network, Then put all containers that are related in that network and call them by DNS names. You wont have internal DNS in default bridge network so following nginx config wont work.

upstream worker {
    server worker:8000;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda