简体   繁体   中英

Why does NodeJs req.get('host') result to localhost:3000 on digital ocean instead of my actual domain name

I have a droplet on digital ocean. Everything works fine and is configured properly. However, I have nodeJs running on this droplet. This code req.get('host') outputs localhost:3000 instead of my domain name. Please how do i fix this, what file do i need to make this change.

I found it in the docs of expressJs. I observed digital ocean is proxying my requests, therefore adding the hostname in x-forwarded-host header property.

http://expressjs.com/en/5x/api.html#req.host

When the trust proxy setting does not evaluate to false, this property will instead get the value from the X-Forwarded-Host header field. This header can be set by the client or by the proxy.

If there is more than one X-Forwarded-Host header in the request, the value of the first header is used. This includes a single header with comma-separated values, in which the first value is used.

I think it's related to your dns settings in digital ocean panel they explain it here

WILL DIRECT TO, which can be set to: 
A DigitalOcean Droplet or Load Balancer by typing its name and selecting it from the menu.

Its possible that you use WILL DIRECT TO option and because it's redirected the hostname is localhost

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM