Search code examples
node.jsexpressnginxreverse-proxypm2

How can I fix this 404 issue that I am having whenever Ubuntu is accessed from an external port. It could be an Ubuntu, pm2, nginx, or express problem


I am getting a web page titled "404 - Not Found" whenever I make a request to my Ubuntu server. So whenever I type my IP address into chrome it gives me the 404.

Initially I thought it was an nginx problem because I didn't think the reverse proxy was working. I didn't think it was working because when I hit my domain wwww.mydomain.com it would give me the same 404 page I have been getting.

Also I could take www.mydomain.com:4000 and it would load the website. Currently I can still do this also I can go to my ip address 222.222.222.222:4000 to work. Anything hitting 222.222.222.222 won't work and will show the 404 page (default port 80).

I tried looking up many different solutions but couldn't figure out how to solve it. I finally resorted to a nuke of my Ubuntu server and to reset everything. When I did this I still got that same 404 page after. Also the 404 page is making me question even if it is an nginx problem, it doesn't have nginx on it. All the page says is 404 - Not Found

The 404 page I am getting when making a request to my IP address

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>404 - Not Found</title>
</head>

<body>
    <h1>404 - Not Found</h1>
</body>

</html>

My nginx.conf file, I commented out the /etc/nginx/conf.d/*.conf to test and see if I could just my stuff in this file to solve the problem. I still get the same 404 page though when going to my domain in chrome. Another reason I am starting to think something else may be the problem.

user  nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    #include /etc/nginx/conf.d/*.conf;

    server {
        listen 80;
        server_name mydomain.com www.mydomain.com;
        location / {
            proxy_pass http://127.0.0.1:4001;
        }
    }
}

The config that I had in the mydomain.conf file for nginx

server {

  listen 80;

  server_name  mydomain.com www.mydomain.com;

  location / {
    proxy_pass http://127.0.0.1:4000;
    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;
  }
}

I have turned off nginx and went to the IP address in chrome and still got the same 404 page. I feel like this means it would have to be a problem with something that isn't nginx. The only other things I use are pm2 and nodejs using express. Could it be a problem with my Ubuntu server? I did port forward for the server too with no luck.

I have run the command sudo nc -l 80 and ran across something odd. When I go to the IP address in chrome (not the real one) 222.222.222.222, I don't get any response from that; however, when I hit 192.168.1.175 I do get a response from the command.

UFW currently

22/tcp                     ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
4001                       ALLOW       Anywhere
4000                       ALLOW       Anywhere
80                         ALLOW       Anywhere
402                        DENY        Anywhere
4002                       ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)
4001 (v6)                  ALLOW       Anywhere (v6)
4000 (v6)                  ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
402 (v6)                   DENY        Anywhere (v6)
4002 (v6)                  ALLOW       Anywhere (v6)

Update

When I go to the domain as any other port than 80, so in my case 443 for https it works. I presume every port works but port 80 for some reason.


Solution

  • OMG WTH!

    Looks like when I turned the server off I could still get the 404 on port 80. So I was like what the heck is this hitting then. I decided to reboot my router and like magic everything worked. Somehow 222.222.222.222 was hitting the router and not the server. This is despite the port 80 being forwarded to the server on my router. Not exactly sure how the router decided not to forward it and try and handle it itself.