squid配置正反代理

squid配置php

1. 什么是squidcss

squid能够作代理也能够作缓存html

squid缓存不只能够节省宝贵的带宽资源,也能够大大下降服务器的I/O.  linux

squid不只能够作正向代理,又能够作反向代理。 web

正向代理,squid后面是客户端,客户端上网要经过Squid去上;反向代理,squid后面是服务器,服务器返回给用户数据须要走squid. vim

正向代理用在企业的办公环境中,员工上网须要经过squid代理来上网,这样能够节省网络带宽资源。而反向代理用来搭建网站静态项(图片、html、流媒体、js、css等)的缓存服务器,它用于网站架构中。 缓存


2. 搭建squid正向代理服务器

官方网站为 http://www.squid-cache.org/  网络

yum install -y squid架构

squid -v  查看版本以及编译参数

> /etc/squid/squid.conf

vim /etc/squid/squid.conf

加入以下配置

http_port 3128

acl manager proto cache_object

acl localhost src 127.0.0.1/32 ::1

acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1

acl localnet src 10.0.0.0/8     # RFC1918 possible internal network

acl localnet src 172.16.0.0/12  # RFC1918 possible internal network

acl localnet src 192.168.0.0/16 # RFC1918 possible internal network

acl SSL_ports port 443

acl Safe_ports port 80 8080         # http

acl Safe_ports port 21          # ftp

acl Safe_ports port 443         # https

acl CONNECT method CONNECT

http_access allow manager localhost

http_access deny manager

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access allow localnet

http_access allow localhost

http_access allow all

cache_dir aufs /data/cache 1024 16 256

cache_mem 128 MB

hierarchy_stoplist cgi-bin ?

coredump_dir /var/spool/squid

refresh_pattern ^ftp:           1440    20%     10080

refresh_pattern ^gopher:        1440    0%      1440

refresh_pattern -i (/cgi-bin/|\?) 0     0%      0

refresh_pattern \.(jpg|png|gif|mp3|xml) 1440    50%     2880    ignore-reload

refresh_pattern .               0       20%     4320

### 到此结束,acl是访问控制


mkdir  /data/cache  #建立缓存目录

chown -R squid:squid /data/cache  #更改权限

squid -z  #初始化缓存目录,会在缓存目录下生成不少子目录,该步骤能够省略

/etc/init.d/squid start 

squid  -kcheck #能够检测配置文件是否有错

squid -k rec #能够从新加载配置

service squid restart #重启squid服务


设置一下正向代理,Internet选项--连接--局域网设置--高级,输入代理ip地址和端口

测试:

tcpdump -nn port 3128 and host 192.168.11.13

或者

curl -xlocalhost:3128 www.qq.com -I          -x指的是代理服务器

查看缓存文件 find /data/cache/ -type f


访问图片,测试缓存: curl -xlocalhost:3128   -I 'http://www.bier.com/bbs/static/p_w_picpath/common/logo.png'




设置白名单,或者说只想代理某几个域名  vim /etc/squid/squid.conf

acl http proto HTTP 

acl good_domain dstdomain .lishiming.net .aminglinux.com 

http_access allow http good_domain 

http_access deny http !good_domain 


测 curl -xlocalhost:3128 www.google.com.hk -I  #403

测 curl -xlocalhost:3128 www.bier.com/bbs/forum.php  -I #200


vim /etc/squid/squid.conf  #设置域名黑名单

acl http proto HTTP 

acl bad_domain dstdomain .baidu.com

http_access deny http !bad_domain 


日志 ls /var/log/squid/cache.log



3. 搭建squid反向代理

vim /etc/squid/squid.conf  #以下变动

http_port 3128 改成 http_port 80 accel vhost vport 

增长以下内容:

cache_peer 123.125.119.147 parent 80 0 originserver name=a #指定访问的服务器

cache_peer 61.135.169.125 parent 80 0 originserver name=b 

cache_peer_domain a www.qq.com 

cache_peer_domain b www.baidu.com 

以前增长的域名白/黑名单相关配置去掉

若是是squid要代理一台web上的全部域名,那么就写成这样: cache_peer 192.168.10.111 80 0 originserver  #只须要这一行,cache_peer_domain  均可以省掉

/etc/init.d/squid restart 

测试 www.baidu.com www.qq.com  www.bier.net


curl -x127.0.0.1:80 www.qq.com -I

curl -x127.0.0.1:80 -I 'https://www.baidu.com/img/bd_logo1.png'

相关文章
相关标签/搜索