title: Nginx and Express to serve both static files and node app
body: <p>i have my nodejs app at <code>/var/www/html/admin</code> and my static website at <code>/var/www/html/index.html</code></p>
  
  <p>Inside /admin i have two folders /public - angular front end app and /server the express app for my API.</p>
  
  <p>the server.js file looks like this:</p>
  
  <pre><code>const express = require("express");
  const MongoClient = require("mongodb").MongoClient;
  var morgan = require("morgan"); // used to see requests
  const bodyParser = require("body-parser");
  var mongoose = require("mongoose");
  var config = require("./config");
  var path = require("path");
  const app = express();
  
  const port = 3000;
  
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(bodyParser.json());
  
  app.use(function(req, res, next) {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Methods", "GET, POST");
    res.setHeader(
      "Access-Control-Allow-Headers",
      "X-Requested-With,content-type, Authorization"
    );
    next();
  });
  
  // log all requests to the console
  app.use(morgan("dev"));
  
  // connect to our database (hosted on modulus.io)
  mongoose.connect(config.database);
  
  var apiRoutes = require("./routes/routes")(app, express);
  app.use("/api", apiRoutes);
  
  // set static files location
  // used for requests that our frontend will make
  app.use(express.static('public'));
  
  // MAIN CATCHALL ROUTE ---------------
  // SEND USERS TO FRONTEND ------------
  // has to be registered after API ROUTES
  /*app.get("*", function(req, res) {
    res.sendFile(path.join(__dirname + "./../public/index.html"));
  });*/
  
  app.listen(port, () =&gt; {
    console.log("We are live on " + port);
  });
  </code></pre>
  
  <p>no i have the nginx conf file that looks like this:</p>
  
  <pre><code>server {
          listen 80 default_server;
          listen [::]:80 default_server;
  
          # SSL configuration
          #
           listen 443 ssl default_server;
          #ssl on;
          ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
          ssl_certificate_key /etc/nginx/ssl/server.key;
          ssl_prefer_server_ciphers on;
          # listen [::]:443 ssl default_server
          # Add index.php to the list if you are using PHP
          index index.html index.htm index.nginx-debian.html;
  
          server_name example.com;
  
          location / {
                  # First attempt to serve request as file, then
                  # as directory, then fall back to displaying a 404.
                  root /var/www/html;
                  try_files $uri $uri.html $uri/index.html;
  
  
  
          }
  
  location  /admin/ {
                  proxy_pass http://localhost:3000/;
                  rewrite ^/admin /$1 break;
                  try_files $uri $uri/ =404;
      }
  }
  </code></pre>
  
  <p>I'm not sure how to setup those two in order to have my static public website at www.example.com and have my nodejs app running at www.example.com/admin .</p>