I have two Express powered NodeJS applications that are being served by Nginx. One application is a RESTful API, that is consumed by an Angular SPA and the other one is an Admin portal. This is something I'm trying to achieve here:

location / { # Serves Client Side Angular Application } location /api { # Serves RESTful Application } location /admin { # Serves Admin Portal }

Here is my complete configuration for nginx:

server { server_name localhost; listen 80; listen [::]:80; index index.html; location / { expires -1; add_header Pragma "no-cache"; add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"; root /var/www/example/client/dist; try_files $uri $uri/ /index.html =404; } location /admin/ { proxy_pass http://127.0.0.1:3010/; proxy_http_version 1.1; rewrite /admin/(.*) /$1 break; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host/admin/; proxy_set_header X-NginX-Proxy true; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location /api { proxy_pass http://127.0.0.1:3011; proxy_http_version 1.1; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }

Both of the NodeJS application utilizes forever and run on their defined ports, perfectly.

The problem I'm facing with my admin application, when I try to hit http://example.com/admin it redirects me to http://example.com/login when it should redirect me to http://example.com/admin/login . I tried to solve this issue by adding a middleware to the admin application, like:

app.use('*', function (req, res, next) { if(process.env.target === 'dev'){ if(req.originalUrl.indexOf('/admin') > -1){ res.redirect('/admin' + req.url); } next(); } });

But this doesn't work. How should I handle this situation? Is there anyway nginx can handle application redirects? If not, how should I handle it in application for different environments(Dev, Staging, Prod etc)? I'm using nginx 1.4.6, node 0.12.2 and express 4.12.2. I have no issue if I have to add any 3rd party module in my application but I'd prefer if there is an nginx solution for my problem.

Thanks.