dynamic后端与HAProxy

我试图找出处理HTTP请求的dynamic代理的最好方法。

基本上我想以myname .cust.mydomain.com的forms取一个dynamic的主机名,然后把请求转发到一个HTTP主机名为“myname”的HTTP后端服务器。

我一直在试图找出解决这个问题的最好方法,如果HAProxy甚至可以完成任务。

我在想的另一个select就像Lighttpd与LUA甚至Nginx。 任何build议将不胜感激。 谢谢!

最简单的解决scheme就是使用DNS将foo.cust.mydomain.com映射到特定的服务器IP,如womble所示。 这将跳过整个代理服务器。 也许这对你来说是不可能的,例如,如果你没有后端服务器的公有IP地址。

将所有请求引导到一个服务器(使用通配符DNS),然后根据Host头dynamic转发请求会有点复杂,看起来HAProxy不能这样做,因为每个后端服务器都必须在HAProxyconfiguration中明确定义。

Nginx虽然不同,但有了正确的configuration,Nginx可以使用Host头来select后端。 当然,Nginx需要一个将名字映射到后端地址的DNS服务器。

这是一个configuration的小例子:

 server { listen 80 default; location / { # You might need to send some headers to the backend server proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; resolver 192.168.1.1; # DNS server IP # Forward all requests to the backend server using $http_host # (this is the 'Host:' header value) proxy_pass http://$http_host$request_uri; } } 

这将http://myserver.cust.mydomain.com/foo/redirect到http://myserver.cust.mydomain.com/foo/ 。 一见到似乎不是很有帮助。 但是如果你build立一个专用的DNS服务器,将这些名称映射到后端服务器地址,这个请求实际上被转发到一个专用地址上的正确的后端服务器。

但是,这种DNS服务器设置可能不是所希望的,在某些情况下可能会导致问题。 所以,对于Nginxconfiguration的一些补充,我们可以采取另一种方法:

 location / { # headers... resolver 192.168.1.1; # A regex to get the first part (hostname) from the Host header if ($http_host ~* "([a-z0-9-]+)(\.[a-z0-9-]+)*") { # Save a captured part from the regex to a variable set $redirect_hostname $1; # Pass the request to a desired backend proxy_pass http://$redirect_hostname.private.mydomain.com$request_uri; } } 

现在,redirect从http://myserver.cust.mydomain.com/foo/转到http://myserver.private.mydomain.com/foo/ 。 DNS服务器可以保存不同域下的私有地址,并且可以修改proxy_pass指令以匹配所需的名称服务器configuration。

我仍然认为这种代理可能不是解决整个问题的最简单的方法,但毕竟是一种可能。 如果这有什么帮助,我很高兴。

参考资料: Nginx的Wiki ,特别是HttpProxyModule和HttpRewriteModule