3

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

masegaloeh
  • 18,236
  • 10
  • 57
  • 106
newguy
  • 195
  • 3
  • 10

3 Answers3

4

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.

newguy
  • 195
  • 3
  • 10
3

I had a similar problem like this. It turned out that it was a problem of SELinux that showed after an update.

*This solution applies if your static files are bellow an user home folder

Looking into nginx log in /var/log/nginx/-error.log I could see that nginx process has access denied when trying to open static files.

After that, I looked into the audit log (/var/log/audit/audit.log). This file has all kind of info about what is going on, but is difficult to read.

But there's an nice tool called audit2why (yum install policycoreutils-python)

So if you try this: grep 1433926027.242:416 /var/log/audit/audit.log | audit2why

I've got this number 1433926027.242 looking the tail of audit.log file

When run, it showed the solution. Just need to seed a flag with setsebool:

setsebool -P httpd_read_user_content 1

After that, my nginx could open the static files with no problem. *

Hope it helps!

0

If you are in CentOS you can do:

# This allows to use semanage - SELinux Policy Management tool
sudo yum install -y policycoreutils-python 

sudo chown -R $USER:$USER /home/path/Documents/python_projects/to_pm/static
sudo usermod -a -G $USER nginx
sudo chmod 755 -R /home
sudo semanage fcontext -a -t httpd_sys_content_t /home/path/Documents/python_projects/to_pm/static(/.*)?"
sudo restorecon -R -v /home/path/Documents/python_projects/to_pm/static
sudo systemctl restart nginx
iDevFS
  • 101
  • 1