Reputation: 2076
I have a nodejs express service which is running inside a container. And I have configured nginx on my host machine.
I am able to access my service on both ports and without the port. http://localhost:8091/api/test http://localhost/api/test
Express Code Snippet:
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 Configuration:
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;
}
I want to redirect any request which holds '/api' should redirect to the base path i.e. '/api/test'. Is there any configuration I need to do to the nginx configuration?
Upvotes: 0
Views: 1214
Reputation: 18578
you need to add the follwoing to your server
section in nginx
config:
location /api {
rewrite /api /api/test break;
proxy_pass http://127.0.0.1:8091;
}
Upvotes: 1