Iptables将端口80转发到远程鱿鱼

我有服务器A的IP 192.0.2.2,它没有连接到互联网,所以它必须通过所有互联网HTTPstream量到服务器B上的IP鱿鱼代理与IP地址192.0.2.3监听端口8080。

我使用下面的规则build立到squid代理的连接,但是当我启动一个wget时,我得到了一个400错误的请求错误:

iptables -t nat -A OUTPUT -p tcp --dport 80 -j DNAT --to-destination IP:8080 

wget的:

 wget external.example.net/v4/Picture/pic1.jpg --2015-09-05 18:42:01-- http://external.example.net/v4/Picture/pic1.jpg Resolving external.example.net... 60.192.194.32, 61.54.192.95, 77.36.14.19, ... Connecting to external.example.net|60.192.194.32|:80... connected. HTTP request sent, awaiting response... 400 Bad Request 2015-09-05 18:42:01 ERROR 400: Bad Request. 

当我检查鱿鱼日志,我看到的url从域中丢失。

 1 192.0.2.2 NONE/400 1186 GET /v4/Picture/pic1.jpg - NONE/- - 

如果我使用bash http_proxy而不是iptables规则,我可以成功下载图像和鱿鱼logging完整的url

 export http_proxy=http://192.0.2.3:8080 wget external.example.net/v4/Picture/pic1.jpg 

wget的:

 2015-09-05 18:55:48-- http://external.example.net/v4/Picture/pic1.jpg Connecting to 192.0.2.3:8080... connected. Proxy request sent, awaiting response... 200 OK Length: 83760 (82K) [image/jpeg] Saving to: `pic1.jpg' 

鱿鱼日志

  7 1.2.3.4 TCP_HIT/200 84455 GET http://external.example.net/v4/Picture/pic1.jpg - NONE/- image/jpeg 

为什么我的第一个请求没有工作?

这不是代理如何工作。

当您通过HTTP代理发送请求时,它看起来像这样,其中proxy.example.net是代理,而target.example.net是您尝试获取的站点:

 GET http://target.example.net/path/to/resource HTTP/1.1 Host: proxy.example.net 

这是说明性的,因为您会注意到用于向代理发送HTTP请求的URL是这样的:

 http://proxy.example.net/http://target.example.net/path/to/resource 

当您使用DNAT简单地redirectstream量时,wget将parsingtarget.example.net,并将URI的path部分作为请求正文而不是要代理的URL发送,因此它仅发送以下内容:

 GET /path/to/resource HTTP/1.1 Host: target.example.net 

鱿鱼服务器是可以理解的,因为它不是target.example.net,而不是在/ path / to / resource服务文档。

如果你将squidconfiguration成一个透明的代理服务器,你可以使用下面的指令为2.6版本之前的squid实现这个function:

 httpd_accel_host virtual httpd_accel_port 8080 httpd_accel_with_proxy on httpd_accel_uses_host_header on 

最后一个是httpd_accel_uses_host_header on ,告诉squid使用我上面的示例(需要HTTP 1.1)中的Host:头来找出从哪里获取资源,而不是使用上面提到的显式代理请求。 请参阅http://squidconfiguration.com/config-manual-2-4/httpd-accelerator-options/httpd_accel_uses_host_header/

对于较新版本的squid(3.1及更高版本),使用这个listen指令:

 http_port 8080 intercept 

看到这里: http : //www.squid-cache.org/Doc/config/http_port/

对于一些老版本的鱿鱼来说,它是“透明的”而不是“截取的”。

还有一个来自squid wiki的好教程: http : //wiki.squid-cache.org/ConfigExamples/Intercept/LinuxRedirect