Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup routes with Express and NGINX?

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;
    }
}
like image 320
Sai Datta Avatar asked Jun 21 '17 06:06

Sai Datta


People also ask

Can I use Express with nginx?

Express runs only in node. js. Usually, when you combine nginx with Express, you direct the incoming requests to nginx. It then applies whatever you want nginx to do and then forwards remaining requests to your express port (which, if on the same host, is a different port from what the user connected to).

How do you deploy a react app with Express and nginx?

Firstly, let's create a sample React App using create-react-app boilerplate. The command npx create-react-app my-app-nginx will create a basic app for that. If you want to see the app running, run npm start . The app will show the default view of the Create React App boilerplate.

Do I need nginx with Express?

You will need Nginx (or Apache) on any scenario. With one server or multiple. Using Express or not. Express is only an application framework to build routes.

Is nginx same as Express?

Express is a minimal and flexible node. js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications. On the other hand, nginx is detailed as "A high performance free open source web server powering busiest sites on the Internet".


1 Answers

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.

like image 103
Oleg Avatar answered Sep 20 '22 14:09

Oleg