nginx反向代理配置_squid反向代理nginx并实现缓存的配置

更新时间:2019-11-30    来源:nginx    手机版     字体:

【www.bbyears.com--nginx】


使用squid作为方向代理服务器可以像nginx一样起到缓存功能,并且可以将缓存内容保存在内存中,nginx是将缓存内容保存在硬盘上,配置原理与nginx一样,squid自己以web服务器方式工作,方向代理后端的web服务器给用户。

1、安装squid
tar -zxvf squid-3.4.10.tar.gz
cd squid-3.4.10
./configure --prefix=/usr/local/squid \
--enable-gnuregex \
--enable-icmp \
--enable-linux-netfilter \
--enable-default-err-language="Simplify_Chinese" \
--enable-kill-parent-hack \
--enable-cache-digests \
--enable-dlmalloc \
--enable-poll \
--enable-async-io=240 \
--enable-delay-pools \
--with-filedescriptors=65536 \
--enable-snmp \
--enable-arp-acl \
--with-large-files
make && make install

创建squid服务用户,并设置缓存目录权限

useradd -s /bin/nolog -M squid
chown -R squid:squid /usr/local/squid/var

2、配置squid

?
vim /usr/local/squid/etc/squid.conf
#配置squid服务的用户和组
cache_effective_user squid
cache_effective_group squid
#配置squid监听在80端口,accel是使用加速模式,vhost是使用虚拟主机
http_port 80 accel vhost vport
#定义后端web服务器,如果有多台可以写多个
cache_peer 127.0.0.1 parent 8080 0 no-query originserver round-robin name=node1
http_access allow all
#定义缓存所使用的最大内存
cache_mem 128 MB
#定义最大缓存对象为10MB
maximum_object_size 10240 KB
#定义日志
cache_log /usr/local/squid/var/logs/cache.log
cache_dir ufs /usr/local/squid/var/cache 128 16 256
#定义访问控制元素
#定义名为manage的元素,类型为proto(url访问协议),cache_object机制是squid的特性它用于访问squid的缓存管理接口
acl manager proto cache_object
#定义名为localhost的元素,类型为src(源地址),定义源地址为127.0.0.1/32
acl localhost src 127.0.0.1/32
#定义名称为Safe_ports的元素,类型为port,指定的port值为8080、80
acl Safe_ports port 8080 # proxy
acl Safe_ports port 80 # Web
#定义名为SSL_port的元素,类型为port,值为443
acl SSL_ports port 443 #https
#定义名为Purge,类型为method(http的请求方法),PURGE是squid的特殊方法,能强制删除缓存对象
acl Purge method PURGE
#定义名为CONNECT的元素,类型为method,CONNECT指定的方法
acl CONNECT method CONNECT
#根据定义好的元素创建访问控制列表
#运行任意的http访问
http_access allow all
#只允许本地访问manager元素,用于管理squid
http_access allow manager localhost
#拒绝其他客户端访问manager
http_access deny manager
#拒绝在Safe_ports定义之外的端口的访问
http_access deny !Safe_ports
#拒绝非443端口的connect方法
http_access deny CONNECT !SSL_ports
#只允许本地使用purge元素中定义的地址清除缓存
http_access allow Purge localhost
http_access deny Purge
http_access deny all
icp_access allow all

3、创建squid启动脚本

?
#!/bin/bash
# chkconfig: 2345 85 15
# description: squid is a proxy server
 
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0
 
#
SCREEN=`stty -F /dev/console size 2>/dev/null`
#获取列数,如果 /dev/console 这个文件不存在则设置默认值80
COLUMNS=${SCREEN#* }
[ -z $COLUMNS ] && COLUMNS=80
#定义颜色
RED="\033[31m"
BLUE="\033[34m"
GREEN="\033[32m"
YELLOW="\033[33m"
#还原为正常
NORMAL="\033[0m"
SPA_COL=$[$COLUMNS-14]
 
success() {
    string=$1
    RT_SPA=$[$SPA_COL-${#string}]
    echo -n "$string"
    for I in `seq 1 $RT_SPA`;do
        echo -n " "
    done
    echo -e " [  ${GREEN}OK${NORMAL}  ]"
}
 
failure(){
    string=$1
    RT_SPA=$[$SPA_COL-${#string}]
    echo -n "$string"
    for I in `seq 1 $RT_SPA`;do
        echo -n " "
    done
    echo -e "[ ${RED}FAILED${NORMAL} ]"
}
 
squid="/usr/local/squid/sbin/squid"
prog=$(basename $squid)
squid_conf="/usr/local/squid/etc/squid.conf"
 
rh_status() {
    status $prog
}
 
start() {
    rh_status
    retval=$?
    [ $retval -ne 0 ] && $squid -f $squid_conf && success "Starting squid"
}
 
stop() {
    rh_status
    retval=$?
    [ $retval -eq 0 ] && $squid -k shutdown && success "Stopping squid"
}
 
restart() {
    $squid -k restart
}
 
reload() {
    $squid -k reconfigure
}
 
check() {
    $squid -f $squid_conf -k check
    retval=$?
    [ $retval -eq 0 ] && echo "syntax is ok"
}
 
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    reload)
        reload
        ;;
    check)
        check
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|reload|check}"
esac

本文来源:http://www.bbyears.com/caozuoxitong/81562.html