Why am I receiving a 404 not found when I try to access a route other than "/"?

Posted March 20, 2020 11.6k views

I am using nodejs.
When I try to access a route other than “/”, I receive 404 not found.

It works

It does not work

nginx/sites-available/ file

server {

        root /var/www/;
        index index.js index.html index.htm index.nginx-debian.html;


        location / {
                try_files $uri $uri/ =404;
                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;


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}server {
    if ($host = {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        listen 80;
        listen [::]:80;

    return 404; # managed by Certbot


index.js file

const express = require("express");
const app = express();
const multer = require("multer");
const path = require("path");

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, "upload/");
    filename: function (req, file, cb) {
        cb(null, file.originalname + + path.extname(file.originalname));
const upload = multer({storage});

app.set("view engine", "ejs");

app.get("/", (req, res) => {

app.get("/rfid", (req, res) => {
});"/upload", upload.single("file"), (req, res) => {
    res.send("Arquivo recebido");

app.get("/face", (req, res) => {
    // res.render("face");

app.listen(3000, () => {
    console.log("servidor rodando");

I so a lot of answers here, but none of them helped me.
Thanks in advance.


edited by MattIPv4

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hi there @claudineigoncalves,

I think that you don’t need the try_files $uri $uri/ =404; part in your Nginx server block.

You can take a look at this tutorial here on how to set up NodeJS with Nginx:

Hope that this helps!

by Brennen Bearnes
Node.js is an open source JavaScript runtime environment for easily building server-side and networking applications. Node.js applications can be run at the command line but this guide focuses on running them as a service using PM2, so that they will automatically restart on reboot or failure, and can safely be used in a production environment.
  • Hi @bobbyiliev

    I have the same issue and I already followed the tutorial to setup nodejs and nginx on ubuntu 20.04. I’ve been looking for solution for over 3 days now and from what I can see I’ve done the right configuration. I’m new to nginx or deploying anything to the cloud.

    • Hi there,

      Can you share your Nginx server block here?

      Also after the changes, you’ve made, did you try restarting Nginx?

      Here is a quick video demo that I recorded a few days ago based on the tutorial above: