更新代码-但仍不起作用。
设置.py
STATIC_ROOT = '/home/ubuntu/virtualenv/mysite/homelaunch/static/'
STATIC_URL = '/static/'
nginx 配置 :: /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 错误。
答案1
该答案已被编辑以总结解决方案。
在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
以获取更详细的错误信息。
答案2
替换为:
location /static/ {
alias /home/ubuntu/virtualenv/mysite/homelaunch/;
}
对于这个:
location /static/ {
alias /home/ubuntu/virtualenv/mysite/homelaunch/static/;
}
如果你使用别名,你需要输入绝对路径,因为你已经在 settings.py 上定义了静态目录
STATIC_URL = '/static/'
因此如果你将此目录的别名设为 /home/ubuntu/virtualenv/mysite/homelaunch/;
Nginx 认为静态文件位于 /home/ubuntu/virtualenv/mysite/homelaunch/ 上,而不是 /home/ubuntu/virtualenv/mysite/homelaunch/static 中
抱歉我的英语不好,我会努力提高 :(
我希望你能理解我。