I was setting up basic Django site with nginx. When i navigate to the url of the file i get error 404 Not Found nginx/1.18.0 (Ubuntu)
This is the conf file
upstream django {
server unix:///home/jo/testproject/testproject.sock;
}
# configuration of the server
server {
listen 80;
server_name _;
charset utf-8;
# max upload size
client_max_body_size 75M;
# Django media and static files
location /media/ {
alias /home/jo/testproject/media/;
}
location /static {
alias /home/jo/testproject/static;
}
# Send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/jo/testproject/uwsgi_params;
}
}
the media folder is in /home/jo/testproject/media and inside it i have media.gif, but ip/media/media.gif doesnt work
when i write the ip in the browser i get
Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com.
Thank you for using nginx.
I think everything else i setup correctly
Docker + NGINX + Gunicorn + Django
Django project:
djangoapp
- ...
- media
- static
- manage.py
- requirements.txt
nginx
- Dockerfile
- nginx.conf
docker-compose.yml
Dockerfile
Dockerfile:
FROM ubuntu:20.04
# Prevents Python from writing pyc files to disc
ENV PYTHONDONTWRITEBYTECODE 1
# Prevents Python from buffering stdout and stderr
ENV PYTHONUNBUFFERED 1
ENV DEBUG 0
# https://hub.docker.com/r/fnndsc/ubuntu-python3/dockerfile
RUN apt-get update \
&& apt-get install -y python3-pip gunicorn
RUN mkdir /app
COPY djangoapp/requirements.txt requirements.txt
COPY djangoapp /app/
RUN pip3 install -r requirements.txt
WORKDIR /app/
EXPOSE 8000
CMD ["gunicorn3", "-b", "0.0.0.0:8000", "djangoapp.wsgi:application", "--workers=2"]
docker-compose.yml:
version: '3'
services:
djangoapp:
build: .
container_name: djangoapp1
volumes:
- .:/djangoapp
- static:/app/static
- media:/app/media
nginx:
build: ./nginx
container_name: nginx
environment:
- SERVER_NAME=8.43.162.54
ports:
- "80:80"
volumes:
- .:/djangoapp
- static:/app/static
- media:/app/media
restart: always
networks:
default:
driver: bridge
volumes:
media:
static:
nginx/Dockerfile:
FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/
nginx/nginx.conf:
server {
listen 80;
server_name $SERVER_NAME;
location /static/ {
alias /app/static/;
}
location /media/ {
alias /app/media/;
}
location / {
proxy_set_header Host $host;
proxy_pass http://djangoapp1:8000;
}
}
settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = []
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With