1

I have a node app that works locally and uses Express for routing. I have tried to set this up on digital ocean using their guide. I can see the home page of my app when I navigate to /, but I cannot access any inner pages.

To debug I stopped the app running through pm2 and simply ran it by calling 'node app.js' in the terminal so that I can see the console log messages.

I put comments on Express routes for testing:

app.get('/', function (req, res) {
console.log('/ route requested');
if (req.isAuthenticated()) {
  res.render('home',
    {
      user: req.user
    });
} else {
  res.render('home',
    {
      user: null
    });
}
});

app.get('/signup', function (req, res) {
    console.log('/signup route requested');
    res.render('signup');
});

When I request '/' the console log message is printed. However trying to visit '/signup' nothing is shown in the terminal. I am taken to a default 404 page (nginx/1.10.0 (Ubuntu).

I have tried to set up Nginx as a reverse proxy as detailed here. Here is my nginx configuration from etc/nginx/sites-available/default

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

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

        server_name _;

        location / {
proxy_pass http://localhost:2000;
        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;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #

Also the index page isn't loading resources from my public directory, such as styles.

I would appreciate any help trying to get the Express routes and resources to load on the remote server. Please let me know if their is further information that would be helpful for debugging.

Paul Trotter
  • 607
  • 7
  • 14
  • This section looks quite suspect: `# First attempt to serve request as file, then` – Tom Hallam May 15 '17 at 11:24
  • Good point @freshnode. It's not in the digital ocean example, so i'm not quite sure where I got that from :$ I'll try without and report back – Paul Trotter May 15 '17 at 11:27
  • @ freshnode removing the `# First attempt to serve request as file, then` section of the config file did the trick. Thanks. Want to write this up as an answer so I can mark that as correct? – Paul Trotter May 15 '17 at 11:33
  • [Edited] Refer this StackOverflow answer https://stackoverflow.com/a/49964108/4270123 this one solved my issue. – Arun Yokesh Feb 12 '19 at 04:21

2 Answers2

1

Judging from the nginx configuration you posted, it seems that you're using the try_files directive alongside proxy_pass which is probably leading to a race condition. nginx is probably trying to serve your URI as a file, which it almost definitely cannot do.

Removing the following lines from your Location directive should do the trick:

# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
Tom Hallam
  • 1,924
  • 16
  • 24
0

Since you receive a default 404 page from nginx, this probably means your node.js application is not reachable directly, but behind some nginx installation. Is that true? Because in this case, you probably have to set up nginx correctly, and express has nothing to do with it.

Maybe you should try connecting to the nodejs application directly to verify (which is [host]:3000 in most examples).

user826955
  • 3,137
  • 2
  • 30
  • 71
  • Thanks - I can access the app by including the port number. So it seems that I have made some error in setting up nginx as the reverse proxy when following these instructions: 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 I will post the nginx config code shortly – Paul Trotter May 15 '17 at 11:15