twisted / mantissa

Divmod Mantissa is an application server with a web interface built using Axiom and Nevow.
MIT License
5 stars 10 forks source link

Add reverse proxy mode #65

Open mithrandi opened 7 years ago

mithrandi commented 7 years ago

This probably shouldn't be active all the time, but it would be something to activate when running behind a reverse proxy, in order to set the request protocol / host / port correctly based on X-Forwarded-Proto and Host so that URL generation happens correctly.

We could have a flag on SiteConfiguration that activates this, along with code in the site that calls setHost based on the incoming request headers.

mithrandi commented 7 years ago

PoC that I'm monkey-patching in at the moment:

    def process(self, *a, **kw):
        """
        Override the host if we get proxy info.
        """
        proto = self.requestHeaders.getRawHeaders(b'x-forwarded-proto')
        if proto is not None:
            proto = proto[0]
            parts = (
                self.requestHeaders.getRawHeaders(b'host')[0]
                .split(b':', 1))
            host = parts[0]
            if len(parts) == 1:
                port = 443 if proto == b'https' else 80
            else:
                port = int(parts[1])
            self.setHost(host, port, ssl=proto == b'https')
        return self.store.transact(NevowRequest.process, self, *a, **kw)