Question

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

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

It works https://www.celr.com.br

It does not work https://www.celr.com.br/rfid

nginx/sites-available/celr.com.br file

server {

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

        server_name celr.com.br www.celr.com.br;

        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/celr.com.br/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/celr.com.br/privkey.pem; # 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 = www.celr.com.br) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = celr.com.br) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;

        server_name celr.com.br www.celr.com.br;
    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 + Date.now() + path.extname(file.originalname));
    }
})
const upload = multer({storage});

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

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

app.get("/rfid", (req, res) => {
    res.render("caminhao");
});

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

app.get("/face", (req, res) => {
    res.send("resposta");
    // 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.

claudinei


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
March 23, 2020
Accepted 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:

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04#set-up-nginx-as-a-reverse-proxy-server

Hope that this helps!

Regards,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.