title: Serving static files with Ngnix, Django and Docker
body: <p>I am fairly new to Nginx and Docker and am currently facing an issue regarding a docker container setup. The setup consists of three containers: Nginx, Django and Postgres. It works as expected for the most part, however, I am not able to access static files through Nginx.</p>
  
  <p>Here is the nginx.conf:</p>
  
  <pre><code>user nginx;
  worker_processes  1;
  error_log /var/log/nginx/error.log warn;
  pid /var/run/nginx.pid;
  events {
    worker_connections 1024;
  }
  http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local]'
                    '"$request" $status $body_bytes_sent'
                    '"$http_referer" "$http_user_agent"'
                    '"$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log main;
    upstream server {
      server server:8000;
    }
    server {
      listen 80 default_server;
      listen [::]:80 default_server;
      server_name localhost;
      charset utf-8;
      root /usr/share/nginx/html;
      index index.html;
      location / {
        try_files $uri $uri/ @rewrites;
      }
      location @rewrites {
        rewrite ^(.+)$ /index.html last;
      }
      location ^~ /static/ {
        autoindex on;
        alias /usr/share/nginx/html/static/; 
      }
      location ~ ^/api {
        proxy_pass http://server;
      }
      location ~ ^/admin {
        proxy_pass http://server;
      }
    }
  }
  </code></pre>
  
  <p>I would expect Nginx to serve <em>/usr/share/nginx/html/static/</em> when I access the address <em>localhost:8000/static</em>. I did check the container fs at <em>/usr/share/nginx/html/static/</em>, and the static files are present.</p>
  
  <p>Here is the docker-compose.yml:</p>
  
  <pre><code>version: "3"
  services:
    nginx:
      container_name: nginx
      build:
        context: .
        dockerfile: ./nginx/Dockerfile
      image: nginx
      restart: always
      volumes:
        - ./server/static:/usr/share/nginx/html/static
      ports:
        - 80:80
      depends_on:
        - server
      command: nginx -g 'daemon off';
    server:
      container_name: server
      build:
        context: ./server
        dockerfile: Dockerfile
      hostname: server
      ports:
        - 8000:8000
      volumes:
        - ./server:/src/project
      depends_on:
        - "db"
      restart: on-failure
      env_file: .env
      command: &gt;
        bash -c '
        python manage.py makemigrations &amp;&amp;
        python manage.py migrate &amp;&amp;
        gunicorn project.wsgi -b 0.0.0.0:8000'
    db:
      container_name: postgres
      image: postgres:latest
      hostname: postgres
      ports:
        - 5432:5432
      volumes:
        - /var/lib/postgresql/data
  </code></pre>
  
  <p>The folder <em>./server/static</em> contains all static files assembled trough <code>python manage.py collectstatic</code> and adds them to the volume <em>/usr/share/nginx/html/static</em>. However, when I try to access the static files, f.e. at <em>localhost:8000/admin</em>, I receive warnings for missing css files (base.css, login.css, ..).</p>