title: 404 while trying to make dockerized nginx serve static files mounted in a volume
body: <p>I'm struggling to setup nginx inside a docker container. I basically have two containers:</p>
  
  <ul>
  <li><p>a php:7-apache container that serves a dynamic website, including its static contents.</p></li>
  <li><p>a nginx container, with a volume mounted inside it as a <strong>/home/www-data/static-content</strong> folder (I do this in my docker-compose.yml), to try to serve a static website (unrelated to the one served by the apache container).</p></li>
  </ul>
  
  <p>I want to use the domain <strong>dynamic.localhost</strong> to serve my dynamic website, and <strong>static.localhost</strong> to serve my static website only made up of static files.</p>
  
  <p>I have the following Dockerfile for my nginx container:</p>
  
  <pre><code>########## BASE #########
  
  FROM nginx:stable
  
  ########## CONFIGURATION ##########
  
  ARG DEBIAN_FRONTEND=noninteractive
  
  ENV user www-data
  
  COPY ./nginx.conf /etc/nginx/nginx.conf
  COPY ./site.conf /etc/nginx/conf.d/default.conf
  
  RUN touch /var/run/nginx.pid &amp;&amp; \
      chown -R ${user}:${user} /var/run/nginx.pid &amp;&amp; \
      chown -R www-data:www-data /var/cache/nginx
  
  RUN chown -R ${user} /home/${user}/ &amp;&amp; \
      chgrp -R ${user} /home/${user}/
  
  USER ${user}
  </code></pre>
  
  <p>As you see I'm using two configuration files for nginx: nginx.conf and site.conf.
  Here is nginx.conf (it's not important because there is nothing special in it but if I'm doing something wrong just let me know):</p>
  
  <pre><code>worker_processes auto;
  error_log /var/log/nginx/error.log debug;
  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;
      sendfile on;
      keepalive_timeout 65;
      include /etc/nginx/conf.d/*.conf;
  }
  </code></pre>
  
  <p>And here is the file site.conf that I have been failing miserably at writing correctly for days now:</p>
  
  <pre><code>server {
      listen 8080;
      server_name static.localhost;
      root /home/www-data/static-content;
  
      location / {
          try_files $uri =404;
      }
  }
  
  server {
      listen 8080;
      server_name dynamic.localhost;
  
      location / {
          proxy_pass http://dynamic;
          proxy_redirect off;
          proxy_set_header Host $host:8080;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Port 8080;
          proxy_set_header X-Forwarded-Host $host:8080;
      }
  }
  </code></pre>
  
  <p>(<a href="http://dynamic" rel="nofollow noreferrer">http://dynamic</a> passes the request to the apache container that I name "dynamic").</p>
  
  <p>So basically I keep getting 404 for whatever file I try to access in my static-content directory. E.g.:</p>
  
  <ul>
  <li>static.localhost:8080/index.html should serve /home/www-data/static-content/index.html but I get 404 instead.</li>
  <li>static.localhost:8080/css/style.css should serve /home/www-data/static-content/css/style.css but I get 404 too.</li>
  </ul>
  
  <p>I tried various things, like writing <em>try_files /home/www-data/static-content/$uri</em>, but I didn't get any result. I read some parts of nginx documentation and searched on Stack Overflow but nothing that I found helped me. If I made a stupid mistake I apologize, but the only thing that I care about now is to get this to work, and to understand what I'm doing wrong.</p>
  
  <p>Thanks</p>