Nginx
Nginx + Gunicorn + Django - nginx 不允許靜態文件
更新的程式碼 - 但這仍然不起作用。
Settings.py
STATIC_ROOT = '/home/ubuntu/virtualenv/mysite/homelaunch/static/' STATIC_URL = '/static/'
nginx conf :: /etc/nginx/sites-enabled/mysite
server { server_name ec2-x-x-x-x.compute-1.amazonaws.com; access_log /home/ubuntu/virtualenv/mysite/error/access.log; error_log /home/ubuntu/virtualenv/mysite/error/error.log warn; connection_pool_size 2048; location /static/ { #alias /home/ubuntu/virtualenv/mysite/homelaunch/static/; #alias /static/; root /home/ubuntu/virtualenv/mysite/homelaunch/; } location / { proxy_pass http://127.0.0.1:8001; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_set_header X-Forwarded-Host $server_name; #proxy_set_header X-Real-IP $remote_addr; add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"'; } }
gunicorn 配置 -
command = '/usr/local/bin/gunicorn' logfile = "/home/ubuntu/virtualenv/mysite/error/gunicorn.log" loglevel = "info" pythonpath = '/home/ubuntu/virtualenv/mysite' bind = '127.0.0.1:8001'
img 目錄和 css 目錄的完整路徑:
/home/ubuntu/virtualenv/mysite/homelaunch/static/css /home/ubuntu/virtualenv/mysite/homelaunch/static/img
在 error.log 中收到錯誤:
001/favicon.ico", host: "ec2-xx-xx-xx-xx.compute-1.amazonaws.com" 2013/09/02 16:57:42 [error] 2819#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: ec2-xx-xx-xx-xx.compute-1.amazonaws.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: "ec2-xx-xx-xx-xx.compute-1.amazonaws.com"
有什麼明顯的錯誤嗎?當我嘗試通過輸出的 html 原始碼查看 img 和 css 文件時,仍然出現 404 錯誤。
此答案已被編輯以總結解決方案。
在nginx的配置中
更換:
alias /home/ubuntu/virtualenv/mysite/homelaunch/;
和:
root /home/ubuntu/virtualenv/mysite/homelaunch/;
有關更多詳細資訊,請參閱文件。
更換:
proxy_set_header X-Forwarded-Host $server_name; proxy_set_header X-Real-IP $remote_addr;
和:
proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
在 gunicorn 的配置中
更換:
bind = 'ec2-x-x-x-x.compute-1.amazonaws.com:8001'
和:
bind = '127.0.0.1:8001'
在 django 的設置中
設置
STATIC_ROOT = '/home/ubuntu/virtualenv/mysite/homelaunch/static/'
哪個是正確的絕對文件系統路徑。設置
DEBUG = True
有更詳細的錯誤資訊。