How to serve django media files via nginx ?

半腔热情 提交于 2019-11-28 18:45:48
j_syk

Here's a example of how I have my nginx servers setup

server {
    server_name example.com www.example.com;
    location /static {
        autoindex on;
        alias /home/myusername/myproject/static/;
    }
    location /media {
        autoindex on;
        alias /home/myusername/myproject/media/;
    }
    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

I serve django with Gunicorn on localhost port 8000. (that's what the proxy_pass is for)

The Nginx wiki example configuration may help you too. Notice in their static file serving they specify allowed filetypes and use 'root' instead of 'alias' but they are similar.

This ServerFault question may help.

The following code works for me:

server {
server_name example.com www.example.com;
location /static {
    autoindex on;
    **alias /home/myusername/myproject/;**
}
location /media {
    autoindex on;
    **alias /home/myusername/myproject/;**
}
location / {
    proxy_pass http://127.0.0.1:8000;
}

}

I bold the different parts according to the previous answer.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!