我按照this教程在DigitalOcean上部署了Django项目。我已经安装了Nginx和Supervisor,并且一切正常,直到我在settings.py中将DEBUG选项设置为False
我试图配置nginx.conf和settings.py万次。将root更改为别名无济于事。
Nginx配置文件:
upstream app_server {
server unix:/home/db1/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
# add here the ip address of your server
# or a domain pointing to that ip (like example.com or www.example.com)
server_name ...;
keepalive_timeout 5;
client_max_body_size 4G;
access_log /home/db1/logs/nginx-access.log;
error_log /home/db1/logs/nginx-error.log;
location /static/ {
root /home/db1/site1/static;
}
# checks for static file, if not found proxy to app
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
Settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
答案 0 :(得分:0)
这很棘手,只需根据DEBUG
的状态使用这两行之一
STATIC_ROOT = os.path.join(BASE_DIR, 'static/') #Production
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static/'), ) #Development
答案 1 :(得分:0)
以下几步帮助我解决了这个问题。不知道到底哪个是实际的解决方案)
不过,我仍然不明白为什么添加PORT会导致静态文件保留在服务器上