简体   繁体   English

如何使用django和nginx配置静态和媒体文件?

[英]How to configure static and media files with django and nginx?

I have next settings.py on my local server. 我在本地服务器上有下一个settings.py。

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_root")
MEDIA_URL = '/media/'
MEDIA_ROOT= os.path.join(os.path.dirname(BASE_DIR), "media_root")

I know that in production Nginx should handle static and media. 我知道在生产中Nginx应该处理静态和媒体。 Okay. 好的。 I use gunicorn and supervisor on my prod server. 我在产品服务器上使用gunicorn和超级用户。 My nginx config: 我的nginx配置:

server {
    listen 8000;
    server_name 194.87.95.46;
    access_log  /var/log/nginx/example.log;

    location /static {
        alias /home/split/static_root/;
    }


     location /media {
        alias /home/split/media_root/;
    }

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

But nginx doesnt handle static and media, what's the problem? 但是nginx无法处理静态和媒体问题,这是什么问题?

I think your issue is caused by trailing slashes - you haven't provided a trailing slash in the location definition, but you have in the alias. 认为您的问题是由斜杠引起的-您未在位置定义中提供斜杠,但在别名中提供了斜杠。 Try this instead: 尝试以下方法:

location /static/ {
    alias /home/split/static_root/;
}

location /media/ {
    alias /home/split/media_root/;
}

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

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