title: Configuring nginx in a single docker container environment (AWS ElasticBeanstalk)
body: <p>I am currently deploying a Django + uWSGI app using a <a href="http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/docker-singlecontainer-deploy.html" rel="noreferrer">single container docker environment</a> in AWS ElasticBeanstalk. This environment already ships with nginx, which I am currently trying to configure.</p>
  
  <p>I am trying to achieve the following:</p>
  
  <ol>
  <li>Terminate HTTPS at the environment's load balancer</li>
  <li>Redirect HTTP requests to HTTPS using nginx (which is shipped with the environment)</li>
  <li>Pass requests from nginx to uwsgi</li>
  </ol>
  
  <p>Environment information: </p>
  
  <ul>
  <li><strong>Configuration and Solution Stack Name</strong>: Single Container Docker 1.11
  version 2.3.0 </li>
  <li><strong>AMI</strong>: 64bit Amazon Linux 2016.09 v2.3.0 running Docker
  1.11.2
  2016.09.0 </li>
  <li><strong>Docker Version</strong>: 1.11.2</li>
  <li><strong>Proxy Server</strong>: nginx 1.10.1</li>
  </ul>
  
  <p>This is my current configuration:</p>
  
  <p><strong>.ebxtensions/00-loadbalancer-terminatehttps.config</strong></p>
  
  <pre><code>option_settings:
    aws:elb:listener:443:
      ListenerEnabled: true
      ListenerProtocol: HTTPS
      SSLCertificateId: &lt;resource-id-here&gt;
      InstancePort: 443
      InstanceProtocol: HTTP
    aws:elb:listener:80:
      ListenerEnabled: true
      ListenerProtocol: HTTP
      InstancePort: 80
      InstanceProtocol: HTTP
  </code></pre>
  
  <p><strong>.ebextensions/01-nginx-proxy.config</strong></p>
  
  <pre><code>files:
    "/etc/nginx/sites-available/test.domain.com.conf":
      mode: "000644"
      owner: root
      group: root
      content: |
        server {
          listen 80;
          server_name test.domain.com;
          access_log /var/log/nginx/$server_name.access.log;
  
          location / {
            return 301 https://$server_name$request_uri;
          }
  
          location = /status/ {
            access_log /var/log/nginx/$server_name.healthd.log healthd;
            include uwsgi_params;
            uwsgi_pass docker;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
          }
  
        }
  
        server {
          listen 443;
          server_name test.domain.com;
          access_log /var/log/nginx/$server_name.access.log;
  
          location / {
            include uwsgi_params;
            uwsgi_pass docker;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            client_max_body_size 100m;
          }
  
          location /static {
              alias /var/www/static;
          }
        }
  
  commands:
     00_enable_site:
      command: 'rm -f /etc/nginx/sites-enabled/* &amp;&amp; ln -s /etc/nginx/sites-available/test.domain.com.conf /etc/nginx/sites-enabled/test.domain.com.conf'
  </code></pre>
  
  <p><strong>.ebextensions/02-healthcheckurl.config</strong></p>
  
  <pre><code>option_settings:
    - namespace:  aws:elasticbeanstalk:application
      option_name:  Application Healthcheck URL
      value:  /status/
  </code></pre>
  
  <p><strong>application.ini (uwsgi config)</strong></p>
  
  <pre><code>[uwsgi]
  master = true
  socket = :3031
  processes = 4
  enable-threads = true
  threads = 2
  chdir = /opt/app/
  wsgi-file = test/wsgi.py
  logto2 = /var/log/uwsgi.log
  callable = application
  py-autoreload = 3
  </code></pre>
  
  <p>Now, when testing the configuration:</p>
  
  <p><strong>Checking <a href="http://test.domain.com/status/" rel="noreferrer">http://test.domain.com/status/</a> works fine</strong></p>
  
  <pre><code>$ wget http://test.domain.com/status/
  --2017-01-14 23:00:18--  http://test.domain.com/status/
  Resolving test.domain.com... 52.xx.xx.xx, 52.xx.xx.xy
  Connecting to test.domain.com|52.xx.xx.xx|:80... connected.
  HTTP request sent, awaiting response... 200 OK
  </code></pre>
  
  <p><strong>Checking <a href="http://test.domain.com/hello/" rel="noreferrer">http://test.domain.com/hello/</a> doesn't work as expected. It redirects fine, but it then hangs until the request times out.</strong></p>
  
  <pre><code>$ wget http://test.domain.com/hello/
  --2017-01-14 22:59:13--  http://test.domain.com/hello/
  Resolving test.domain.com... 52.xx.xx.xx, 52.xx.xx.xy
  Connecting to test.domain.com|52.xx.xx.xx|:80... connected.
  HTTP request sent, awaiting response... 301 Moved Permanently
  Location: https://test.domain.com/hello/ [following]
  --2017-01-14 22:59:15--  https://test.domain.com/hello/
  Connecting to test.domain.com|52.xx.xx.xx|:443... connected.
  HTTP request sent, awaiting response... 408 REQUEST_TIMEOUT
  2017-01-14 23:00:17 ERROR 408: REQUEST_TIMEOUT.
  </code></pre>