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

微笑、不失礼 提交于 2019-11-30 00:02:48

问题


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!


回答1:


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'



回答2:


I couldn't add a comment to the https://stackoverflow.com/a/20730038/1115187 , but I know, that Lighttpd sends X-Host header, so for Lighttpd proxy use:

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


来源:https://stackoverflow.com/questions/20729678/cherrypy-httpredirect-redirects-to-ip-instead-of-hostname-using-abs-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!