By mikewinkel
I am following a tutorial on how to dockerize django with postgres, gunicorn and nginx on an ubuntu 20.04 system. (see as reference: https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/ )
Unfortunately while setting up the nginx part of the app I’ve seem to have stumbled accross a problem with setting up the reverse proxy correctly.
When I try to access the django app via <ip-address>:1337 I get ‘The requested resource was not found on this server.’ error.
While I know that this is due to django not being properly served to nginx, but I’m quite new at web deployment and currently at a loss on how to proceed.
The relevant settings:
docker-compose.prod.yml
services:
web:
build:
context: ./app
dockerfile: Dockerfile.prod
command: gunicorn hello_django.wsgi:application --bind 0.0.0.0:8000
expose:
- 8000
env_file:
- ./.env.prod
depends_on:
- db
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.env.prod.db
nginx:
build: ./nginx
ports:
- 1337:80
depends_on:
- web
volumes:
postgres_data:
nginx.conf
upstream hello_django {
server web:8000;
}
server {
listen 80;
location / {
proxy_pass http://hello_django;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
nginx Dockerfile
FROM nginx:1.19.0-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
ALLOWED_HOSTS = ['<ip-address>', 'localhost']
Any help would be really appreciated.
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!
Hi there @mikewinkel,
What I could suggest is checking the error log of both your Nginx and Web containers.
To do that you can run the following command:
- docker ps -a
This would return a list of your containers, and then get the container ID and check the logs:
- docker logs container_id_here
This will give you more information on why the services are not returning any responses.
Feel free to share the errors here if you wish.
Regards, Bobby
Hi @mikewinkel !!
Did you, by any chance, find a solution to this particular problem?
I’m struggling with the same problem on a very similar docker django-gunicorn/nginx configuration and I can’t figure it out.
Thanks a lot!
Heya,
an update on an older topic in case someone finds it helpful
Your setup and configuration for dockerizing Django with Postgres, Gunicorn, and Nginx seem mostly correct, but the issue you’re facing with accessing the Django app via <ip-address>:1337 and getting a ‘The requested resource was not found on this server’ error is likely due to a misconfiguration in Nginx or the Django settings. Let’s go through a few steps to troubleshoot and hopefully resolve this issue.
ALLOWED_HOSTSIn your settings.py, you’ve added <ip-address> and localhost to ALLOWED_HOSTS. Make sure that the <ip-address> here is correctly replaced with the actual IP address of your server.
ALLOWED_HOSTS = ['your_server_ip', 'localhost']
Your Nginx configuration seems to be set up correctly to proxy requests to your Django app running in the web container. However, double-check the following:
nginx.conf is correctly placed in the Nginx container and that the path in the Dockerfile is correct.nginx.conf.upstream directive in Nginx points to web:8000. Ensure that the web service is correctly named in your docker-compose.prod.yml file.Since you’re using Docker Compose, all containers should be on the same network by default, but it’s worth checking if there are any networking issues:
curl to test if it can reach the Django app.Ensure that your Django project’s URL configuration (urls.py) is correctly set up to serve the pages you’re trying to access.
Inspect the logs for your Docker containers to identify any errors:
docker-compose logs web
docker-compose logs nginx
Make sure your server’s firewall is not blocking port 1337. On Ubuntu, you can check this with UFW:
sudo ufw status
If the port is not allowed, you can open it with:
sudo ufw allow 1337/tcp
After making configuration changes, rebuild and restart your Docker containers:
docker-compose down
docker-compose up --build
Sometimes, browsers cache pages aggressively. Try accessing your app in incognito mode or clearing the browser cache.
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.