user3285218
user3285218

Reputation: 35

Can't use routes in Express.JS with nginx

So I set up nginx in front of Express.JS and all is good, the issue is that say I go to website.com/users, I end up getting a 404 Not Found. Apparently going to website.com is fine, but I guess It's not passing through to routes. Here's my config file for nginx

upstream default {
    server 127.0.0.1:3000;
    keepalive 8;
}
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name website.com default;
    access_log /var/log/nginx/default.log;

    return 301 https://website.com$request_uri;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://default/;
        proxy_redirect off;
    }
}

My users route

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;

Upvotes: 0

Views: 1122

Answers (1)

Brian Lewis
Brian Lewis

Reputation: 5729

You need to change your route to look like this:

/* GET users listing. */
router.get('/users', function(req, res, next) {
  res.send('respond with a resource');
});

You might also need to include the port for the express server (3000 is default):

proxy_pass http://default:3000;

Upvotes: 1

Related Questions