How to serve django media files via nginx ?

前端 未结 2 948
傲寒
傲寒 2020-12-08 07:30

I\'m new at Nginx, I\'ve successfully bound my django project to Nginx. However I can\'t serve my static files and I guess I set my media folder\'s location wrongly. Here is

相关标签:
2条回答
  • 2020-12-08 07:30

    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.

    0 讨论(0)
  • 2020-12-08 07:33

    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.

    0 讨论(0)
提交回复
热议问题