2

I have a basic http server that runs on a server where multiple domains point to. I need to find the host of the request (the domain where the request is coming from).

 require("http").createServer(function (req, res) {
     console.log(req.headers.host);                
     res.end("Hello World!");                      
 }).listen(9000);                                  

req.headers.host's value is 127.0.0.1:9000 instead of the domain name (example.com or so).

How can I get the domain name from request object?

The node server is proxied via nginx. The configuration is like this:

server {
   listen 80;
   server_name ~.*;
   location / {
       proxy_pass http://127.0.0.1:9000;
   }
}
3
  • How is the node server proxied? nginx? Commented Sep 21, 2014 at 7:45
  • @JoachimIsaksson Exactly. Commented Sep 21, 2014 at 7:46
  • 1
    @JoachimIsaksson Don't forget to add an answer then. The issue is solved. :-) Commented Sep 21, 2014 at 7:49

1 Answer 1

3

The issue is that proxy_pass in nginx rewrites the host header to whatever host is referenced in your rewrite. If you want to override that behavior, you can manually override the host header of the outgoing - proxied - request using proxy_set_header;

server {
   listen 80;
   server_name ~.*;
   location / {
       proxy_pass http://127.0.0.1:9000;
       proxy_set_header Host $http_host;
   }
}

A somewhat more detailed explanation is available here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.