title: ERR_CONNECTION_REFUSED Mean app with nginx
body: <p>I have 2 web apps which run on <strong>XX.XX.XX.XX/</strong> and <strong>XX.XX.XX.XX/harvin</strong>, one of them is only node-express and another one is a MEAN stack app respectively. They are configured with <strong>nginx</strong> as below</p>
  
  <h1>nginx.conf</h1>
  
  <pre><code># node-express
  location / {
          proxy_pass http://localhost:3000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
  }
  
  # MEAN app
  location /harvin {
          rewrite ^/harvin/(.*)$ /$1 break;
          proxy_pass http://localhost:8081;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
  }
  </code></pre>
  
  <p>This question is about the <strong>'Harvin' app (MEAN app)</strong>.
  Here is my <strong>app.js</strong> file with <a href="https://github.com/expressjs/cors" rel="nofollow noreferrer">CORS</a> (Cross-Origin Resource Sharing) configuration.</p>
  
  <h1>app.js</h1>
  
  <pre><code>const express = require("express");
  const logger = require("morgan");
  const cors = require("cors");
  var app = express();
  
  var whitelist = ["http://localhost:4200", "XX.XX.XX.XX"];
  
  app.use(
      cors({
          origin(origin, callback) {
              if (whitelist.indexOf(origin) !== -1) {
                  callback(null, true);
              } else {
                  callback(new Error("Not allowed by CORS"));
              }
          },
          methods: ["PUT, GET, POST, DELETE, OPTIONS"],
          allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With"],
          credentials: true
      })
  );
  
  //some middlewares
  
  app.get("/api/login", (req, res) =&gt; {
      console.log("login route");
      // login the user
  });
  
  // Connect server to Angular 2 Index.html
  app.get("/", (req, res) =&gt; {
      res.sendFile(path.join(__dirname + "/public/index.html"));
  });
  
  //some more routes
  
  module.exports = app;
  </code></pre>
  
  <p><br/></p>
  
  <h1>The problem</h1>
  
  <p>The problem I am facing is that when I make any kind of request (like login or signup etc.) on <em>localhost:8081/api/login</em> from the client side of <strong>Harvin (MEAN app)</strong> then the console shows <strong>ERR_CONNECTION_REFUSED</strong>.
  <br/></p>
  
  <p>Even <a href="https://github.com/expressjs/morgan" rel="nofollow noreferrer">morgan</a> is not logging any kind of login request.</p>
  
  <p><br/>
  <strong>Exception</strong>  Homepage of Harvin is loading successfully without any error.</p>
  
  <p><br/></p>
  
  <h1>Expected behavior</h1>
  
  <p>No <strong>ERR_CONNECTION_REFUSED</strong> in the console when any kind of http request is made from the client side. </p>
  
  <h1>P.S.</h1>
  
  <p>This Harvin app is working perfectly on my local machine. </p>