How to server second location using nginx server

May 6, 2018 677 views
Nginx Node.js Ubuntu 16.04

Hi there,

I was following the tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

Where they set up Nginx as a reverse Proxy server. I tried serving a second simple node application on port 7474.

test.js

var express = require('express');
var app = express();

app.get('/', function(req, res) {
    res.send('Hello There!')
})
app.listen(7474, function() {
    console.log('listening on port 7474')
})

I followed the tutorial and added a second location to my /etc/nginx/sites-available/default File

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

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name _;

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

    location /neo4j {
        proxy_pass http://localhost:7474;
        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;
    }
}

When accessing my server under http://123.45.6.789/neo4j results in a 404 error.

What am I doing wrong?

1 comment
  • EDIT: Title should say of course: How to serve a second application when using neo4j as proxy server

1 Answer

I think this may be an easy fix. You can learn about how Nginx parses your configuration here.

Nginx will serve the first match it encounters. Since /neojs matches the / location block (it is located under this directory) this location block is used to handle the request.

If you place your /neojs location block above your / location block, requests for locations in /neojs will now match the correct location block and all other requests would filter down to the other block.

by Justin Ellingwood
Nginx is one of the most popular web servers in the world. In this guide, we will discuss how Nginx selects the server and location block that will handle a given client's request. We will go over the algorithm in place, as well as the directives and options you can use to modify the selection process.
Have another answer? Share your knowledge.