node.js - Node Express: res.redirect broken when using proxy -


I have a node Express app running behind the proxy, which is configured in Apache ProxyPass (httpd.conf ):

Uses passports to use passport to authenticate the user, so when I reach the base path, I should be redirected to the login page. :

- Old Redirect: ->

- New Redirect: ->

Unfortunately, the redirect is not working:

- Broken redirect: ->

I tried to enable proxy configuration in Express but it did not matter:

  app.enable (' Trust proxy '); ** Edit: ** As suggested by Ethan in the comment below, I can harden all the paths in accordance with the new environment, although this is the path The machine at the root will break ('/')  

I am looking for a more centralized solution that can be suitable for different runtime environments if possible?

To work in both ways, you need to calculate the proxy path or local path for each request is required.

I am using a Nginx proxy, which is "x-forwarded-for" and "x-forward-prototo" (protocol of oriental request) to see what your proxy might be headers , Dump "Req.headers".

Before creating it in your Express Conference:

  app.use (function (req, res, next) {if (req.headers.hasOwnProperty ('x- Forwarded-for ')) // Proxy effects in req.redirUrl = req.headers [' x-forwarded-proto '] + ": //" + Req.headers.host // proxy // plus any proxy subdivisions If required, "+" / "+ proxy_subdir;} and {// direct requeset req.redirUrl = req.protocol +": // "+ req.headers.host;} forthcoming ();});  

You now have a req.redirUrl, which you can use at any time of the beginning of the redirect, which is a proxy or x Will operate under the C:

  res.redirect (Req.redirUrl + "/ redir_path");  

Comments