squid(代理服务器)

------------------------------------------------------

client:192.168.4.1                   eth0

------------------------------------------------------

squid:192.168.4.5                   eth0

          192.168.2.5                   eth1

------------------------------------------------------

web:192.168.2.100                 eth1


环境配置

1、真实机(确认:virbr1(192.168.4.1))



web网站【加速】

代理软件 varnish squid nginx


1、正向代理【FQ】

正向代理的用途:

  (1)访问原来无法访问的资源,如google

       (2) 可以做缓存,加速访问资源

  (3)对客户端访问授权,上网进行认证

  (4)代理可以记录用户访问记录(上网行为管理),对外隐藏用户信息


搭建web服务器

web:192.168.2.100

[root@web1 ~]# yum -y install httpd

[root@web1 ~]# service httpd start

[root@web1 ~]# chkconfig httpd on

[root@web1 ~]# netstat -anptu |grep httpd

tcp        0      0 :::80                       :::*                        LISTEN      3479/httpd          

[root@web1 ~]# echo "123" > /var/www/html/index.html

[root@web1 ~]# cat /var/www/html/index.html


搭建squid服务器

squid:192.168.4.5

[root@proxy ~]# yum -y install squid

[root@proxy ~]# vim /etc/squid/squid.conf

http_access allow all                                    //允许所有人访问

# Squid normally listens to port 3128

http_port 3128                                             //默认监听端口

visible_hostname squid.example.com        //主机名

cache_dir ufs /var/spool/squid 100 16 256     

//缓冲位置/var/spool/squid

//100M的容量,自动创建16个一级子目录和256个二级子目录

[root@proxy ~]# service squid start

[root@proxy ~]#ls /var/spool/squid

[root@proxy ~]# curl http://192.168.2.100/


访问后查看访问日志,没问题看第一个日志,有问题看后面两个日志。

service lnmp_soft]# ls /var/log/squid/

access.log  cache.log  squid.out


在客户端访问web服务器

client:192.168.4.1

[root@client ~]# curl http://192.168.2.100/

curl: (7) Failed to connect to 192.168.2.100: 网络不可达

[root@client ~]# curl --proxy1.0 http://192.168.4.5:3128 http://192.168.2.100/

123


[root@client yum.repos.d]# curl -I --proxy1.0 http://192.168.4.5:3128 http://192.168.2.100

HTTP/1.0 200 OK

Last-Modified: Fri, 06 Aug 2021 22:52:15 GMT

Accept-Ranges: bytes

Content-Length: 4

Content-Type: text/html; charset=UTF-8

Date: Sat, 07 Aug 2021 00:10:50 GMT

Server: Apache/2.2.15 (Red Hat)

ETag: "1a010f-4-5c8ebe3092f50"

X-Cache: HIT from localhost

X-Cache-Lookup: HIT from localhost:3128

Via: 1.0 localhost (squid/3.1.23)

Connection: keep-alive



client设置:

Firefox

(编辑--首选项--高级--网络--设置)

  手动设置代理,http代理:192.168.4.5  端口:3128

地址栏输入服务器的IP地址:192.168.2.200


缓存目录的文件

[root@service lnmp_soft]# cat /var/spool/squid/00/00/00000000

a`http://192.168.2.100/HTTP/1.1 200 OK

Date: Fri, 06 Aug 2021 23:41:38 GMT

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Fri, 06 Aug 2021 22:52:15 GMT

ETag: "1a010f-4-5c8ebe3092f50"

Accept-Ranges: bytes

Content-Length: 4

Connection: close

Content-Type: text/html; charset=UTF-8


123




2、反向代理【加速】 加速+DNS(view)

反向代理的作用:

(1)保证内网的安全,阻止web攻击,大型网站,通常将反向代理作为公网访问地址,Web服务器是内网

(2)负载均衡,通过反向代理服务器来优化网站的负载


正向代理:用户-----网站

反向代理:网站



[root@proxy ~]# vim /etc/squid/squid.conf

http_port 80 vhost

cache_peer 192.168.2.100 parent 80 0 originserver

[root@proxy ~]# service squid restart

[root@proxy ~]# netstat -anptul|grep :80

^[[5~tcp        0      0 :::80                       :::*                        LISTEN      2541/(squid)


[root@client ~]# curl http://192.168.4.5/



加速+DNS(view)



acl    名称    类型    内容(内容写什么决定于类型)

acl    自定义    src/dst    ip/net

acl    自定义    time    8:00--9:00

acl    自定义    regex    正则


基本概念

ACL(访问控制列表)

定义acl

-- acl  列表名称    列表类型    列表内容 ... ...

调用acl

-- http_acl allow 列表名称 ...

-- http_acl deny 列表名称 ...


ACL规则匹配顺序

-未设置任何规则时,拒绝所有访问请求

-已设置规则时,依次进行检查,找到匹配即停止,否则采用与最后一条规则相反的权限


常用的ACL列表类型

src        源地址

dst        目标地址

port        目标端口

dstdomain    目标域

time        访问时间

maxconn        最大并发连接

url_regex        目标URL地址

urlpath_regex    整个目标URL路径


[root@service ~]# cat /usr/share/doc/squid-3.1.23/squid.conf.documented

acl aclname time [day-abbrevs] [h1:m1-h2:m2]

#         # [fast]

#         #  day-abbrevs:

#         #     S - Sunday

#         #     M - Monday

#         #     T - Tuesday

#         #     W - Wednesday

#         #     H - Thursday

#         #     F - Friday

#         #     A - Saturday




[root@proxy ~]# vim /etc/squid/squid.conf

acl kehuduan src 192.168.4.1

acl xiuxi time MTWHF 01:00-08:00

http_access deny xiuxi kehuduan

[root@proxy ~]# service squid restart


客户端访问:拒绝192.168.4.1访问,其他客户端可以访问。

[root@client ~]# curl http://192.168.4.5


<p>Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect.</p>


[root@room1pc01 桌面]# curl http://192.168.4.5

123