如何为Node.js REST API正确配置Nginx?

时间:2017-05-03 09:18:18

标签: javascript node.js rest api nginx

我有一个节点应用程序在Apache和Nginx作为反向代理的服务上运行。

在同一台服务器上,Node REST API也在运行。

JavaScript代码如下所示:

api.js

// Express
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

// Express App
const app = express();

// Env
const PORT     = process.env.PORT || 3000;
const NODE_ENV = process.env.NODE_ENV || 'development';

// Config
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());

// Routes
const users = require('./routes/users');

// Angular Http content type for POST etc defaults to text/plain at
app.use(bodyParser.text(), function ngHttpFix(req, res, next) {
  try {
    req.body = JSON.parse(req.body);
    next();
  } catch(e) {
    next();
  }
});

app.use('/api', users);

app.listen(PORT, function() {
   console.log('Listen on http://localhost:' + PORT + ' in ' + NODE_ENV);
});

/routes/users.js

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

// get all users
router.get('/users', function(req, res) {
  models.Beekeeper.findAll({}).then(function(users) {
    res.json(users);
  });
});

module.exports = router;

Nginx配置如下:

index   index.html index.htm;

upstream api {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443;
    root /var/www;

    ssl on;
    ssl_prefer_server_ciphers On;
    ssl_protocols TLSv1.2;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;

    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;

    ssl_dhparam /etc/nginx/ssl/dhparam.pem;
    ssl_certificate /etc/letsencrypt/live/example.com/cert.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    server_name example.com;

    location / {
            proxy_pass http://127.0.0.1:8000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;

    }

    location /api {
            proxy_pass http://api;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            rewrite ^/api/?(.*) /$1 break;
            proxy_redirect off;
    }
}

问题是如果我在我的开发服务器上进行API调用,例如localhost:3000 / api / users,它会按预期工作。

但是,如果我在我的生产服务器上进行API调用,例如https://example.com/api/users,我会无法获取/用户 404 NOT FOUND ,分别

我认为我的Nginx配置有问题,但是,虽然我已经在Stackoverflow上阅读了很多关于类似问题的帖子,但我无法解决问题。

1 个答案:

答案 0 :(得分:4)

请注意,您要求:

https://example.com/api/users

但错误说明了这一点:

  

无法获取 / users

因此,在传递给您的节点服务器之前,/api前缀将从请求路径中删除。

这是由这一行完成的:

rewrite ^/api/?(.*) /$1 break;

解决方案:删除该行。