Score:-1

Can't go to routes using nginx and Express.JS

lt flag

So I have express.js set up behind nginx and when I go to website.com/users, I get a 404. But when I go to website.com, the page loads fine. This appears to be all other routes too, I can't get to website.com/public/css either. Here's my nginx config.

The weird part is on localhost it works perfect, also on a Lambda on AWS. Only it fails on DigitalOcean Droptlet (Ubuntu 20)

I have the express running on port 3001 using PM2

This is my nginx config file:

# Default server configuration
#
server {
    listen 80;
    listen [::]:80;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name api.example.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;

            proxy_http_version 1.1;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://localhost:3001;
    }


}

SOLUTION: My problem was I needed to add the next line to both of my nginx config files error_page 404 / that would tell NGINX to use the routes from the app and not from nginx itself. So I have 1 webapp on port 80 and an express app running on port 81. I apply the code for each file inside /etc/nginx/sites-available and now the API and the webapp can use routing from the URL.

ws flag
The config you have shown us here passes **EVERYTHING** to the server at 3001. That's where you should start looking for your issue unless you have EVIDENCE that this is working as expected.
Alonso Espinosa de los Leal avatar
lt flag
Thank you. In case anyone is wondering. My problem was I needed to add the next line to both of my nginx config files `error_page 404 /` that would tell NGINX to use the routes from the app and not from nginx itself.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.