9

I'm trying to configure an Express server with NGINX as a reverse proxy. NGINX to serve static files, and Express for the dynamic content.

Problem : The normal root link works (website.com) , but when I navigate to (website.com/api), I get a 404 from NGINX


This is my server.js :

var express = require("express");
var app = express();
var server = app.listen(process.env.PORT || 5000);

console.log("Server Running");

app.get("/",function(req,res){res.send("HOME PAGE")});

app.get("/api", function(req, res) {
    res.send('API PAGE');
});

This is my NGINX Config file:

server {
    listen 80 default_server;
    listen [::]:80 default_server;


    server_name website.com www.website.com;

    location ~ ^/(assets/|images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
    root /home/foobar/public; #this is where my static files reside
    access_log off;
    expires 24h;
    }

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
        proxy_pass http://localhost:5000;
        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;

        try_files $uri $uri/ =404;
    }
}
Sai Datta
  • 895
  • 1
  • 9
  • 25
  • Hi can you please help me out, i am trying to load my / route as my home page but nginx keeps displaying its own home page. also, when i try to access the route /api i keep getting nginx error. What is the point of this `proxy_pass http://localhost:5000;` instead of indicating your public ipv4 address – kd12345 Jan 26 '21 at 14:14
  • @kd12345 your node js server should be running on `localhost:5000` so when nginx recieves a request, it forwards it to `localhost:5000` – Sai Datta Jan 27 '21 at 08:55
  • My node server shouldnt be running on port 80 or 443? and why do i not use my public ip address here? – kd12345 Jan 27 '21 at 09:00
  • @kd12345 nginx already handles port 80,433 for you depending on your config, your server's IP access is directly nginx, the requests pass through nginx and go to your node server. so run your node on 5000 and use nginx to pass requests to node – Sai Datta Jan 27 '21 at 09:26
  • After doing so even if my local computer is switched off will i still be able to access the public ip from anywhere else? i have the exact same code you have but except this `location ~ ^/(assets/|images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) { root /home/foobar/public; #this is where my static files reside access_log off; expires 24h; }` and instead of `location / {` this `location /api {` – kd12345 Jan 27 '21 at 09:31

2 Answers2

26

Try to remove this line:

try_files $uri $uri/ =404;

With this directive nginx tries to serve a static file (or directory), and returns 404 if there is no such file.

Oleg
  • 22,300
  • 9
  • 68
  • 84
  • 1
    Saved my day. Thanks a lot – Rahul Mar 14 '19 at 16:42
  • 1
    Oh dear... I have followed the instruction of how to set up ngnix + node.js from a hosting company. I have stuck for days with 404, when trying to router other paths. Thank for the answer! – Qin Wang Jun 12 '19 at 10:25
  • Thank you so much for this! You just ended my 3 days of debugging because of this silly line. Also note for others, it may be important to have different location settings with different requirements just in case other places do need try_files like I did. – PepperAddict Jul 23 '20 at 21:09
  • Hi can you please help me out, i am trying to load my / route as my home page but nginx keeps displaying its own home page. also, when i try to access the route /api i keep getting nginx error. What is the point of this `proxy_pass http://localhost:5000;` instead of indicating your public ipv4 address – kd12345 Jan 26 '21 at 14:15
0

In my case, the location property inside server is this:

location {
    proxy_pass http://localhost:3000;
    rewrite ^/(.*)$ /$1 break; # --> this helps
}
Carlos Vallejo
  • 3,290
  • 3
  • 9
  • 13