Report this

What is the reason for this report?

How to server second location using nginx server

Posted on May 6, 2018

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?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.