title: Deploying Next.js w/Express Application to Elastic Beanstalk (502)
body: <p>I’m trying to deploy a next.js application using express to electric beanstalk and I’m getting constant 502 Bad Gateway errors.</p>
  
  <blockquote>
    <p>2020/03/02 15:26:28 [error] 8286#0: *172 connect() failed (111:
    Connection refused) while connecting to upstream, client:
    109.190.231.251, server: , request: "GET /db/websql/index.php?lang=en HTTP/1.1", upstream:
    "<a href="http://127.0.0.1:8081/db/websql/index.php?lang=en" rel="nofollow noreferrer">http://127.0.0.1:8081/db/websql/index.php?lang=en</a>", host:
    "54.244.187.52"</p>
  </blockquote>
  
  <p>I’ve read that this could be a port issue (:8081) so I have configured a PORT env variable in the software configuration.  This isn’t fixing the issue.  And my port is set as follows in my express server:  </p>
  
  <pre><code>const PORT = process.env.PORT || 8081;
  </code></pre>
  
  <p>I’m using the </p>
  
  <blockquote>
    <p>us-west-2</p>
  </blockquote>
  
  <p>region.</p>
  
  <p>I have an EB config file in the .ebextensions directory which reads 'npm start' and that looks like the following in my package.json  </p>
  
  <pre><code>"start": "NODE_ENV=production next build &amp;&amp; NODE_ENV=production node ./server.js",
  </code></pre>
  
  <p>Everything runs fine locally in both dev and prod modes but I can’t seem to get it running on EB.  </p>
  
  <p>Any help would be very much appreciated!</p>