大网数据广告活动
服务器运维问答

configure nginx for serving django development static files

· 52CE 我爱测

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 local…

问题原文

原帖链接 · newguy · CC BY-SA 3.0

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

社区高分解答

原帖链接 · newguy · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-12-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。