Question

How to server second location using nginx server

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?

Show comments

Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.