How to serve static content with Nginx and Django Gunicorn when using Traefik

后端 未结 2 1814
-上瘾入骨i
-上瘾入骨i 2021-02-10 04:52

I have a web application (Django based) that is utilising multiple containers:

  1. Web Application (Django + Gunicorn)
  2. Traefik (acting as the reverse proxy an
2条回答
  •  再見小時候
    2021-02-10 05:13

    I don't have much idea about Traefik and Docker.

    But I can tell you how you can install nginx and use it to serve static files(which is always recommended in order to not choke the django server by serving static files)

    Install nginx and follow the steps mentioned to setup nginx .

    sudo apt-get install nginx
    

    The site-available file should look something like this:

    server {
            listen 80;
            listen [::]:80;
    
            server_name xyz.com;
            client_max_body_size 20M;
    
            # xyz.com/media/any_static_asset_file.jpg 
            # when anyone hits the above url then the request comes to this part.
            location /media/ {
    
                    # do make sure that the autoindex is off so that your assets are only accessed when you have proper path
    
                    autoindex off; 
    
                    # this is the folder where your asset files are present.
    
                    alias /var/www/services/media/; 
            }
    
            # whenever any request comes to xyz.com then this part would handle the request
            location / {
                    proxy_pass http://unix:/var/www/services/xyz/django_server.sock;
                    proxy_http_version 1.1;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection 'upgrade';
                    proxy_set_header Host $host;
                    proxy_cache_bypass $http_upgrade;
            }
    }
    

提交回复
热议问题