title: Using Docker Nginx Reverse Proxy for Docker containers applications
body: <p>I am having difficulty on port forwarding using Nginx reverse proxy (docker container) to an angularjs docker containers. Here is my Dockerfile and Nginx configuration with the commands I am using.</p>
  
  <p>Dockerfile:</p>
  
  <pre><code>FROM nginx:latest
  COPY nginx-reverseproxy.conf /etc/nginx/conf.d/
  RUN mkdir /etc/nginx/ssl
  COPY chained.crt /etc/nginx/ssl
  COPY private.key /etc/nginx/ssl
  RUN chown -R root:root /etc/nginx/ssl
  RUN chmod -R 644 /etc/nginx/ssl
  EXPOSE 80
  EXPOSE 443
  </code></pre>
  
  <p>Nginx Configuration:</p>
  
  <pre><code>server {
      listen         80;
      listen         443 ssl;
      server_name mywebserver.com;
  
      if ($scheme = http) {
      return 301 https://$server_name$request_uri;
      }
  
  ssl on;
  ssl_certificate    /etc/nginx/ssl/chained.crt
  ssl_certificate_key /etc/nginx/ssl/private.key
  
  location / {
        proxy_set_header                Host                    $host:$server_port;
              proxy_set_header                X-Real-IP               $remote_addr;
              proxy_set_header                X-Forwarded-For         $proxy_add_x_forwarded_for;
              proxy_set_header                X-Forwarded-Proto       $scheme;
  
  
               proxy_pass                      http://127.0.0.1:4200;
               proxy_read_timeout              180;
  
               proxy_redirect     off;
               proxy_redirect                  http://127.0.0.1:4200  $scheme://mywebserver.com;
  
  
                proxy_http_version              1.1;
                proxy_request_buffering         off;
   }
  }
  </code></pre>
  
  <p>Command to create Nginx container:</p>
  
  <pre><code>docker run -d -p 80:80 -p 443:443 --name "container-nginx" nginx-reverse-proxy-image:latest
  </code></pre>
  
  <p>Command to create myserver.com container:</p>
  
  <pre><code>docker run -d -p 4200:4200 --name "container-mywebserver" angularjs-image:latest ng serve --host 0.0.0.0 --disable-host-header
  </code></pre>
  
  <p>I am getting "502 Bad Gateway" error message when I browse. But, when I curl localhost:4200, I could see the content. Any help would be appreciated. </p>