Jyoti Prakash Mallick
Jyoti Prakash Mallick

Reputation: 2076

Bind different path to base path through nginx

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

Answers (1)

LinPy
LinPy

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

Related Questions