nginx / njs

A subset of JavaScript language to use in nginx
http://nginx.org/en/docs/njs/
BSD 2-Clause "Simplified" License
1.14k stars 148 forks source link

Port missing in Host header when using ngx.fetch with non-standard port #707

Closed mtyaka closed 5 months ago

mtyaka commented 5 months ago

When using ngx.fetch() with a non-standard port, the Host header of the request contains only the hostname without the port.

The request that hits the server when using the code below contains the Host header with value myhost.com, when it should be myhost.com:8443.

ngx.fetch('https://myhost.com:8433/api')

As a workaround it's possible to manually set the Host header when making a fetch request:

ngx.fetch('https://myhost.com:8433/api', {headers: {'Host': 'myhost.com:8443'}})
xeioex commented 5 months ago

Hi @mtyaka,

Thank you for the report. It will be fixed.

I wander, with what webservers you have issues? Because even though the port is missing in Host header, ngx.fetch() performs a connection to the specified port.

xeioex commented 5 months ago

@mtyaka

Feel free to test the patch.

mtyaka commented 5 months ago

Thank you @xeioex!

I wander, with what webservers you have issues? Because even though the port is missing in Host header, ngx.fetch() performs a connection to the specified port.

I noticed this while connecting to a custom web service that logs the value of the Host header and I noticed inconsistencies between requests made from a browser and those made with ngx.fetch. It wasn't causing any real issues in my case, but it does deviate from the spec.