1

I cant seem to figure out what I am doing wrong with this simple bit of setup so I setup a node app (super simple to test things out) which looks like this:

// import express
const express = require('express');

// create new express app and assign it to `app` constant
const app = express();

// server port configuration
const PORT = 3000;

// create a route for the app
app.get('/', (req, res) => {
  res.send('Hello this seems to work!');
});

// server starts listening the `PORT`
app.listen(PORT, () => {
  console.log(`Server running at: http://localhost:${PORT}/`);
});

and my nginx server config looks something like this

server {
    listen 80;
    server_name  www.example.org;

    #add in ssh details here later

    #These lines create a bypass for certain pathnames
    #www.example.com/test.js is now routed to port 3000
    #instead of port 80
    location ~* \.(js)$ {
        proxy_pass http://localhost:3000;
        # proxy_set_header Host $host;
    }
}

now according to me when I run this - nginx should redirect to express always yes? but I keep seeing the welcome to nginx page when I go to the website - not "Hello this seems to work!"

I was following this page since it had a similar case of static files and such as me Express + Nginx. Can't serve static files

Indrajeet Haldar
  • 163
  • 1
  • 13

1 Answers1

0

When you look at the config of nginx

 location ~* \.(js)$ {
        proxy_pass http://localhost:3000;
        # proxy_set_header Host $host;
    }

Nginx only forward requests ending by ".js" to your express application. The request to access the main page at http://localhost has not been treated by this block, and you have the nginx default page (I think there are a block to serve static file somewhere in your config)

If you want to forward all the request to Express application, you can try :

 location / {
        proxy_pass http://localhost:3000;
        # proxy_set_header Host $host;
    }

You can find how regex works in Nginx in this link

Đăng Khoa Đinh
  • 5,038
  • 3
  • 15
  • 33