企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] 192.168.56.130 - client 192.168.56.131:3128 - proxy 192.168.56.120:8000 - server(nginx) ## 1. 安装配置squid(代理) ### 1.1 安装 ~~~ apt-get update apt-get install -y squid ~~~ ### 1.2. 修改配置文件 ~~~ mkdir -p /var/log/squid chown -R proxy.proxy /var/log/squid mkdir -p /data/squid/cache chown -R proxy.proxy /data/squid/cache ~~~ ~~~ # 设置缓存 cache_mem 1 MB cache_dir aufs /data/squid/cache 10240 32 128 cache_mgr 931309012@qq.com # 访问日志 access_log daemon:/var/log/squid3/access.log squid cache_log /var/log/squid/cache.log cache_store_log /var/log/squid/store.log acl inner src 192.168.56.0/24 http_access allow inner ~~~ ### 1.3 启动 1. 启动 ~~~ service squid3 start ~~~ 2. 查看启动是有信息(是否有错误) ~~~ squid3 -k check ~~~ 3. 查看配置信息 ~~~ squid3 -k parse ~~~ ## 2. 客户端设置代理 1. 修改/etc/profile文件 这个文件是每个用户登录时都会运行的环境变量设置,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置。 ~~~ vim /etc/profile ~~~ ~~~ export http_proxy=192.168.56.131:3128 export https_proxy=$http_proxy ~~~ 2. 刷新/etc/profile文件 ~~~ source /etc/profile ~~~ ## 3. 服务端 服务端就是一个简单的nginx,开启了日志,用于查看客户端ip地址 nginx日志格式 ~~~ log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; ~~~ $remote_addr :远程请求服务器的ip地址 $remote_user : 用于HTTP基础认证服务的用户名 [$time_local] : 时间 $request: 代表客户端的请求地址 $status :HTTP响应码 $body_bytes_sent :传输给客户端的字节数,响应头不计算在内 $http_referer: url跳转来源,用来记录从那个页面链接访问过来的 $http_user_agent:用户终端浏览器等信息 $http_x_forwarded_for: 简称XFF头,它代表客户端,也就是HTTP的请求端真实的IP,只有在通过了HTTP 代理或者负载均衡服务器时才会添加该项 ### 查看squid访问日志 ![](https://box.kancloud.cn/7b31a751c65dea5aeda5e58b52e68048_1428x168.png) tail -f /var/log/nginx/logs/access.log ![](https://box.kancloud.cn/7cdc5ebeccc51245a3ee8bdeb1363a66_1257x250.png) 此时,虽然client通过192.168.56.131代理访问nginx,但是client的真正ip:192.168.56.130也是暴露给了nginx。想要‘匿名’的访问nginx,则需要对http_x_forwarded_for进行设置。 ~~~ request_header_access X-Forwarded-For deny all request_header_access user-agent deny all ~~~ 重启squid3(去掉缓存后),再次访问nginx ![](https://box.kancloud.cn/b225de97dabf6d0326cf0f85ab1eba19_1329x280.png) 此时隐藏了代理设置的 X-Forwarded-For和UserAgent,防止了客户端ip被暴露。爬虫代理ip也是如此,通过更换代理ip来保护我们真实的ip。 ## 4. 缓存 squid会对client的请求进行缓存,对于已经缓存过的请求,代理服务器会直接将本地缓存的结果返回给client,效率大大提升。 ### 4.1 client向nginx发起请求 ~~~ curl http://192.168.56.120:8000 # 得到以下响应 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> ~~~ **nginx日志没有变化 ,说明刚才客户端的请求没有到达nginx,由代理服务器将缓存的index.html返回给了client:** ~~~ tail -f /var/log/nginx/logs/access.log # 没有变化 ~~~ 在设置的缓存路径/data/squid/cache/00/00中,多了一个00000000文件,看看这个文件是啥? ![](https://box.kancloud.cn/ea134ef1b4628967dc8af836e3fadfca_571x91.png) ![](https://box.kancloud.cn/9a13f4a11a65a9dc174a230d647a1671_1091x821.png) 对,这就是缓存的nginx首页HTML代码。