2017-04-06 150 views
1

我有一個nginx服務器與我的網站啓用它正在偵聽443和轉發流量到我的django應用程序服務器使用uWSGI。我可以進入管理頁面並登錄,但不提供靜態文件。我跑了python3 manage.py collectstatic。在使用letsencrypt添加SSL之前,所有工作都正常。聞聽此事在nginx的訪問日誌:靜態文件404找不到錯誤與ssl - Django + uWSGI + 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; 
    } 
} 

我缺少什麼?

+0

我希望您在服務器上運行collectstatic命令,以便將靜態文件收集到指定的文件夾中。 –

+0

命令'python3 manage.py collectstatic'已經運行。 '/ home/ubuntu/api/staticfiles'中有文件。 – pitchdiesel

回答

0

您的alias陳述錯誤。 location的值和alias的值都應以/結尾,或以/結束。

此外,使用root其中alias值以location值結尾。詳情請參閱this document

例如:

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

這工作:

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

感謝@理查德 - 史密斯指着我在正確的方向!