[Twisted-web] internal vs. external hostname in Request.getHost9)

Glyph Lefkowitz glyph at twistedmatrix.com
Tue Mar 14 01:10:30 MDT 2017


> On Mar 13, 2017, at 11:01 PM, Ilya Skriblovsky <ilyaskriblovsky at gmail.com> wrote:
> 
> Hi,
> 
> I'm using Twisted Web server behind Nginx reverse-proxy and I'm getting backend's internal host:port from Request.getHost().
> 
> Seems like Request.host is explicitly set to socket's address (i.e. internal address) here: ​https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http.py#L838 <https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http.py#L838> But comment at ​https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http.py#L1297 <https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http.py#L1297> and what this method does points that Request.host meant to reflect Host header of the request, i.e. user-visible hostname and port.
> 
> This creates problems for me when using Klein because it correctly uses Request.getHost() to create host part of URLs for redirects.
> 
> It seems like inconsistency in Twisted code. I'd expect Request.host should be only set from the Host request header to reflect user-visible hostname, not the internal backend server's address. Or may be I'm missing something?


You're absolutely correct!  I even filed a ticket for this functionality, 5 years ago: https://twistedmatrix.com/trac/ticket/5807  There's even a branch for it.  Oddly enough we do have a private _XForwardedForRequest, but... it's only used for logging, for some reason.

If you want accurate access logging and request information, https://twistedmatrix.com/trac/ticket/7704 will probably also be of interest to you.

I'm so sorry you've hit this glaring deficiency in Twisted.

On the other hand: I'm so glad that you've hit this glaring deficiency in Twisted!  I hope you will be motivated to fix it :-).  It's bothered me for quite some time that we don't play nicely with proxying setups, when such setups are so incredibly common.  If you can write pull requests to fix these issues and put them into review, I'm pretty sure you will find an enthusiastic reviewer quickly.

-glyph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-web/attachments/20170314/2818e581/attachment.html>


More information about the Twisted-web mailing list