【问题标题】:Static files 404 not found error with ssl - Django + uWSGI + nginx使用 ssl - Django + uWSGI + nginx 找不到静态文件 404 错误
【发布时间】:2017-09-02 04:28:10
【问题描述】:

我有一个启用了我的站点的 nginx 服务器,它正在侦听 443 并将流量转发到使用 uWSGI 的 django 应用服务器。我可以进入管理页面并登录,但不提供静态文件。我跑过python3 manage.py collectstatic。在使用letsencrypt 添加 SSL 之前一切正常。在 nginx 访问日志中获取此信息:

"GET /static/admin/css/base.css HTTP/1.0" 404

这是我的反向代理 nginx 站点配置:

upstream staging_app_server {
    server 52.52.52.52;
}

server {
    listen 80;
    server_name staging.site.com;
    rewrite ^/(.*)  https://staging.site.com/$1 permanent;
}

server {

    listen 443;
    server_name staging.site.com;

    include snippets/ssl-staging.site.com.conf;
    include snippets/ssl-params.conf;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;

    access_log            /var/log/nginx/staging.access.log;

    location / {

      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-Proto $scheme;

      proxy_pass          http://staging_app_server;
      proxy_redirect      http://staging_app_server https://staging_app_server;
    }

    location ~ /.well-known {
                allow all;
    }

  }

以及应用服务器 nginx 配置:

server {
    listen 80;
    server_name staging.site.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        alias /home/ubuntu/api/staticfiles;
    }

    location /media/ {
        alias /home/ubuntu/api/media;
    }

    location / {
        include         uwsgi_params;
        uwsgi_pass      unix:/run/uwsgi/api.sock;
        uwsgi_param     UWSGI_SCHEME $scheme;
        uwsgi_param       Host $host;
        uwsgi_param       X-Real-IP $remote_addr;
        uwsgi_param       X-Forwarded-For $proxy_add_x_forwarded_for;
        uwsgi_param       X-Forwarded-Host $server_name;
        uwsgi_param       X-Forwarded-Proto $scheme;
    }
}

我错过了什么?

【问题讨论】:

  • 希望你在服务器运行collectstatic命令,将静态文件收集到你指定的文件夹中。
  • 命令python3 manage.py collectstatic 已运行。 /home/ubuntu/api/staticfiles中有文件。

标签: django ssl nginx uwsgi lets-encrypt


【解决方案1】:

这行得通:

location /static {
    autoindex on;
    alias  /home/ubuntu/api/staticfiles;
}

感谢@richard-smith 为我指明了正确的方向!

【讨论】:

    【解决方案2】:

    您的alias 陈述是错误的。 location 的值和alias 的值都应该以/ 结尾,或者都不以/ 结尾。

    另外,使用root,其中alias 值以location 值结尾。详情请见this document

    例如:

    location /static/ {
        alias /home/ubuntu/api/staticfiles/;
    }
    location /media/ {
        root /home/ubuntu/api;
    }
    

    【讨论】:

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