403 Forbidden nginx/1.18.0 (Ubuntu) for Django 3.1, facing issue in media files and admin css

Viewed 1163

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.

4 Answers

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

Hello Harsh Sonawane try put slash after media like this

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

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.

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/;
}
Related