user3439521
user3439521

Reputation:

Nginx Not Serving Static Files (Django + Gunicorn) Permission denied

nginx.conf

server {
    listen 80;
    server_name serveraddress.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ec2-user/projectname;
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://unix:/home/ec2-user/projectname/projectname.sock;
    }
}

settings.py

STATIC_URL = '/static/'

STATICFILES_DIR = '/home/ec2-user/projectname/static/'

STATIC_ROOT = '/home/ec2-user/projectname/static/'

If I run the server using the Django development server with manage.py runserver or with gunicorn, all the static files work perfectly, but using nginx on port 80, none of the static files work; which leads me to believe that it's an issue involving nginx. And yes, I've ran python manage.py collectstatic and 'django.contrib.staticfiles' is installed. I'm using RHEL 7 (Centos 7).

Nginx error.log

2016/09/22 20:44:33 [error] 322#0: *371 open() "/home/ec2-user/projectname/static/css/home.css" failed (13: Permission denied), client :##.###.##.##, server: ##.###.###.###, request: "GET /static/css/home.css HTTP/1.1", host: "##.###.###.###", referrer: "http://##.###.###.###/"

Upvotes: 0

Views: 993

Answers (1)

monkut
monkut

Reputation: 43870

You have a permission denied issue it seems. (13: Permission denied)

nginx often runs under it's own nginx user, and this user probably does not have the permissions to access the location/files and can't serve them.

Upvotes: 1

Related Questions