Custom per-VIRTUAL_HOST location config without effect: 502 Bad Gateway
I try to setup a dockerized nginx reverse proxy server, which should supply a dockerized Gunicorn webserver with. The Gunicorn webserver shall be accessible through example.com/django
so I added custom nginx directives, as described here: https://github.com/nginx-proxy/nginx-proxy/blob/main/README.md#per-virtual_host-location-configuration
I placed the directives in a example.com_django
/www.example.com_django
file. However, they are not adopted in the main nginx config file and don't seem to have any effect. The website is not accessible and results in a 502 Bad Gateway
error.
Docker Compose files and the custom nginx config file are further below.
Main ressources I used are:
- https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/#project-setup
- https://github.com/nginx-proxy/nginx-proxy/blob/main/README.md
- https://docs.gunicorn.org/en/stable/deploy.html#nginx-configuration
docker-compose.yml: NGINX Proxy
version: "3.9"
services:
nginx-proxy:
image: nginxproxy/nginx-proxy:alpine
container_name: nginx-proxy
volumes:
- conf:/etc/nginx/conf.d
- html:/usr/share/nginx/html
- dhparam:/etc/nginx/dhparam
- vhost:/etc/nginx/vhost.d:ro
- certs:/etc/nginx/certs:ro
- /var/run/docker.sock:/tmp/docker.sock:ro
restart: always
networks:
- nginx-net
ports:
- 80:80
- 443:443
acme:
image: nginxproxy/acme-companion:latest
container_name: nginx-proxy-acme
depends_on:
- nginx-proxy
volumes:
- html:/usr/share/nginx/html
- conf:/etc/nginx/conf.d
- dhparam:/etc/nginx/dhparam
- vhost:/etc/nginx/vhost.d:ro
- certs:/etc/nginx/certs:rw
- acme:/etc/acme.sh
- /var/run/docker.sock:/var/run/docker.sock:ro
environment:
- NGINX_PROXY_CONTAINER=nginx-proxy
- DEFAULT_EMAIL=account@domain.com
restart: always
networks:
- nginx-net
volumes:
conf:
certs:
html:
vhost:
dhparam:
acme:
networks:
nginx-net:
external: true
docker-compose.yml: Django Server
version: '3.8'
# Prod environment
services:
web:
build:
context: .
dockerfile: Dockerfile
command: gunicorn core.wsgi:application --forwarded-allow-ips="172.31.0.0/24,www.example.com,example.com" --bind 0.0.0.0:8083
expose:
- 8083
env_file:
- ./.env
environment:
- VIRTUAL_HOST=example.com,www.example.com
- VIRTUAL_PATH=/django
- LETSENCRYPT_HOST=example.com,www.examplecom
- LETSENCRYPT_EMAIL=account@domain.com
depends_on:
- db
networks:
- nginx-net
db:
image: postgis/postgis:15-3.3-alpine
env_file:
- ./.env
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
ports:
- ${DB_PORT_EXT}:${DB_PORT_INT}
volumes:
postgres_data:
networks:
nginx-net:
external: true
example.com_django / www.example.com_django in /etc/nginx/vhost.d
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme
proxy_set_header Host $http_host;
proxy_redirect off;