skip to Main Content

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

3

Answers


  1. 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/'
    
    Login or Signup to reply.
  2. sudo nano /etc/nginx/sites-enabled/inventory.conf

    the upstream component nginx needs to connect to

    upstream django {
        server unix:///home/user/Inventory-System/inventory.sock;
    }
    
    #configuration of the server
    server {
        listen      80;
        server_name trading.code www.trading.code;
        charset     utf-8;
        # max upload size
        client_max_body_size 75M;
        # Django media and static files
        location /media {
            autoindex on;
            alias home/user/Inventory-System/media/;
        }
    
    location /static {
        autoindex on;
        alias home/user/Inventory-System/static/;
    }
    # Send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include    home/user/Inventory-System/uwsgi_params;
    } }
    

    then

    user@user: /etc/nginx/sites-enabled$ sudo rm default
    user@user: $ sudo /etc/init.d/nginx restart

    finally

    check http://127.0.0.0/media/

    Login or Signup to reply.
  3. I also faced this problem and I add this in my sudo nano /etc/nginx/sites-available/project file

    server {
    
        listen 80;
    
        server_name 45.76.42.234;
    
        location = /favicon.ico { access_log off; log_not_found off; }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/run/gunicorn.sock;
        }
    
        location  /static/ {
            root /home/django/Django_tutorials/21_Django-deployment;
        }
    
        location  /media/ {
            root /home/django/Django_tutorials/21_Django-deployment;
        }
        location /Images/ {
              alias /home/django/Django_tutorials/21_Django-deployment/Images/;
        }
    }
    

    where Images is my image folder that contains uploaded images

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search