Nginx – configure nginx for serving django development static files

djangonginx

I am using Nginx to serve static files on django development server. However I can't make it work. The server runs fine but nginx is not able to find static files.

This is my settings in nginx.conf:

listen       8080;
server_name  localhost;

#charset koi8-r;
#access_log  logs/host.access.log  main;

location / {
    #root   html;
    #index  index.html index.htm;
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
}
location /static/  {
    alias /path/Documents/python_projects/to_pm/static/;
    access_log off;
    expires    30d;
}

My project settings.py:

STATIC_URL = '/static/'

# for production
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

And I have also used python manage.py collectstatics to copy all static files to /path/Documents/python_projects/to_pm/static/ folder.

But when I use http://localhost:8080 the site comes up very ugly, clearly the static files are not serving by nginx. How can I fix this?

My error logs said

13: Permission denied` for static files

I check the users and see nginx is running under root as master process and nobody as worker process. I can't change user to root and I can't change the user to my default user as I get this error if I change the user:

[emerg] getgrnam("root") failed in /usr/local/etc/nginx/nginx.conf:2

Best Answer

I have found the answer to my question:

The answer is in this article:

https://gist.github.com/jhjguxin/6208474

As it points out:

Nginx needs to have read permission of the files that should be served AND 
have execute permission in each of the parent directories along the path from 
the root to the served files.

The static folder needs to owned by your nginx user too.