繁体   English   中英

Django在生产中不提供静态文件

[英]Django not serving static files in production

settings.py:

DEBUG = False

STATIC_ROOT = '/opt/app/statics/'

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/statics/'

# Additional locations of static files
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'statics'),)

urls.py

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import settings

if settings.DEBUG == True:
    urlpatterns += staticfiles_urlpatterns()

/ etc / nginx / sites-available / app

server {
    server_name xxx.xxx.xxx.xxx;

    access_log on;

    location /statics/ {
        alias /opt/app/statics/;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        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"';
    }
}

/ etc / nginx / sites-enabled / app与sites-available完全相同 。静态文件文件夹已被授予最高特权(chmod 777)。

每当我尝试使用以下命令运行服务器时:

gunicorn -b 0.0.0.0:8000 example.wsgi:application

它实际上在运行服务器,但不能正确重定向到静态文件

请使用此:

root /opt/app/statics/;

代替:

alias /opt/app/statics/;

如果您使用root进行的配置是这样的,

    location /static/ {
            root /opt/app/statics/;
    }

在这种情况下,Nginx将得出的最终路径将是

/opt/app/statics/statics

这将返回404,因为statics /中没有statics /

这是因为位置部分被附加到根中指定的路径中。 因此,使用root,正确的方法是

location /static/ {
    root /opt/app/;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM