配置squid,实现正向代理

环境:CentOS 6.5

代理主机ip:192.168.3.224,10.0.0.10

内网主机ip:10.0.0.11

安装前准备

1、关闭selinux

[[email protected] ~]# setenforce 0
[[email protected] ~]# getenforce
permissive
[[email protected] ~]# vim /etc/selinux/config
SELINUX=disabled

2、关闭防火墙filter表,设置防火墙端口转发规则

[[email protected] ~]# iptables -t filter -F
[[email protected] ~]# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

[[email protected] ~]# service iptables save

3、修改主机路由模式

[[email protected] ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

编译安装squid

1、安装squid

http://www.squid-cache.org/Versions/v3/3.2/squid-3.2.3.tar.gz

[[email protected] ~]# tar xf squid-3.2.3.tar.gz
[[email protected] ~]# cd squid-3.2.3

[[email protected] ~]# ./configure --prefix=/usr/local/squid --enable-dlmalloc --enable-gnuregex --disable-carp --enable-async-io=100 --with-aufs-threads=32 --with-pthreads --enable-storeio="ufs,aufs" --enable-removal-policies="heap,lru" --enable-icmp --enable-htcp --enable-delay-pools --enable-useragent-log --enable-referer-log --disable-wccp --disable-wccpv2 --enable-kill-parent-hack --enable-arp-acl --disable-snmp --enable-default-err-language=Simplify_Chinese --enable-err-languages="Simplify_Chinese English" --disable-poll --disable-select --enable-epoll --enable-auth --enable-auth-basic="DB,NCSA,PAM,RADIUS,SASL" --with-aio --disable-ident-lookups --enable-truncate --enable-stacktraces --with-maxfd=65535 --disable-ipv6 --enable-ipf-transparent --enable-linux-netfilter

2、配置squid

[[email protected] ~]# mkdir -p /data/squid/{cache,coredump,logs}

[[email protected] ~]# /usr/sbin/groupadd squid
[[email protected] ~]# /usr/sbin/useradd squid -g squid -s /sbin/nologin

[[email protected] ~]# chmod -R 777 /data/squid/{cache,coredump,logs}
[[email protected] ~]# chown -R squid.squid /data/squid/{cache,coredump,logs}

3、配置文件内容

[[email protected] ~]# vim /usr/local/squid/etc/squid.conf
http_port 10.0.0.10:1080
                                           
cache_effective_user squid
cache_effective_group squid
                                           
cache_mem 2048 MB
cache_swap_low 90
cache_swap_high 95
                                           
ipcache_size 1024
ipcache_low 90
ipcache_high 95
                                           
cache_replacement_policy lru
memory_replacement_policy lru
                                           
cache_dir aufs /data/squid/cache 20480 16 256
coredump_dir /data/squid/coredump
                                           
memory_pools_limit 1024 MB
max_open_disk_fds 0
minimum_object_size 0 KB
maximum_object_size 32768 KB
maximum_object_size_in_memory 2048 KB
                                           
access_log /dev/null
cache_access_log none
                                           
cache_log /dev/null
cache_store_log none
                                           
cache_swap_log /data/squid/logs/swap.log
                                           
logfile_rotate 1
pid_filename /usr/local/squid/var/logs/squid.pid
                                           
cache_mgr [email protected]
strip_query_terms off
visible_hostname ProxySrv
error_directory /usr/local/squid/share/errors/zh-cn
                                           
request_header_max_size 64 KB
request_body_max_size 0 KB
                                           
negative_ttl 5 minutes
read_timeout 1 minutes
client_lifetime 10 minutes
connect_timeout 1 minute
peer_connect_timeout 30 seconds
request_timeout 2 minutes
persistent_request_timeout 1 minute
                                           
client_persistent_connections off
server_persistent_connections on
tcp_recv_bufsize 65535 bytes
half_closed_clients off
httpd_suppress_version_string off
ie_refresh off
allow_underscore on
                                           
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
                                           
dns_nameservers DNS服务器IP
                                           
acl OverConnLimit maxconn 300
http_access deny OverConnLimit
                                           
acl our_network src 192.168.0.0/16
http_access allow our_network
                                           
acl SSL_ports port 443
acl CONNECT method CONNECT
http_access deny CONNECT !SSL_ports
                                           
request_header_access Via deny all
request_header_access X-Forwarded-For deny all
#检查配置是否正确
[[email protected] ~]# /usr/local/squid/sbin/squid -k parse

#初始化cache缓存目录
[[email protected] ~]# /usr/local/squid/sbin/squid -z

4、配置启动脚本

[[email protected] ~]# vim /etc/init.d/squid
#!/bin/sh
#
#squid - this script start and stop the squid daemon
#
# chkconfig: - 90 25
# description: squid is a pagecache reverse proxy.
# processname: squid
# pidfile: /usr/local/squid/var/logs/squid.pid
# config: /usr/local/squid/etc/squid.conf
#
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
              
BINFILE="/usr/local/squid/sbin/squid"
CFGFILE="/usr/local/squid/etc/squid.conf"
PIDFILE="/usr/local/squid/var/logs/squid.pid"
LOCKFILE="/var/lock/squid.lock"
CACHEPATH="/data/squid/cache"
OUTFILE="/data/squid/logs/squid.out"
              
SQUID_PIDFILE_TIMEOUT=${SQUID_PIDFILE_TIMEOUT:-20}
SQUID_SHUTDOWN_TIMEOUT=${SQUID_SHUTDOWN_TIMEOUT:-100}
              
[[ -f $BINFILE ]] && SQUID="${BINFILE}"
              
CACHE_SWAP=`sed -e ‘s/#.*//g‘ ${CFGFILE} | grep cache_dir | awk ‘{print $3}‘`
[ -z "$CACHE_SWAP" ] && CACHE_SWAP="${CACHEPATH}"
              
RETVAL=0
              
start() {
    if [[ ! -f ${CFGFILE} ]]; then
        echo "The configuration file: ${CFGFILE} has no found!" 1>&2
        exit 6
    fi
                 
    SQUID_OPTS="-s -f ${CFGFILE}"
                 
    [[ -z "$SQUID" ]] && echo "Insufficient privilege" 1>&2 && exit 4
                 
    for adir in $CACHE_SWAP
    do
        if [[ ! -d $adir/00 ]]; then
            echo -n "init_cache_dir $adir"
            $SQUID -z -F -D >> ${OUTFILE} 2>&1
        fi
    done
                 
    echo -n "Starting squid..."
    $SQUID $SQUID_OPTS >> ${OUTFILE} 2>&1
                 
    RETVAL=$?
                 
    if [[ $RETVAL -eq 0 ]]; then
        timeout=0;
                     
        while :
        do
            [[ ! -f ${PIDFILE} ]] || break
            [[ $timeout -ge $SQUID_PIDFILE_TIMEOUT ]] && RETVAL=1 && break
                         
            sleep 1 && echo -n "."
            timeout=$((timeout+1))
        done
    fi
                 
    echo ""
    [[ $RETVAL -eq 0 ]] && touch ${LOCKFILE}
    [[ $RETVAL -eq 0 ]] && echo "start squid is ok!"
    [[ $RETVAL -ne 0 ]] && echo "start squid is failed!"
                 
    return $RETVAL
}
              
stop() {
    SQUID_SHUTDOWN_TIMEOUT=${SQUID_SHUTDOWN_TIMEOUT:-100}
    echo -n "Stopping squid..."
    $SQUID -k check >> ${OUTFILE} 2>&1
                 
    RETVAL=$?
                 
    if [[ $RETVAL -eq 0 ]]; then
        $SQUID -k shutdown &
        rm -f ${LOCKFILE}
                     
        timeout=0
                     
        while :
        do
            [[ -f ${PIDFILE} ]] || break
            [[ $timeout -ge $SQUID_SHUTDOWN_TIMEOUT ]] && echo "" && return 1
                         
            sleep 2 && echo -n "."
            timeout=$((timeout+2))
        done
                     
        echo ""
        echo "Stop squid is ok!"
    else
        echo ""
        echo "Stop squid is failed!"
        [[ ! -e ${LOCKFILE} ]] && RETVAL=0
    fi
                 
    return $RETVAL
}
              
restart() {
    stop
    sleep 1
    start
}
              
case "$1" in
start)
    start
    ;;
                 
stop)
    stop
    ;;
                 
reload)
    SQUID_OPTS=${SQUID_OPTS:-"-D"}
    $SQUID -k reconfigure -f ${CFGFILE}
    ;;
                 
restart)
    restart
    ;;
                 
condrestart)
    [[ -e ${LOCKFILE} ]] && restart || :
    ;;
                 
*)
    echo $"Usage: $0 {start|stop|reload|restart|condrestart}"
    exit 2
esac
              
exit $?
[[email protected] ~]# chmod +x /etc/init.d/squid  #添加执行权限
[[email protected] ~]# service squid start         #启动服务

3、配置主机ip地址


代理主机内网ip

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=10.0.0.10
NETMASK=255.0.0.0

内网主机ip地址

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=52:54:00:B1:B4:99
TYPE=Ethernet
UUID=4dd9081e-2cf6-4f81-bde4-561d3877267e
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=10.0.0.11
NETMASK=255.0.0.0
GATEWAY=10.0.0.10
DNS1=8.8.8.8
DNS2=8.8.4.4

内网主机测试可行:

[[email protected] ~]# curl -I www.qq.com
HTTP/1.1 200 OK
Server: squid/3.4.3
Date: Wed, 13 Jul 2016 06:01:36 GMT
Content-Type: text/html; charset=GB2312
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
Expires: Wed, 13 Jul 2016 06:02:36 GMT
Cache-Control: max-age=60
Vary: Accept-Encoding
Access-Control-Allow-Origin: http://bz.qq.com
X-Cache: HIT from nanjing.qq.com
时间: 2024-08-06 03:22:35

配置squid,实现正向代理的相关文章

centos7 安装配置 squid作为正向代理

安装 yum install squid -y 配置 配置文件 /etc/squid/squid.conf 1.无认证配置 cache_mem 64 MB                                                       缓存和日志设置maximum_object_size 4 MBcache_dir ufs /var/spool/squid 100 16 256access_log /var/log/squid/access.log 2.带用户认证的配

Linux与云计算——第二阶段 第一十一章:代理Proxy服务器架设—Squid代理服务器正向代理和客户端配置

Linux与云计算--第二阶段Linux服务器架设 第一十一章:代理Proxy服务器架设-Squid代理服务器正向代理和客户端配置 安装Squid 安装Squid来配置代理服务器. [1] 这是一个通用的转发代理配置 [[email protected] ~]# yum -y install squid [[email protected] ~]# vi /etc/squid/squid.conf # line 26: 添加一条新的ACL acl lan src 192.168.96.0/24

实践出真知——基于squid实现正向代理实践

实践出真知--基于squid实现正向代理实践 前言 ? 俗话说得好:时间是治愈一切伤口的良药,实践是检验真理的唯一标准!本文将结合代理服务器的基本原理,进行基于squid软件实现传统(标准)代理(正向代理)以及透明代理的实例演示. 先说说squid是个啥 ? 避(kai)免(ge)被(wan)喷(xiao),还是介绍一下什么是squid吧. squid的概念 ? squid是一种用来缓存Internet数据的软件.用于接受来自客户端需要下载对象(object)的请求并适当的处理这些请求.也就是说

Centos7使用squid实现正向代理

正向代理:代理服务器帮助客户端(浏览器)实现互联网的访问 (1)代理服务器配置 1.安装squid yum install squid -y 2.编辑squid配置文件 #vim /etc/squid/squid.conf acl local src 192.9.191.0/24 //允许192.9.191.0/24网段内所有客户机访问代理服务器 http_access allow localnet //该记录一定要添在deny all之前 http_port 3128 3.防火墙配置 syst

SQUID传统正向代理

传统正向代理 1.1 问题 本案例要求先快速搭建好一台Web服务器: 建立Web测试文件 /var/www/html/index.html 然后创建代理服务器,使得192.168.4.0/24网段中的所有主机,均可以通过该代理服务器访问Web服务. 设置代理服务端口为3128 设置内存缓存容量128M 设置硬盘缓存容量为200M,一级目录16个,二级目录128个 然后验证客户机能否通过代理服务器访问Web服务器: 客户端从浏览器访问Web服务器 查看服务器B的代理访问日志,验证来访地址 查看服务

apache 配置代理服务器(正向代理,反向代理)

正向代理和反向代理是从数据流方向命名的,正向代理,代理的用户请求:反向代理,替代服务器接受请求 正向代理: 用户 -> 代理服务器 -> www  用户的请求数据给发代理服务器,代理服务器帮助用户去www请求数据,然后再返回给用户 作用:配置代理服务器,客户端做代理设置后可以访问facebook,Twitter 如何配置: <VirtualHost *:80> DocumentRoot /home/web ServerName www.xxx.com ServerAlias xxx

squid的正向代理和反向代理

squid:可以实功能.做翻墙~代理 可以实现代理也可以设置缓存 安装 :yum -y install squid 配置文件:vim /etc/squid/squid.conf cache_dir  缓存目录 必须打开(在下面添加) cache_mem 28MB 启用一点内存 缓存对象refresh_pattern \.(jpg|png|gif|js|css|MP3|MP4) 1440 20% 2880 ignore-reload(忽略重新加载) visible=hostname 定义hostn

centos7 下 apache nginx squid https正向代理

apache yum install httpd mod_ssl -y vim /etc/httpd/conf.d/ssl.conf Listen 443 https <VirtualHost *:443> ServerName example.com SSLEngine on SSLCertificateFile /cert/server.crt SSLCertificateKeyFile /cert/server.key ProxyRequests On ProxyVia On <P

squid正向代理和反向代理服务器搭建

squid是比较知名的代理软件,它不仅可以跑在Linux上还可以跑在Windows和UNIX上,它的技术已经非常成熟.目前使用squid的用户也十分广泛.squid与Linux下其他的代理软件如Apache.socks.TIS FWTK等相比,下载安装简单,配置简单灵活,支持缓存和多种协议. squid之所以用的很多,是因为它的的缓存功能,squid的缓存不仅可以节省宝贵的带宽资源,也可以大大降低服务器的I/O.从经济角度考虑,它是很多网站架构中不可或缺的角色. squid可以做正向代理,也可以

Squid做反向代理缓存

环境介绍: 主机名 角色 IP地址 squid.contoso.com squid代理服务器 192.168.49.135 image01.contoso.com 图片服务器(web) 192.168.49.139 一.准备工作 以其中一台为例: [[email protected] etc]# iptables -L Chain INPUT (policy ACCEPT) target     prot opt source               destination Chain FO