title: AWS Elastic Beanstalk / NodeJS / Nginx Error: Keep getting 502 bad gateway
body: <p>I have deployed my web application for several times, and it went well.</p>
  
  <p>but since yesterday deployment, I'm keep getting 502 Bad Gateway, whenever I try to get my website.</p>
  
  <p>I terminated my app from AWS elastic beanstalk and recreated, but still getting 502 bad gateway.</p>
  
  <p>here's my nodejs.log:</p>
  
  <pre><code>Error: ENOENT: no such file or directory, open '/var/app/current/config/awsconfig.json'
      at Error (native)
      at Object.fs.openSync (fs.js:642:18)
      at Object.fs.readFileSync (fs.js:510:33)
      at Object.readFileSync (/var/app/current/node_modules/aws-sdk/lib/util.js:97:26)
      at Config.loadFromPath (/var/app/current/node_modules/aws-sdk/lib/config.js:409:39)
      at Object.&lt;anonymous&gt; (/var/app/current/server.js:14:12)
      at Module._compile (module.js:577:32)
      at Object.Module._extensions..js (module.js:586:10)
      at Module.load (module.js:494:32)
      at tryModuleLoad (module.js:453:12)
  fs.js:642
    return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
  </code></pre>
  
  <p>and this is the part in my server.js that related to nodejs error log:</p>
  
  <pre><code>const express = require("express");
  const path = require("path");
  const fs = require("fs");
  const template = require("./public/template");
  const bodyParser = require("body-parser");
  const _ = require("lodash");
  const uuidv1 = require("uuid/v1");
  
  const AWS = require("aws-sdk");
  const multer = require("multer");
  const multerS3 = require("multer-s3");
  
  // Image Upload
  AWS.config.loadFromPath(path.join(__dirname, "/config/awsconfig.json"));
  const s3 = new AWS.S3();
  const upload = multer({
    storage: multerS3({
      s3,
      bucket: "hackingdeal",
      key: (req, file, cb) =&gt; {
        cb(null, new Date().valueOf() + path.extname(file.originalname));
      },
      acl: "public-read-write"
    })
  });
  </code></pre>
  
  <p>FYI, "server.js" file is located in root dir("./server.js") and awsconfig.json is located in "./config/awsconfig.json"</p>
  
  <p>I made a .gitignore to ignore "config/awsconfig.json".
  I also created a .ebignore but I didn't contained "config/awsconfig.json" so my elastic beanstalk could find and read "config/awsconfig.json" file.</p>
  
  <p>This is my nginx/error.log:</p>
  
  <pre><code>2018/10/20 23:32:05 [error] 8103#0: *96 connect() failed (111: Connection refused) while connecting to upstream, client: 110.12.50.169, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "hackingdeal-env.4wgwkb9tdv.ap-northeast-2.elasticbeanstalk.com"
  2018/10/20 23:32:05 [error] 8103#0: *96 connect() failed (111: Connection refused) while connecting to upstream, client: 110.12.50.169, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "hackingdeal-env.4wgwkb9tdv.ap-northeast-2.elasticbeanstalk.com", referrer: "http://hackingdeal-env.4wgwkb9tdv.ap-northeast-2.elasticbeanstalk.com/"
  2018/10/20 23:48:00 [error] 12936#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 110.12.50.169, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "hackingdeal.com"
  2018/10/20 23:48:01 [error] 12936#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 110.12.50.169, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "hackingdeal.com", referrer: "http://hackingdeal.com/"
  </code></pre>
  
  <p>hackingdeal.com is my website. I connected that domain to my Elastic Beanstalk by using Route53 service, making alias.</p>
  
  <p>The thing is,
  My app runs successfully on localhost.</p>
  
  <p>My app run successfully right after when I deployed "server.js" with Image upload statements(using AWS S3 and multer/multerS3 thing: See upper code block"</p>
  
  <p>But from yesterday, it keep returns 502 bad gateway.</p>
  
  <p>I'm totally lost, I don't know where to start, or where to find my problem.
  please help..</p>