不能在Express.JS中使用nginx中的路由

时间:2015-01-19 04:37:44

标签: javascript node.js express nginx

所以我在Express.JS面前设置了nginx,一切都很好,问题是说我去了website.com/users,我最终获得了404 Not Found。显然去website.com很好,但我猜它不是通过路线。这是我的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;
    }
}

我的用户路线

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;

1 个答案:

答案 0 :(得分:1)

您需要将路线更改为:

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

您可能还需要包含Express服务器的端口(默认为3000):

proxy_pass http://default:3000;