我可以在我的Varnish服务器上使用iptables将HTTPSstream量转发到特定的服务器吗?

我们使用Varnish作为我们的前端Webcaching和负载均衡器,所以我们在我们的开发环境中有一个Linux服务器,在一对Windows 2008 IIS Web服务器上运行Varnish,其中包含一些基本的caching和负载均衡规则。

我们有一个通用的DNS规则,指向这个Varnish框的* .development,所以我们可以浏览http://www.mysite.com.development,http ://www.othersite.com.development等。问题是由于Varnish无法处理HTTPSstream量,因此无法访问https://www.mysite.com.development/

对于开发/testing,我们不需要任何加速或负载平衡 – 我所需要的就是告诉这个盒子作为一个哑代理,并将端口443上的任何传入请求转发到特定的IIS服务器。 我怀疑iptables可能会提供一个解决scheme,但是我写了一个iptables规则已经很久了。 一些最初的黑客已经让我尽可能的

iptables -F iptables -A INPUT -p tcp -m tcp --sport 443 -j ACCEPT iptables -A OUTPUT -p tcp -m tcp --dport 443 -j ACCEPT iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to 10.0.0.241:443 iptables -t nat -A POSTROUTING -p tcp -d 10.0.0.241 --dport 443 -j MASQUERADE iptables -A INPUT -j LOG --log-level 4 --log-prefix 'PreRouting ' iptables -A OUTPUT -j LOG --log-level 4 --log-prefix 'PostRouting ' iptables-save > /etc/iptables.rules 

(其中10.0.0.241是托pipeHTTPS网站的IIS框),但是这似乎不起作用。

澄清 – 我意识到HTTPS代理/caching的安全含义 – 我正在寻找的是完全透明的IPstream量转发。 我不需要解密,caching或检查任何数据包; 我只想让端口443上的任何东西stream过Linux机箱到它后面的IIS框,好像Linux机器甚至不在那里。

任何帮助感激地收到…

编辑:包括完整的iptablesconfiguration脚本​​。

这里是你应该做的redirect在一个特定的端口从一台主机到另一台的stream量,请注意,每个端口443的请求将redirect到你在iptables上指向的主机:

1)打开443端口进行通讯:

 iptables -A INPUT -p tcp -m tcp --sport 443 -j ACCEPT iptables -A OUTPUT -p tcp -m tcp --dport 443 -j ACCEPT 

2)添加特定的规则来redirect传入和传出的数据

 iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to ip.listenig.to:443 iptables -t nat -A POSTROUTING -p tcp -d ip.listening.to --dport 443 -j MASQUERADE 

3)或者,您可以redirect来自特定主机的stream量,如:

  iptables -t nat -A PREROUTING -s ip._not_.listening -p tcp --dport 443 -j DNAT --to-destination ip.listening.to:443 

(这一步特别有用,如果你想在你的networking下的其他客户端处理端口443)

4)通知内核你将接受ip转发

编辑文件/etc/sysctl.conf (或者适合你的发行版的文件)并追加(或者更改)

 net.ipv4.ip_forward=1 

然后发出命令

 sysctl -p /etc/sysctl.conf (or the file that suits your distro) 

我希望它有帮助

好的,下面是完整的解决scheme – 这是12.04 LTS(GNU / Linux 3.2.0-23-generic x86_64)

首先,我必须通过编辑/etc/sysctl.conf启用ip4端口转发并取消注释:

 net.ipv4.ip_forward=1 

然后我必须运行/sbin/sysctl -p才能使此更改生效。

接下来configuration(并捕获) iptables规则脚本:

 # flush any existing rules iptables -F # Configure iptables to allow incoming traffic on port 443 iptables -A INPUT -p tcp -m tcp --sport 443 -j ACCEPT # Configure iptables to allow outgoing traffic on port 443 iptables -A OUTPUT -p tcp -m tcp --dport 443 -j ACCEPT # Configure iptables to NAT incoming 443 traffic to 10.0.0.241:443 iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to 10.0.0.241:443 # Configure iptables to route responses from these requests back to the original requester iptables -t nat -A POSTROUTING -p tcp -d 10.0.0.241 --dport 443 -j MASQUERADE # Dump the ruleset and save it into the file /etc/iptables.rules iptables-save > /etc/iptables.rules 

最后,为了让更改在重新启动时保持不变,我必须编辑/ etc / network / interfaces:

 # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet static address 10.0.0.240 netmask 255.255.255.0 network 10.0.0.0 broadcast 10.0.0.255 gateway 10.0.0.1 # dns-* options are implemented by the resolvconf package, if installed dns-nameservers 192.168.0.11 dns-search spotmain.com # The next line was added to enable iptables rules on system restart pre-up iptables-restore < /etc/iptables.rules