【问题标题】:nginx+django+uwsgi static files 403 Forbiddennginx+django+uwsgi静态文件403 Forbidden
【发布时间】:2015-04-28 06:05:37
【问题描述】:

嗨~我正在 CentOS 6.5 中部署一个网站。框架是django1.7。我使用 nginx 和 uwsgi。

除了所有静态文件的 403 Forbidden 之外,该网站现在可用。

我google了几个小时,将整个站点文件夹更改为chmod 777。文件夹的所有者是kltc,nginx.conf中的用户也是kltc。

我不知道为什么问题仍然是 Permission denied。

nginx.conf

user  kltc;
worker_processes  1;

kuaileticao.miyayx.me

 server {
        # the port your site will be served on
        listen 80;
        # the domain name it will serve for
        server_name kuaileticao.miyayx.me; # substitute your machine's IP address or FQDN
        charset utf-8;
        access_log /srv/www/nginx/kuaileticao.miyayx.me/access.log;
        error_log /srv/www/nginx/kuaileticao.miyayx.me/error.log;

        location /static {
            autoindex  on;
            alias /home/kltc/Project/sunny_sports/sunny_sports/static; 
        }

        # Finally, send all non-media requests to the Django server.
        location / {
        uwsgi_pass 127.0.0.1:8001; 
        include uwsgi_params; # the uwsgi_params file you installed
        }
    }

uwsgi.ini

[uwsgi]
# the base directory (full path)
chdir=/home/kltc/Project/sunny_sports
# Django's wsgi file
module=sunny_sports.wsgi:application
master=true
# maximum number of worker processes
processes=10
# the socket (use the full path to be safe
socket=127.0.0.1:8001
chmod-socket=664
chown-socket=kltc:kltc
pidfile=/tmp/project-master.pid
# clear environment on exit
vacuum=true
max-requests=5000
daemonize=sunny_sports.uwsgi.log

错误日志

[error] 34335#0: *73 open() "/home/kltc/Project/sunny_sports/sunny_sports/static/css/font-awesome.min.css" failed (13: Permission denied), client: 157.7.154.194, server: kuaileticao.miyayx.me, request: "GET /static/css/font-awesome.min.css HTTP/1.1", host: "kuaileticao.miyayx.me", referrer: "http://kuaileticao.miyayx.me/"
[kltc@localhost static]$ pwd
/home/kltc/Project/sunny_sports/sunny_sports/static
[kltc@localhost static]$ ls -l
total 32
4 drwxrwxrwx.  2 kltc kltc 4096 Feb 25 18:51 avatars
4 drwxrwxrwx.  7 kltc kltc 4096 Feb 25 18:51 css
4 drwxrwxrwx.  2 kltc kltc 4096 Feb 25 18:51 custom
4 drwxrwxrwx.  2 kltc kltc 4096 Feb 25 18:51 font
4 drwxrwxrwx.  2 kltc kltc 4096 Feb 25 18:51 fonts
4 drwxrwxrwx.  3 kltc kltc 4096 Feb 25 18:51 images
4 drwxrwxrwx.  2 kltc kltc 4096 Feb 25 18:51 img
4 drwxrwxrwx. 10 kltc kltc 4096 Feb 26 08:18 js

>

[kltc@localhost static]$ ps -aux | grep nginx
root     37444  0.0  0.0 110008  2048 ?        Ss   09:02   0:00 nginx: master process /usr/sbin/nginx
kltc     37445  0.0  0.0 112548  3700 ?        S    09:02   0:00 nginx: worker process

我尝试了几种方法。例如,将 nginx.conf 用户更改为root。但它们都不起作用。

希望得到您的帮助。谢谢!

【问题讨论】:

  • error.log 收到什么?此外,如果您将父目录设置为 nginx 工作人员可以读取,这可能会有所帮助。
  • error.log 有很多这样的行:2015/02/26 09:02:49 [error] 37445#0: *11 open() "/home/kltc/Project/sunny_sports/sunny_sports/static/js/jquery.hotkeys.min.js" failed (13: Permission denied), client: 101.20.9.33, server: kuaileticao.miyayx.me, request: "GET /static/js/jquery.hotkeys.min.js HTTP/1.1", host: "kuaileticao.miyayx.me", referrer: "http://kuaileticao.miyayx.me/"
  • 我在听取@supermario 的建议后解决了这个问题。这是因为 /home 目录的权限。非常感谢!
  • 这之前让我绊倒了。乐意效劳。然后我添加了答案。

标签: django nginx static uwsgi


【解决方案1】:

确保您将项目的父目录 (/home) 设置为可供 nginx 工作人员读取。

chown -R kltc:kltc /home 
chmod -R ug+r /home

【讨论】:

  • 很高兴为您提供帮助!所以请考虑接受答案。
  • 也解决了我的问题,基本上所有的父目录都需要被指定的 nginx 用户读取。
【解决方案2】:

确保在设置文件中声明了 STATIC_ROOT。

另外,在运行服务器之前尝试运行python manage.py collectstatic --noinput

希望有效!

【讨论】:

    猜你喜欢
    • 2013-01-26
    • 2017-06-05
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 2013-02-19
    • 2018-12-31
    相关资源
    最近更新 更多