title: 502 bad gateway Nginx Node on AWS ElasticBeanstalk
body: <p>I've read a lot of posts about fixing this, but so far nothing has worked, i'm out of ideas.</p>
  
  <p>I have a Express Generator app, runs fine locally, doesn't run once on AWS Elastic Beanstalk site.</p>
  
  <p>The EB runs as single instance, and the security group allows PORT 80 tcp from 0.0.0.0/0, and the rest is all default EB config (ip tables, nginx conf etc)</p>
  
  <p>This is where i've got to...</p>
  
  <p>1) Set node command to <code>npm start</code> to ensure it runs instead of <code>app.js</code><br>
  2) updated the EC2 env <code>PATH</code> to add current <code>node</code>/<code>npm</code> to it<br>
  3) Ensured node port is running as 8081<br>
  4) checked node is actually running ok (on the box I can CURL both by IP and elastic beanstalk ULR successfully)  </p>
  
  <p>But from outside the box, the elastic beanstalk url returns 502 with this error..</p>
  
  <pre><code>2015/07/07 21:54:59 [error] 7883#0: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 94.174.20.81, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "ae-prod.elasticbeanstalk.com", referrer: "http://ae-prod.elasticbeanstalk.com/"
  </code></pre>
  
  <p>Nginx config</p>
  
  <pre><code>upstream nodejs {
      server 127.0.0.1:8081;
      keepalive 256;
  }
  
  server {
      listen 8080;
  
      location / {
          proxy_pass  http://nodejs;
          proxy_set_header   Connection "";
          proxy_http_version 1.1;
          proxy_set_header        Host            $host;
          proxy_set_header        X-Real-IP       $remote_addr;
          proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      }
  
  
  }
  </code></pre>
  
  <p>The IP tables on the box look like this..  </p>
  
  <pre><code>$sudo iptables -t nat -L
  
  Chain PREROUTING (policy ACCEPT)
  target     prot opt source               destination
  REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:http redir ports 8080
  
  Chain INPUT (policy ACCEPT)
  target     prot opt source               destination
  
  Chain OUTPUT (policy ACCEPT)
  target     prot opt source               destination
  REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:http redir ports 8080
  
  Chain POSTROUTING (policy ACCEPT)
  target     prot opt source               destination
  </code></pre>
  
  <p>Any one got any ideas?</p>