通过NGINX将不同的路径与基本路径结合



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

我能够在两个端口和无端口上访问我的服务。http://localhost:8091/api/testhttp://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配置做任何配置?

您需要将follwoing添加到 server config的 CC_1部分:

location /api {
  rewrite /api /api/test break;
  proxy_pass http://127.0.0.1:8091;
}

最新更新