本文共 10840 字,大约阅读时间需要 36 分钟。
作为应用层的代理服务软件,squid主要提供缓存加速和应用层过滤控制功能
客户端向网站发送请求数据
(为了能承受更多的并发连接客户端访问先请求代理服务器听过代理服务器提供出的数据给客户端,如果代理服务器上没有客户端的需求则代理服务器江湖发送请求给web服务器请求数据然后缓存到自己的缓存里面)
win7设置代理
[root@localhost home]# tar zxvf /home/squid-4.1.tar.gz -C /opt/[root@localhost home]# cd /opt/[root@localhost opt]# lsrh squid-4.1
[root@localhost home] cd /opt/squid-4.1[root@localhost squid-4.1] ./configure > --prefix=/usr/local/squid \ #指定安装路径> --sysconfdir=/etc \ #配置文件存放位置 > --enable-arp-acl \ #通过mac地址进行管理防止arp欺骗> --enable-linux-netfilter \ #内核过滤 > --enable-linux-tproxy \ #指定支持透明模式> --enable-async-io=100 \ #制定性能> --enable-err-language="Simplify_Chinese" \ #出现报错用中文方式显示> --enable-underscore \ #允许有下划线> --enable-poll \ #提高性能> --enable-gnuregex #支持正则表达式[root@localhost squid-4.1]# make && make install
[root@localhost squid-4.1] ln -s /usr/local/squid/sbin/* /usr/local/sbin/
[root@localhost squid-4.1] useradd -M -s /sbin/nologin squid
[root@localhost squid-4.1] chown -R squid.squid /usr/local/squid/var/
[root@localhost squid-4.1] vim /etc/squid.conf# And finally deny all other access to this proxyhttp_access allow allhttp_access deny all# Squid normally listens to port 3128http_port 3128cache_effective_user squid #添加 指定程序用户cache_effective_group squid #添加 指定账号基本组
[root@localhost ~] squid -z[root@localhost ~] 2018/07/23 16:22:26| Created PID file (/usr/local/squid/var/run/squid.pid)2018/07/23 16:22:27 kid1| Set Current Directory to /usr/local/squid/var/cache/squid2018/07/23 16:22:27 kid1| Creating missing swap directories2018/07/23 16:22:27 kid1| No cache_dir stores are configured.2018/07/23 16:22:27| Removing PID file (/usr/local/squid/var/run/squid.pid)
[root@localhost squid-4.1]# squid [root@localhost squid-4.1]# netstat -ntap #查看3128端口有没有开启Active Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 3031/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1001/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1000/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2201/master tcp 0 0 192.168.32.207:22 192.168.32.1:49265 ESTABLISHED 14414/sshd: root@pt tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 1001/sshd tcp6 0 0 ::1:631 :::* LISTEN 1000/cupsd tcp6 0 0 :::3128 :::* LISTEN 30544/(squid-1) tcp6 0 0 ::1:25 :::* LISTEN 2201/master
[root@localhost ~] cd /etc/init.d/[root@localhost init.d] vim squid#!/bin/bash#chkconfig: 2345 90 25PID="/usr/local/squid/var/run/squid.pid"CONF="/etc/squid.conf"CMD="/usr/local/squid/sbin/squid"case "$1" in start) netstat -natp | grep squid &> /dev/null if [ $? -eq 0 ] then echo "squid is running" else echo "正在启动 squid..." $CMD fi ;; stop) $CMD -k kill &> /dev/null rm -rf $PID &> /dev/null ;; status) [ -f $PID ] &> /dev/null if [ $? -eq 0 ] then netstat -natp | grep squid else echo "squid is not running" fi ;; restart) $0 stop &> /dev/null echo "正在关闭 squid..." $0 start &> /dev/null echo "正在启动 squid..." ;; reload) $CMD -k reconfigure ;; check) $CMD -k parse ;; *) echo "用法:$0{start|stop|status|reload|check|restart}" ;;esac[root@localhost init.d] chkconfig --add squid[root@localhost init.d] chkconfig --level 35 squid on
[root@localhost init.d]# service squid start正在启动 squid...[root@localhost init.d]# netstat -ntapActive Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 3031/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1001/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1000/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2201/master tcp 0 52 192.168.32.207:22 192.168.32.1:49265 ESTABLISHED 14414/sshd: root@pt tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 1001/sshd tcp6 0 0 ::1:631 :::* LISTEN 1000/cupsd tcp6 0 0 :::3128 :::* LISTEN 5412/(squid-1) tcp6 0 0 ::1:25 :::* LISTEN 2201/master [root@localhost init.d]# service squid stop
[root@localhost init.d] vim /etc/squid.conf#在配置文件中添加# Squid normally listens to port 3128http_port 3128cache_mem 64 MB #指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最>好为4的倍数,单位为MB,建议设为物理内存的1/4reply_body_max_size 10 MB #允许用户下载的最大文件大小,以字节为单位。默认设置0表示不进行限制maximum_object_size 4096 KB #允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接转发给用户cache_effective_user squid #添加 指定程序用户cache_effective_group squid #添加 指定账号基本组
[root@localhost init.d] iptables -F #清空防火墙[root@localhost init.d] setenforce 0 #关闭安全性模块[root@localhost init.d] iptables -I INPUT -p tcp --dport 3218 -j ACCEPT #允许访问3128端口访问tcp协议
[root@localhost init.d]# service squid start 正在启动 squid...[root@localhost init.d]# netstat -ntapActive Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 3031/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1001/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1000/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2201/master tcp 0 52 192.168.32.207:22 192.168.32.1:49265 ESTABLISHED 14414/sshd: root@pt tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 1001/sshd tcp6 0 0 ::1:631 :::* LISTEN 1000/cupsd tcp6 0 0 :::3128 :::* LISTEN 5652/(squid-1) tcp6 0 0 ::1:25 :::* LISTEN 2201/master
在Windows系统中开启浏览器Internet选项---》连接----》局域网设置----ip:squid服务器地址 端口:3128地址栏输入web服务器地址查看web服务器访问日志access.log 是代理服务器地址访问#上面是在windows里面设置的代理如果不在windows里面设置在linux/etc/porfile里面也可以设置在Linux系统中设置代理vim /etc/profile HTTP_PROXY=http://192.168.235.206:3128 //http代理服务器地址 HTTPS_PROXY=http://192.168.235.206:3128 //https的代理服务器地址 FTP_PROXY=http://192.168.235.206:3128 //ftp的代理服务器地址 NO_PROXY=192.168.10.,192.168.20. //当这两个网段地址访问网页不通过代理服务器 export HTTP_PROXY HTTPS_PROXY FTP_PROXY NO_PROXYsource /etc/profile 运行配置文件
配置IP地址:
squid的双网卡-
ens33:192.168.100.1ens36:12.0.0.1
web服务器:
12.0.0.12
client客户机
192.168.100.50
配置squid配置文件
#这是4.1以上版本的改动如果你用3.6的版本直接改动就行4.1以上版本直接改的话会冲突http_port 3128 #在3128端口下面重新添加一个端口http_port 192.168.100.1:3129 transparent#查看端口[root@localhost logs]# netstat -ntap | grep 3128tcp6 0 0 :::3128 :::* LISTEN 21144/(squid-1) [root@localhost logs]# netstat -ntapActive Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 3031/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1001/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1000/cupsd tcp 0 0 192.168.100.1:3129 0.0.0.0:* LISTEN 21144/(squid-1) tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2201/master tcp 0 52 192.168.100.1:22 192.168.100.4:52304 ESTABLISHED 20705/sshd: root@pt tcp 0 0 192.168.32.207:22 192.168.32.1:50718 ESTABLISHED 6895/sshd: root@pts tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 1001/sshd tcp6 0 0 ::1:631 :::* LISTEN 1000/cupsd tcp6 0 0 :::3128 :::* LISTEN 21144/(squid-1) tcp6 0 0 ::1:25
配置防火墙
[root@localhost ~]# iptables -F[root@localhost ~]# iptables -t nat -F[root@localhost ~]# setenforce 0[root@localhost ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3129[root@localhost ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3129[root@localhost ~]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT
在透明代理里缓存服务器作为网关访问web服务器
如下图:转载于:https://blog.51cto.com/13645280/2159491