两台服务器
192.168.100.10 安装squid
192.168.100.20 安装nginx
192.168.100.253 win7客户端
一,安装Squid
1,创建管理Squid的用户
[root@centos01 ~]# useradd -M -s /sbin/nologin squid
2,挂载linux光盘解压缩Squid
[root@centos01 ~]# tar zxvf /mnt/squid-3.4.6.tar.gz -C /usr/src/
3,编译安装Squid
[root@centos01 squid-3.4.6]# ./configure --prefix=/usr/local/squid --sysconfdir=/etc --enable-linux-netfilter --enable-async-io=240 --enable-default-err-language=Simplify_Chinese --disable-poll --enable-epoll --enable-gunregx && make && make install
4,优化Squid命令。修改目录所有者
[root@centos01 ~]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
[root@centos01 ~]# chown -R squid:squid /usr/local/squid/
二,配置传统代理
1,修改squid配置文件
[root@centos01 ~]# vim /etc/squid.conf
54 http_access allow all
55 cache_effective_user squid
56 cache_effective_group squid
57 reply_body_max_size 10 M
2,检查修改过的配置文件语法
[root@centos01 ~]# squid -k parse
3,初始化缓存目录后启动服务(启动前必须初始化)
[root@centos01 ~]# squid -z
[root@centos01 ~]# squid
4,客户端访问,网站服务器查看日志
[root@centos02 ~]# tail -f /usr/local/nginx/logs/access.log
正常访问查看日志
5,配置代理访问
查看nginx日志
查看squid日志
三,透明代理
模拟内外网环境
squid服务器 ens32:192.168.100.10 ens34:192.168.200.10
nginx网站服务器 ens32:192.168.200.20
win7客户端:192.168.100.253
1,开启squid服务器路由功能
[root@centos01 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
2,启动防火墙,使用防火墙规则映射端口号
[root@centos01 ~]# systemctl start firewalld
修改默认区域为external
[root@centos01 ~]# firewall-cmd --set-default-zone=external
网卡加入不同的区域
[root@centos01 ~]# firewall-cmd --add-interface=ens32 --zone=trusted
[root@centos01 ~]# firewall-cmd --add-interface=ens34 --zone=external
public区域允许tcp协议3128端口入站
[root@centos01 ~]# iptables -t nat -I PREROUTING -i ens32 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
3,修改squid配置文件
[root@centos01 ~]# vim /etc/squid.conf
55 http_access allow all #允许所有人访问
56 cache_effective_user squid #管理用户squid
57 cache_effective_group squid #管理组squid
58 reply_body_max_size 10 MB Mache_dir /var/spool/squid 100 16 256 #设置
59 http_port 192.168.200.10:3128 transparent #透明代理
4,客户端访问验证
http://192.168.200.20
查看Nginx日志
查看Squid日志
三,反向代理
squid服务器 ens32:192.168.100.10 ens34:192.168.200.10
web01:192.168.100.20 GATEWAY:192.168.100.10
web02:192.168.100.30 GATEWAY:192.168.100.10
win7客户端
1,安装squid和httpd
[root@centos01 ~]# yum -y install squid
[root@centos02 ~]# yum -y install httpd
[root@centos03 ~]# yum -y install httpd
2,网站根目录写入数据,启动httpd
[root@centos02 ~]# echo "www.aaa.com" > /var/www/html/index.html
[root@centos03 ~]# echo "www.bbb.com" > /var/www/html/index.html
[root@centos02 ~]# systemctl restart httpd
[root@centos03 ~]# systemctl restart httpd
3,开启路由功能
[root@centos01 ~]# vim /etc/sysctl.conf
[root@centos01 ~]# sysctl -p
net.ipv4.ip_forward = 1
4,修改squid配置文件
[root@centos01 ~]# vim /etc/squid.conf
39 http_access allow all #允许所有人访问
40 http_port 192.168.200.10:80 vhost #监听外网IP地址和80端口
41 cache_effective_user squid #管理squid用户
42 cache_effective_group squid #管理squid组
43 cache_peer 192.168.100.20 parent 80 0 originserver round-robin #web服务器192.168.100.20端口80调度算法轮询
44 cache_peer 192.168.100.30 parent 80 0 originserver round-robin #web服务器192.168.100.30端口80调度算法轮询
45 cache_dir ufs /var/spool/squid 10 16 256 # 设置缓存数据
5,启动squid服务,客户端验证查看
[root@centos01 ~]# systemctl start squid
http://192.168.200.20
再次访问
访问同时查看Squid日志