Django静态文件和Nginx [英] Django static file and nginx

查看:154
本文介绍了Django静态文件和Nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过nginx在example.org(不是实际的ip)上加载服务器时,内容显示正常. 但是,静态文件(如css和js)不会显示.需要做什么 更改为显示静态文件?我只是在nginx上执行python manage.py runserver来检查是否正在馈送文件,但是仅显示模板而没有静态文件.

When I load up the server at example.org(not actual ip) via nginx, the contents show up fine. However, the static files such as css and js doesn't show up. What needs to be changed for static files to be shown? I am just doing python manage.py runserver on nginx to check if files are being fed, but only template show up without static files.

实际的静态文件夹(css/js/img)位于

The actual static folder(css/js/img) is at

/opt/myenv/myproject/myproject/static

我的/etc/nginx/sites-available/myproject为

I have /etc/nginx/sites-available/myproject as

server {
    listen 80;
    server_name example.org; ##not actual ip
    access_log /var/log/nginx/example.log;

    location /static/ { 
        alias /opt/myenv/static/; 
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

这就是我对/opt/myenv/myproject/mysite/settings.py的要求.

This is what I have for /opt/myenv/myproject/mysite/settings.py.

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

推荐答案

在您的问题中,您指出静态资源文件位于/opt/myenv/myproject/myproject/static,但您的nginx配置文件将其放置在/opt/myenv/static.

In your question you state that your static resource files are located at /opt/myenv/myproject/myproject/static but your nginx configuration file places them at /opt/myenv/static.

此外,您使用alias,其中root效率更高.试试:

Also, you use alias where root would be more efficient. Try:

location /static {
    root /opt/myenv/myproject/myproject;
}

这篇关于Django静态文件和Nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆