Search code examples
python-2.7nginxcherrypy

cherrypy.HTTPRedirect redirects to IP instead of hostname using abs path


I'm running CherryPy behind nginx and need to handle redirects. On my dev machine running on 127.0.0.1:8080, this redirects correctly to 127.0.0.1:8080/login.

However when running via nginx on cherrypy.mydomain.com (port 80), the redirects are still going to 127.0.0.1:8080/login rather than cherrypy.mydomain.com/login. 127.0.0.1:8080 is the correct local address for the application, however the application server in nginx is set to listen on port 80 and pipe requests to the local cherrypy server on 127.0.0.1:8080, but shouldn't directly expose this.

The relevant lines in my app are:

auth failure:

raise cherrypy.HTTPRedirect("/login")

and in my controller:

cherrypy.config.update({
    'tools.encode.on': True, 'tools.encode.encoding': 'utf-8',
    'tools.decode.on': True,
    'tools.trailing_slash.on': True,
    'server.socket_host' : '127.0.0.1',
    'server.socket_port' : 8080,
})

I was wondering if there was an additional cherrypy config item for the server/host name in addition to the socket host, but I'm struggling to find it in the Docs.

Essentially, all I need is for cherrypy to redirect to the cherrypy.mydomain.com hostname rather than the internal IP.

Thanks!


Solution

  • Try tools.proxy config setting:

    'tools.proxy.on': True,
    

    Additionally you may need

    'tools.proxy.local': 'X-Forwarded-Host',
    

    Set to appropriate header. When using NGINX, the header would be

    'tools.proxy.local': 'Host',
    

    In case of Lighttpd this header will be appropriate:

    'tools.proxy.local': 'X-Host'