Search code examples
node.jsexpressmod-proxy

How to get req.hostname when running node.js behind apache with mod_proxy


My node.js application uses req.hostname in order to redirect some pages from HTTP to HTTPS like this:

app.get('*', function(req, res){
    res.redirect('https://' + req.hostname + req.url);
});

This works very well when running the application on localhost. But when the application runs on a server behind mod_proxy then req.hostname does not contain the domain name. Instead it contains just localhost. How do I get the domain name?


Solution

  • on your apache settings you have to add this:

    ProxyPreserveHost On
    

    this will tell the apache to preserve a hostname when doing proxy pass to your node application