-1

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.

  • 1
    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. – symcbean Aug 22 '23 at 23:27
  • 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. – Alonso Espinosa de los Leal Aug 23 '23 at 15:17

0 Answers0