无法在 DigitalOcean VPS 上使用 Nginx 为 Django 提供静态服务
Can't serve statics with Nginx on DigitalOcean VPS for Django
您好,我正在尝试在 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 没有显示。
请尽快指教
尝试以这种方式更改 static
和 media
块:
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
被搜索。
P.S。另外,最好使用 try_files
而不是 if (!-f $request_filename)
:http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
您好,我正在尝试在 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 没有显示。
请尽快指教
尝试以这种方式更改 static
和 media
块:
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
被搜索。
P.S。另外,最好使用 try_files
而不是 if (!-f $request_filename)
:http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files