我如何强制Apache在本地主机上使用http_proxy进行没有iptables的出站stream量?

我有一个Web服务器与几个用户的几个应用程序。 由于我不确定应用程序正在做什么以及它们产生的出站http / httpsstream量,因此我想要获得更多的控制权。 所以我的想法是使用一个只监听127.0.0.1:3128的内部Squid。 起初我只想看一下访问日志,第二步是安全相关的URL和域的黑名单。 这些列表应该过滤apache和所有subprocess的出站stream量(例如:其中一个应用程序正在作为系统调用运行curl)。

我已经添加了http_proxy到/ etc / sysconfig / proxy,到/ etc / environment和apache系统用户的.bashrc。 当我使用shell时,一切都很好,Apache根本不使用代理。 我已经在更改后重新启动了apche,但没有成功。

顺便说一下,我在Web服务器上运行OpenSuse 11。

解决方法:(感谢ALex_hha,对不起,我想我正在读你的答案太快)我input了下面的iptables – 规则:

iptables -t nat -I OUTPUT -p tcp --dport 80 -m owner --uid-owner apache -j DNAT --to-destination 127.0.0.1:3128 

并将Squid设置为透明模式:

 http_port 127.0.0.1:3128 transparent 

现在它运行得很好。

您可以将所有传出的httpstream量redirect到鱿鱼。 鱿鱼应该在透明模式下运行

 # iptables -t nat -I OUTPUT -p tcp --dport 80 -j DNAT --to-destination 127.0.0.1:3128 

但是,您无法确定哪个用户是stream量。 但是你可以尝试在几个端口上运行squid,每个端口都为特定的用户运行

 http_port 127.0.0.1:3128 transparent http_port 127.0.0.1:3129 transparent http_port 127.0.0.1:3130 transparent 

然后将传出的stream量redirect到特定的端口

 # iptables -t nat -I OUTPUT -p tcp --dport 80 -m owner --uid-owner apache -j DNAT --to-destination 127.0.0.1:3128 # iptables -t nat -I OUTPUT -p tcp --dport 80 -m owner --uid-owner joe -j DNAT --to-destination 127.0.0.1:3129 # iptables -t nat -I OUTPUT -p tcp --dport 80 -m owner --uid-owner jack -j DNAT --to-destination 127.0.0.1:3130 

并以鱿鱼日志格式使用lp

 logformat uniq_user %lp %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st %Ss:%Sh 

我在日志文件中遇到了很多错误。 我将服务器的IP地址添加到cal,但没有工作。 无论如何,我认为这个请求会无限循环地结束

你需要绕过鱿鱼本身的所有请求

 # iptables -t nat -I OUTPUT -p tcp --dport 80 -m owner --uid-owner squid -j ACCEPT 

我认为,你的鱿鱼实例运行在squid:squid下,这是默认的

鱿鱼的工作configuration

 acl manager proto cache_object acl localhost src 127.0.0.1/32 ::1 acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1 acl localnet src 10.0.0.0/8 # RFC1918 possible internal network acl localnet src 172.16.0.0/12 # RFC1918 possible internal network acl localnet src 192.168.0.0/16 # RFC1918 possible internal network acl localnet src fc00::/7 # RFC 4193 local private network range acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines acl SSL_ports port 443 acl Safe_ports port 80 # http acl Safe_ports port 21 # ftp acl Safe_ports port 443 # https acl Safe_ports port 70 # gopher acl Safe_ports port 210 # wais acl Safe_ports port 1025-65535 # unregistered ports acl Safe_ports port 280 # http-mgmt acl Safe_ports port 488 # gss-http acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl CONNECT method CONNECT http_access allow manager localhost http_access deny manager http_access deny !Safe_ports http_access deny CONNECT !SSL_ports http_access allow all http_access allow localnet http_access allow localhost http_access deny all http_port localhost:3128 transparent http_port localhost:3129 transparent http_port localhost:3130 transparent hierarchy_stoplist cgi-bin ? coredump_dir /var/spool/squid refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern -i (/cgi-bin/|\?) 0 0% 0 refresh_pattern . 0 20% 4320 logformat uniq_user %lp %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st %Ss:%Sh access_log /var/log/squid/squid-users.log uniq_user 

鱿鱼版本和系统的细节

 # squid -v | head -1 Squid Cache: Version 3.1.10 # uname -r 2.6.32-358.14.1.el6.x86_64 # cat /etc/redhat-release CentOS release 6.4 (Final) 

确保OUTPUT链中的第一条规则是 – “-p tcp –dport 80 -m owner –uid-owner squid -j ACCEPT”