反向代理/加速代理
Squid <-----> Web
例如:一般都是html,htm,css,jpg,gif,png,txt,rar,mp3 等静态文件
没有cache-control协议头控制的,没有cookie协议头的文件,一般都会缓存, 不能缓存一般都是一些动态代码页面:.php,.asp,.jsp,.cgi等----> Squid ---> [亲自代替客户端去问web要数据]web--->Squid--->Client
----> Squid ---> /---如果缓存中命中,就会咨询一下后端web服务器,该文件是否有变化,如果没有变化就直接返回给客户端,如有变化,重新下载该文件并且缓存之和把最新的给客户端 \---如果缓存中没命中,亲自去web要数据,然后缓存之并且返回给客户端另外一种结构:
互联网 | 内网
/-----静态元素--> Squid ------> nginx代理--动静分离-> | \-----动态页面--> Php-web<---- 全局: 互联网的客户端: --> nginx代理的IP内网:
搭建网站程序的过程中 可以临时把解析到php-web服务器 而且通过来搭建 <---向导
nginx代理
proxy.upl.com 10.1.1.28 Squid squid.upl.com 10.1.1.29 php-web dev.upl.com 10.1.1.22hosts:
10.1.1.29 squid.upl.com 10.1.1.28 proxy.upl.com 10.1.1.22 dev.upl.com
一、搭建squid服务器
# ./configure --prefix=/usr/local/squid/ --enable-async-io=120 --enable-storeio=aufs,null,diskd --enable-icmp --enable-useragent-log --enable-referer-log --disable-wccp --disable-wccpv2 --enable-cache-digests --enable-default-err-language="Simplify_Chinese" --enable-linux-netfilter
# make && make install
要求实现反向代理。要求磁盘缓存使用diskd的形式。 # vim squid.confhttp_port 80 accel vhost vport
cache_peer parent 80 0 no-query originserver name=phpserv cache_peer_domain 10.1.1.22 phpserv cache_mem 1300 MB maximum_object_size_in_memory 16 KB cache_dir diskd /cache0 10240 16 256 Q1=64 Q2=72 logformat combined %{X-Real-IP}>h %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh access_log /usr/local/squid/var/logs/access.log combined visible_hostname squid.upl.com dns_nameservers 10.1.1.22 cache_effective_user squid cache_effective_group squid http_access allow all # mkdir /cache0 # id squid # chown squid:squid /cache0/ # chown squid:squid /usr/local/squid/var/logs/
# /usr/local/squid/sbin/squid -X -z
启动:
# /usr/local/squid/sbin/squid -N -D -d1 二、搭建php-web已经搭建过。
三、搭建nginx代理
安装:
pcre nginx实现反向代理,实现动静分离
HTTP 代理
proxy_pass 进行代理分发,亲自代替客户端去文后面指定的路径的web服务器要数据 proxy_set_header 在原本的请求的基础上,自定义了一个协议头字段例子:
location / { <---访问整个网站任意路径的请求都能匹配 proxy_pass ; <---- 所跟的后端服务器可以是直接IP:端口,也可以是一个服务器池 proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; }HTTP Upstream 《---服务器池的定义
upstream backend { server backend1.example.com weight=5; server backend2.example.com:8080; server unix:/tmp/backend3; } server { location / { proxy_pass ; } }
注意:
多个location之间是有优先顺序的,最前面的最优先,匹配了就不会再去匹配的location 错误的location顺序:location / {
}
location /status {}
参考配置文件:user daemon;
worker_processes 4;error_log logs/error.log info;
pid logs/nginx.pid; worker_rlimit_nofile 204800; events { worker_connections 65535; } http { include mime.types; default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';access_log logs/access.log main;
sendfile on;
tcp_nopush on;keepalive_timeout 3;
gzip on;
gzip_min_length 1k; gzip_buffers 16 4k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on;upstream squidserv {
server 10.1.1.29 weight=1 max_fails=3 fail_timeout=30s; }upstream phpserv {
server 10.1.1.22 weight=1 max_fails=3 fail_timeout=30s; }server {
listen 80; server_name ;location = /status {
stub_status on; access_log off; }location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)?$ {
proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass ; } location ~ .*\.php$ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass ; } location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass ; }}
}
客户端测试:
hosts: 10.1.1.28 ========================================= 参考: 主服务器群,然后在利用Squid逆向缓存web80端口来加速自己的网站.各大门户网站象163,sina,chinaitlab之类基本都是使用的这种技术,好处是大大的有。比如加速了网络和可以防***(因为他们见到的都是CDN的主机) 这是利用Squid逆向集群模式做的一种应用网络环境:
主服务器群:源Web服务器群 位于公网ip:220.XXX.XXX.X port:80(后台才是WEB的服务器)
注: 要保证TCP80,UDP3130在防火墙上是开的(供icp_port通讯使用,多台Squid集群才会用到)
全国各地分服务器:A服务器公网IP111.xxx.xxx.x
B服务器公网ip112.xxx.xxx.x
注: 要保证TCP80,UDP3130在防火墙上是开的(供icp_port通讯使用,多台Squid集群才会用到)
……………………
需要解决的问题:全国的所有用户,无论是电信,还是网通,都能速度很好的打开网站
实施
1、分别在主服务器群和全国各地分服务器的三台服务器安装Squid,不会安装的请直接关闭本网页。
2、分别配置Squid,这里只重点叙述Squid集群配置要点。
主服务器群Squid的配置:
http_port 220.XXX.XXX.X:80 vhost vport #让Squid监听本机ip的80端口
icp_port 3130 #多台squid通信使用
cache_peer “内网web服务器的地址” parent 80 0 no-query originserver no-digest name=cache0 #设置源Web服务器群的ip和端口
cache_peer 220.XXX.XXX.X sibling 80 3130 name=cache1 #让远程的squid连接本地Squid工作在sibling模式并指定其端口
cache_peer 111.xxx.xxx.x sibling 80 3130 name=cache2 #A服务器
cache_peer 112.xxx.xxx.x sibling 80 3130 name=cache3 #B服务器
cache_peer_domain cache0 #配置本机squid允许接受访问的域名
acl Safe_ports port 80
acl Safe_ports port 3130 #允许以上端口的代理
全国各地分服务器Squid的配置:
A服务器:
http_port 111.xxx.xxx.x:80 vhost vport
icp_port 3130
cache_peer 220.xxx.xxx.x parent 81 0 no-query originserver no-digest name=cache0 #设置主服务器群Web服务器为源服务器
cache_peer 111.xxx.xxx.x sibling 80 3130 name=cache1
cache_peer 220.xxx.xxx.x sibling 80 3130 name=cache2
cache_peer 112.xxx.xxx.x sibling 80 3130 name=cache3
cache_peer_domain cache0
acl Safe_ports port 80
acl Safe_ports port 3130
B服务器:
http_port 112.xxx.xxx.x:80 vhost vport
icp_port 3130
cache_peer 220.xxx.xxx.x parent 80 0 no-query originserver no-digest name=cache0
cache_peer 112.xxx.xxx.x sibling 80 3130 name=cache1
cache_peer 220.xxx.xxx.x sibling 80 3130 name=cache2
cache_peer 111.xxx.xxx.x sibling 80 3130 name=cache3
cache_peer_domain cache0
acl Safe_ports port 80
acl Safe_ports port 3130
虽然配置好了但是如何让电信和网通的用户能有选择的访问两个不同镜像呢?这个请各位自己查相关的资料,要不到申请双线,电信网通的转发服务注:下面看看cache_peer的参数
通过squid.conf配置文件中的cache_peer选项来配置代理服务器阵
列,通过其他的选项来控制选择代理伙伴的方法。Cache_peer的使用格式如下: cache_peer hostname type http_port icp_port 共有5个选项可以配置: 1. hostname:指被请求的同级子代理服务器或父代理服务器。可以用主机名或ip地址表示; 2. type:指明hostname的类型,是同级子代理服务器还是父代理服务器,也即parent(父) 还是 sibling(子); 3. http_port:hostname的监听端口; 4. icp_port:hostname上的ICP监听端口,对于不支持ICP协议的可指定7; 5. options:可以包含一个或多个关键字。 Options可能的关键字有: 1. proxy-only:指明从peer得到的数据在本地不进行缓存,缺省地,squid是要缓存这部分数据的; 2. weight=n:用于你有多个peer的情况,这时如果多于一个以上的peer拥有你请求的数据时,squid通过计算每个peer的ICP响应时间来 决定其weight的值,然后squid向其中拥有最大weight的peer发出ICP请求。也即weight值越大,其优先级越高。当然你也可以手工 指定其weight值; 3. no-query:不向该peer发送ICP请求。如果该peer不可用时,可以使用该选项; 4. Default:有点象路由表中的缺省路由,该peer将被用作最后的尝试手段。当你只有一个父代理服务器并且其不支持ICP协议时,可以使用default和 no-query选项让所有请求都发送到该父代理服务器; 5.login=user:password:当你的父代理服务器要求用户认证时可以使用该选项来进行认证。curl -I
=====================================
# tcpdump -n tcp port 80 -vv -w squid.cap
# yum install wireshark.i386 wireshark-gnome.i386 -y
ab -c 2000 -n 20000