您好我正在尝试使用Django和Nginx在VPS上运行静态以使我的项目生效。我的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没有显示出来。
请尽快通知。
答案 0 :(得分:0)
尝试以这种方式更改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。另外,if (!-f $request_filename)
代替try_files
,最好使用sudo npm install browserify
:http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files