我正在尝试将我的 django 应用程序部署到我的 VPS。所以我遵循了本教程( link ),我遇到的唯一问题是我无法显示我的静态文件。
我可以看到我的静态文件(所以 nginx 工作正常):
例如:http://188.xxx.xxx.93/static/css/bootstrap.css .
但是当我访问我的网站时,我可以看到它链接了一个像这样的 css 文件:<link href="/static/css/bootstrap.css" rel="stylesheet">
所以它试图找到文件(使用端口):http://188.xxx.xxx.93 : 8001 /static/bootstrap.css 显然不起作用。为了使静态文件工作,我应该对我的 Django 设置进行哪些更改?
您可以在下面找到我在 VPS 上的文件结构。
Virtual Env: /opt/myapps/
Django project: /opt/myapps/uniprogress/
Static Files: /opt/myapps/uniprogress/static/
Nginx 配置:/etc/nginx/sites-available/uniprogress
server {
server_name 188.xxx.xxx.93;
access_log off;
location /static/ {
alias /opt/myapps/uniprogress/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
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"';
}
}
最后在我的 django settings.py 中:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_ROOT = '/opt/myapps/uniprogress/static/'
STATIC_URL = '/static/'
我还使用了:python manage.py collectstatic 但我的静态文件仍然不会显示。
最佳答案
我正在阅读另一个教程 link我发现 STATIC_URL 可以是这样的:STATIC_URL = 'http://static.example.com/'
所以我想解决方案是这个?
如果有经验的人可以让我知道这是正确的方法还是不正确的方法?
附言。我现在在我网站上的 css 文件是这样的:<link href="http://188.xxx.xxx.93/static/css/bootstrap.min.css" rel="stylesheet">
.
关于django - 部署 Django,静态文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23019406/