通过Nginx将其他路径绑定到基本路径

时间:2019-06-28 06:29:09

标签: docker http nginx

我有一个在容器内运行的nodejs Express服务。并且我已经在主机上配置了nginx。

我可以在两个端口上访问我的服务,而无需该端口。 http://localhost:8091/api/test http://localhost/api/test

快速代码段:

const express = require('express');
const app = express();

app.get('/api/test', (req, res) => {
res.send('Hello from App Engine!');
});

const PORT = process.env.PORT || 8091;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);
});

Nginx配置:

server_name _;
location / {
        proxy_pass http://127.0.0.1:8091;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

我要重定向任何包含'/ api'的请求,都应重定向到基本路径,即'/ api / test'。我需要对nginx配置进行任何配置吗?

1 个答案:

答案 0 :(得分:0)

您需要将以下内容添加到server配置中的nginx部分:

location /api {
  rewrite /api /api/test break;
  proxy_pass http://127.0.0.1:8091;
}
相关问题