skip to Main Content

I have django 3.1 installed on digitalocean ubuntu 20.04 and nginx/1.18.0 (Ubuntu) and using rest api

Following are my static and media files settings.

STATIC_URL = '/static/'
STATICFILES_DIRS = [
  BASE_DIR / "static",
]

STATIC_ROOT = BASE_DIR / 'static_in_env'

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

Following is my Digital Ocean Nginx Conf

location /static/ {
    root /home/username/backend/src;
}

location /media {
    root /home/username/backend/src;
}

it’s giving me forbidden on even files less than 100kb.

Also After checking Nginx logs I found that Permission Denied Error.

5

Answers


  1. Hello Harsh Sonawane try put slash after media like this

    location /media/ {
        root /home/username/backend/src;
    }
    
    Login or Signup to reply.
  2. Your config seems to be OK.
    What you should check is you have installed NGINX as a root user or the user that has access to the files you are trying to serve.

    Login or Signup to reply.
  3. Please try:

    location /static/ {
        root /home/username/backend/src/static/;
    }
    location /media/ {
        root /home/username/backend/src/media/;
    }
    

    if that does not work try changing "root" for "alias" like this:

    location /static/ {
        alias /home/username/backend/src/static/;
    }
    location /media/ {
        alias /home/username/backend/src/media/;
    }
    
    Login or Signup to reply.
  4. I solved this error by:

    First:- chmod -R 777 media (outside of media folder)

    Second:- in /etc/nginx/sites-available/project.conf

    location /static/ {
    
        root /home/username/project;
    }
    location /media/ {
    
        root /home/username/project;
    
    }
    

    to

    location /static/ {
    
        alias /home/username/project/static/;
    }
    location /media/ {
    
        alias /home/username/project/media/;
    }
    

    Third:- reload your service file and nginx

    sudo systemd restart project.service

    sudo systemd restart nginx

    Login or Signup to reply.
  5. Make DIR and Change permissions on Ubuntu server:

    sudo mkdir -pv /var/www/your_project/media/
    sudo chown -cR ubuntu:ubuntu /var/www/your_project/media
    

    and then change settings.py

    MEDIA_URL = '/var/www/your_project/media/'
    MEDIA_ROOT = '/var/www/your_project/media'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search