是否可以在端口 9090 上设置一个侦听器并添加一个标头,例如 Host: test.host 到在 9090 上传入的每个请求并将其发送到 8080?
谢谢
编辑:我现在使用反向代理,将主机名:端口应用于传入的任何请求。
Twisted 有一个反向代理的实现,您可以修改它以满足您的需要。您可以在此处查看示例。如果您查看 的源代码twisted.web.proxy
,您可以看到 'Host:' 标头已设置在 中ReverseProxyRequest.process
,因此您可以对其进行子类化并设置您自己的标头。
Unless you need to tailor the proxied request based on parameters that only your web application can know (for example, you need to authenticate the proxied request with your webapp's custom authentication system), you should use your web server's proxy capabilities.
Example with Apache:
Listen 0.0.0.0:9090
ProxyRequests off
<VirtualHost myhost:9090>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ProxyPassReverseCookieDomain localhost myhost
</VirtualHost>
If you have to proxy things in a Flask or Werkzeug application, you can use httplib, creating requests based on the incoming request data and returning the response, either raw or modified (eg for link rewriting). It's doable, I have one such proxy in use where there was no good alternative. If you do that I recommend against using regular expressions to rewrite HTML links. I used PyQuery instead, it's far easier to get it right.