By meghaggarwal
This is my ngnix->vhost.d->default conf file.
upstream djangotango.meghaggarwal.com {
server web:8000;
}
server {
listen 80;
listen 443;
server_name djangotango.meghaggarwal.com
location / {
proxy_pass http://djangotango.meghaggarwal.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /home/app/web/static/;
add_header Access-Control-Allow-Origin *;
}
location /media/ {
alias /home/app/web/media/;
add_header Access-Control-Allow-Origin *;
}
}
docker-compose.staging.yml
version: '3.8'
networks:
public_network:
name: public_network
driver: bridge
services:
web:
build:
context: .
dockerfile: Dockerfile.prod
command: gunicorn djangotango.wsgi:application --bind 0.0.0.0:8000
volumes:
# - .:/home/app/web/
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
expose:
- 8000
env_file:
- ./.env.staging
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.env.staging.db
depends_on:
- web
pgadmin:
image: dpage/pgadmin4
env_file:
- ./.env.staging.db
ports:
- "8080:80"
volumes:
- pgadmin-data:/var/lib/pgadmin
depends_on:
- db
links:
- "db:pgsql-server"
environment:
- PGADMIN_DEFAULT_EMAIL=pgadmin4@pgadmin.org
- PGADMIN_DEFAULT_PASSWORD=root
- PGADMIN_LISTEN_PORT=80
nginx-proxy:
build: ./nginx
restart: always
ports:
- 443:443
- 80:80
volumes:
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- vhost:/etc/nginx/vhost.d
- /var/run/docker.sock:/tmp/docker.sock:ro
depends_on:
- web
networks:
- public_network
nginx-proxy-letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
env_file:
- .env.staging.proxy-companion
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- vhost:/etc/nginx/vhost.d
depends_on:
- nginx-proxy
networks:
- public_network
volumes:
postgres_data:
pgadmin-data:
static_volume:
media_volume:
certs:
html:
vhost:
.env.staging.db
VIRTUAL_HOST=djangotango.meghaggarwal.com
VIRTUAL_PORT=8000
LETSENCRYPT_HOST=djangotango.meghaggarwal.com
I have shown the main snippets only. I’ve tried my best through documenatation, stackoverflow but no help, almost feel giving up
Can anyone help me to configure my ngnix conf file. I keep getting 502 error when loading browser.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Does some requests work and then it stops or does not work at all? my guess is that the container web server does not answer for some reason.
Can you dump the generated nginx config docker exec <nginx-proxy-container-id> cat /etc/nginx/conf.d/default.conf.
Also maybe useful to try to publish the web container port and access it without going thru nginx-proxy.
The 502 Bad Gateway error in an Nginx reverse proxy setup, especially with Docker and services like jwilder/nginx-proxy, is typically due to misconfiguration in the networking between Nginx and the backend service (in your case, a Django application). Let’s go through your setup and identify potential causes and solutions.
proxy_pass directive is trying to send requests to http://djangotango.meghaggarwal.com. However, within the context of Docker and your defined upstream, it should point to the upstream service web, which runs your Django app:location / {
proxy_pass http://web:8000;
# Other proxy settings...
}
Upstream Service Name in Docker: In your docker-compose.staging.yml, the service name web is correct. Ensure that the web service is running and accessible on port 8000, as you’ve defined.
Docker Networking: Your services are on the public_network network, and the Nginx proxy should be able to communicate with the web service on this network. Verify that the network is functioning correctly.
DNS Resolution: The use of domain names like djangotango.meghaggarwal.com in your Docker setup for internal communication can be problematic unless properly resolved. In Docker networks, use service names for internal communication.
Exposing Ports: Ensure that the web service exposes the correct port (8000), which it does in your compose file. No change is needed here, but it’s a common mistake.
Nginx Proxy Companion: The nginx-proxy-letsencrypt companion is used for SSL, which shouldn’t impact basic proxy functionality. Ensure that this service is correctly configured if you’re also setting up HTTPS.
Volumes and Static/Media Files: Your static and media files are mounted as volumes in both the Nginx proxy and web service. Ensure these paths are correctly set up and accessible.
Check Logs: Look at the logs of both the Nginx proxy and the web service. For Docker, use docker-compose logs nginx-proxy and docker-compose logs web to get relevant logs.
Restart Services: Sometimes, simply restarting the services can resolve networking issues: docker-compose down and then docker-compose up -d.
Nginx Config Test: Ensure your Nginx configuration is correct. If you can access the Nginx container, run nginx -t to test the configuration.
Docker Network Inspection: Inspect the Docker network to ensure that services are correctly connected: docker network inspect public_network.
Container Health: Check if all the relevant containers are up and running: docker-compose ps.
After you make the necessary changes, especially the proxy_pass directive in your Nginx configuration, test again. If the issue persists, the logs will be the most helpful resource for further debugging. Remember that configuring reverse proxies and Docker networking can be tricky, and small misconfigurations can lead to errors like the one you’re experiencing.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.