【发布时间】:2019-10-17 13:34:09
【问题描述】:
我正在按照 digitalocean 教程使用 django、gunicorn 和 nginx 在服务器上构建站点。该网站是实时的,但静态文件不显示。
语法简洁 我得出的结论是,问题必须来自 django 或 nginx(因为任何静态文件的配置都与它们中的任何一个有关
我尝试修改 django 和 nginx 配置,因为它们看起来像是源代码。
1)django settings.py文件django_project/django_project/settings.py中的静态位置定义
STATIC_URL = '/static/',
STATIC_ROOT = "/dir1/dir2/dir3/django_project/static"
2) /etc/nginx/nginx.conf 中的 nginx 配置文件
http {
server {
listen 800 default_server;
server_name mysite.com;
return 444;
location /static/ {
alias /dir1/dir2/dir3/django_project/static;
}
}
3) /etc/nginx/sites-availables 中的默认文件:
server {
listen 80;
server_name mysite.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /dir1/dir2/dir3/django_project/static;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
我希望显示静态文件
============================== 编辑:
我尝试打开静态文件时收到的错误消息是:
找不到
在此服务器上找不到请求的资源。
【问题讨论】:
-
您是否运行了
python manage.py collectstatic(它将所有静态文件复制到/dir1/dir2/dir3/django_project)? -
是的,并检查了文件是否存在
-
检查 nginx 日志。可能是权限问题。
-
在
/etc/nginx/sites-availables中将root /dir1/dir2/dir3/django_project/static;更改为alias /dir1/dir2/dir3/django_project/static; -
你可以看到 django here 和 nginx.conf 的完整 nginx 配置文件
标签: python django nginx server