2

I have an express application that is working perfectly on my local environment on port 3001. I need to deploy it on a production server running nginx listening on port 80. Below are some of the express routes

router.get('/', (req, res) => {
    //home page, show login page
});


router.post('/login', (req, res) => {
    if(errors){
        //invalid credentials
        return res.redirect('..');
    }

    //successful login, redirect to admin area
    return res.redirect('../admin');
})

and similar routes for authenticated admin users. The routes are not working properly when deployed on nginx using reverse proxy. The nginx reverse proxy settings are as follows:

location /v2manager/ {
                proxy_set_header        Host    $host;
                proxy_pass  http://127.0.0.1:3001/;
        }

I want the application to be accessible on the route v2manager on my production domain like example.com/v2manager. So all the routes become as:

  1. example.com/v2manager/ for the home page
  2. example.com/v2manager/login for login POST route, and
  3. example.com/v2manager/admin for the admin area

However, the redirects are not working properly, on login failure the return res.redirect('..') redirects me to example.com which has a different page whereas it should redirect to example.com/v2manager

I can obviously change my application routes to add v2manager everywhere but the route v2manager can change frequently depending upon our environments and versioning. Also, a solution that would not hamper the local environment so that localhost:3000/login would work always

user3789185
  • 71
  • 1
  • 9

1 Answers1

2

You need set proxy redirect:

location /v2manager/ {
  proxy_set_header Host $host;
  proxy_redirect ~/(.*)$ /v2manager/$1;
  proxy_pass http://127.0.0.1:3001/;
}
stdob--
  • 28,222
  • 5
  • 58
  • 73
  • Hi i having an issue with nginx, so currently when i access my ipaddress i go to the nginx welcome page but when i go to ipaddress/api i get 404 not found – kd12345 Jan 26 '21 at 15:16