【问题标题】:django+nginx+uwsgi 404 on static filesdjango+nginx+uwsgi 404 静态文件
【发布时间】:2017-06-05 22:42:16
【问题描述】:

我有一个 django 应用程序,使用 nginx 和 uwsgi 提供服务。我的静态文件返回 404。我已将文件的所有权设置为对应于 nginx 用户名。静态文件夹的路径是直接从终端复制的,我运行了 collectstatic(静态文件通过 django 开发服务器成功提供)。我也跑了

chown -R username:username /home 

chmod -R ug+r /home 

我已经尝试过别名和 root。

STATIC_ROOT 在我的 settings.py 中

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

这是我的 nginx.conf:

user username;  
worker_processes 1;

events {

    worker_connections 1024;

}

http {

    sendfile on;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                      text/comma-separated-values
                      text/javascript
                      application/x-javascript
                      application/atom+xml;

    # Configuration containing list of application servers
    upstream app_servers {

        server 127.0.0.1:8080;
        # server 127.0.0.1:8081;
        # ..
        # .

    }

    # Configuration for Nginx
    server {

        # Running port
        listen 80;

        # Settings to serve static files 
        location /static/  {

            # Example:
            # root /full/path/to/application/static/file/dir;
            alias /home/myapp/myapp/static/;

        }

        # Serve a static file (ex. favico)
        # outside /static directory
        location = /favico.ico  {

            root /app/favico.ico;

        }

        # Proxy connections to the application servers
        # app_servers
        location / {

            proxy_pass         http://app_servers;
            proxy_redirect     off;
            proxy_set_header   Host $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-Host $server_name;

        }
    }
} 

【问题讨论】:

  • 你的项目的完整路径是什么?
  • /home/username/uswgi-tutorial/mysite

标签: django nginx uwsgi


【解决方案1】:

/home 目录分配给某些用户是个坏主意。该目录应该属于根目录。改回原来的样子:

chown root:root /home
chmod u=rwx,g=rx,o=rx /home

(您运行的 chmod 可能没有更改其权限,但最好确定一下,因此上面的第二个命令会将其恢复为原来的状态。)

你说你的项目在/home/username下,但是nginx配置说/home/myapp。修复它。我假设它是/home/username/home/username 需要修复一些权限:

chmod o+rx /home/username

另见(我的文章)http://djangodeployment.com/2016/11/21/how-django-static-files-work-in-production/

【讨论】:

    猜你喜欢
    • 2015-04-28
    • 2014-06-07
    • 2017-09-02
    • 2021-01-12
    • 1970-01-01
    • 2016-09-24
    • 2015-11-30
    • 1970-01-01
    • 2013-02-19
    相关资源
    最近更新 更多