gpt4 book ai didi

django - 无法在 Django 的 DigitalOcean VPS 上使用 Nginx 提供静态服务

转载 作者:太空宇宙 更新时间:2023-11-03 17:11:46 24 4
gpt4 key购买 nike

您好,我正在尝试在 VPS 上使用 Django 和 Nginx 提供静态服务,以使我的项目上线。我的 settings.py 包括以下内容。

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')

我的项目文件夹包括:

static
----static
----static-only
----media
----templates

在 Nginx 中我得到以下信息:

upstream gunicorn_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server 127.0.0.1:9000 fail_timeout=0;
}

server {

listen 80;
server_name example.com;
keepalive_timeout 5;
client_max_body_size 4G;

access_log /home/projectuser/logs/nginx-access.log;
error_log /home/projectuser/logs/nginx-error.log;

location /static/ {
alias /venv/project/static/static;
}

location /media/ {
alias /venv/project/static/media;
}
location / {

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

if (!-f $request_filename) {
proxy_pass http://gunicorn_server;
break;
}
}

}

无论如何我都无法提供静态服务,而且管理面板 css 也没有显示。

请尽快告知。

最佳答案

尝试以这种方式更改 staticmedia block :

location /static/ {
root /venv/project/static;
}

location /media/ {
root /venv/project/static;
}

这样,http://example.com/static/admin/css/dashboard.css 将被搜索为 /venv/project/static/static/admin/css/dashboard.css 在文件系统上。

附言也不要使用 if (!-f $request_filename),最好使用 try_files: http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

关于django - 无法在 Django 的 DigitalOcean VPS 上使用 Nginx 提供静态服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35272839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com