您的位置:首页 > 运维架构 > 反向代理

Squid反向代理

2015-11-10 16:31 465 查看
普通代理方式是代理内部网络用户访问Internet上服务器的连接请求,客户端必须指定代理服务器,并将本来要直接发送到internet上服务器的连接请求发送给代理服务器处理。
反向代理方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外表现问一台服务器。

1、反向代理流程说明:

squid做为反向代理服务器,通常工作在一个服务器集群的前端,在用户端看来,squid服务器就是他所要访问的服务器,而实际意义上squid只是接受用户的请求,同时将用户请求转发给内网真正的web服务器,如果squid本身有用户要访问的内容,则squid直接将内容返回给用户,而无需再去后端的服务器中请求
2、squid反向代理生产案例
很多大型门户网站,经常使用squid作为服务器的反向cache,提高了服务器的访问性能,这些cache服务器组有效减轻了后端web服务器的负载,并且提高了访问速度,在某种程度上保护了后端的web服务器。

3、squid反向代理的优点:
1.节约带宽(对CDN),自己在机房部署squid反向代理,不能节约带宽
2.提升用户体验
3.减轻服务器压力,减少WEB,存储,数据库的压力
公司购买CDN了,企业还需要在机房搭建squid反向代理
答:需求不大,有CDN了,静态业务命中到百分之90左右了,所以需求不大
在穿透CDN请求比较多的时候,超过web服务及其他存储,数据库的压力时候,需要部署squid

4、squid 反向代理如何获取数据更新
squid反向代理一般只缓存可缓存的数据(比如html页面,js,css和图片等静态数据),而一些CGI脚本程序或ASP、JSP、PHP之类的动态程序默认不缓存,它根据从WEB服务器返回的HTTP头标记来缓冲静态页面,有四个重要的HTTP头标记。

Last-Modified:告诉反向代理页面什么时间被修改;
Expries:告诉反向代理页面什么时间应该从缓冲区中删除
Cache-Control:告诉方向代理页面是否应该被缓存;
Pragma:用来包含实现特定的指令,最常见的是Pragma:no-Cache

优先级对比(no-cache,expries,max-age)参考
经验:在squid中Cache-Control:no-cache>expries>refresh_pattern>Last-Modified.考前面的最重要,前面的失效了,后面的基本也就失效了。

另外安装一台web服务器,作为源站。也就是RS
web 服务器ip是 10.0.0.4 端口为80
yum install httpd*
/etc/init.d/iptables stop
/etc/init.d/httpd start
netstat -lnt|grep 80
lsof -i :80
echo 111>/var/www/html/index.html
====================================================================
[root@WEB html]# ps -ef |grep http
root      1798     1  0 14:47 ?        00:00:00 /usr/sbin/httpd
apache    1800  1798  0 14:47 ?        00:00:00 /usr/sbin/httpd
apache    1801  1798  0 14:47 ?        00:00:00 /usr/sbin/httpd
apache    1802  1798  0 14:47 ?        00:00:00 /usr/sbin/httpd
apache    1803  1798  0 14:47 ?        00:00:00 /usr/sbin/httpd
apache    1804  1798  0 14:47 ?        00:00:00 /usr/sbin/httpd
apache    1805  1798  0 14:47 ?        00:00:00 /usr/sbin/httpd
apache    1806  1798  0 14:47 ?        00:00:00 /usr/sbin/httpd
apache    1807  1798  0 14:47 ?        00:00:00 /usr/sbin/httpd
root      1811  1743  0 14:49 pts/0    00:00:00 grep http
[root@WEB html]# netstat -lntup|grep 80
tcp        0      0 :::80                       :::*                        LISTEN      1798/httpd
[root@WEB html]# lsof -i :80
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd   1798   root    4u  IPv6  19044      0t0  TCP *:http (LISTEN)
httpd   1800 apache    4u  IPv6  19044      0t0  TCP *:http (LISTEN)
httpd   1801 apache    4u  IPv6  19044      0t0  TCP *:http (LISTEN)
httpd   1802 apache    4u  IPv6  19044      0t0  TCP *:http (LISTEN)
httpd   1803 apache    4u  IPv6  19044      0t0  TCP *:http (LISTEN)
httpd   1804 apache    4u  IPv6  19044      0t0  TCP *:http (LISTEN)
httpd   1805 apache    4u  IPv6  19044      0t0  TCP *:http (LISTEN)
httpd   1806 apache    4u  IPv6  19044      0t0  TCP *:http (LISTEN)
httpd   1807 apache    4u  IPv6  19044      0t0  TCP *:http (LISTEN)
[root@WEB html]#
[root@WEB html]# curl 10.0.0.4
111
[root@WEB html]#
====================================================================
在客户端打开检查看看是否正确
上传3张图片到web站点,后缀为jpg的图片
[root@WEB html]# ls
1.jpg  1.png  2.jpg  3.jpg  index.html  K2.png  keep1.png  na1.png  na2.png
[root@WEB html]# http://10.0.0.4/1.jpg http://10.0.0.4/2.jpg http://10.0.0.4/3.jpg[/code] 
在squid服务器(IP:10.0.0.3),设置squid.conf

#首先设置squid支持虚拟主机模式
host_port 8000 accel vhost vport    ##加速模式

#设置squid运行的用户
cache_effective_user squid
cache_effective_group squid
#设置对哪里web做方向代理
cache_peer imag1.lvnian.org      parent 80 0 no-query no-digest max-conn=32 originserver
#cache_peer vi.gis.cttic.cn      parent  80 0 no-query no-digest max-conn 32 originserve
#设置缓存目录(squid吃没吃文件系统类型、缓存目录、目录大小Mbyte、第一层目录的文件数,第二层文件夹的数目)
cache_dir ufs /app/squid/cache 10000 16 256

cache_mem 128 MB
#这是一个优化选项,增加该内存值有利于缓存。应该注意的是:  一般来说如果系统有内存,设置该值为(n/)3M。现在是3G 所以这里1G
cache_swap_low 90                  #最小允许使用swap 90%
cache_swap_high 95                 #最多允许使用swap 95%
maximum_object_size 8192 KB        #设置缓存在硬盘上的文件的最大大小
minimum_object_size 0 KB            #设置缓存在硬盘上的文件的最小大小
maximum_object_size_in_memory 4096 KB#设置缓存在内存上的文件的大小
emulate_httpd_log on
#将使Squid仿照Web服务器的格式创建访问记录。如果希望使用
memory_replacement_policy lru       #缓存算法,保持最近使用的

#设置这些方向代理的ACL,避免squid被恶意用户作为代理
acl my_allow_domain_list dstdomain imag1.lvnian.org
#acl my_allow_domain_list dstdomain vi.gis.cttic.cn
#只允许我们定义的域名my_allow_domain_list
cache_peer_access imag1.lvnian.org vi.gis.cttic.cn

#设置缓存过期时间
refresh_pattern -i \.jpg$ 30 50% 4320 reload-into-ims
refresh_pattern -i \.png$ 30 50% 4320 reload-into-ims
refresh_pattern -i \.gif$ 30 50% 4320 reload-into-ims
上面的意思是:如果某个响应驻留在cache里的时间没有超过这个30分钟最低的限制,那么他不会过期,类似最高限制4320分钟是存活响应的最高时间限制,如果某个响应驻留在你cache里的实际高于这个最高限制,那么它必须被刷新,在最低和最高时间限制之间的响应,会面对squid的最后修改系数(LM-factor)算法,对这样的响应,squid计算响应的年龄和最后修改的系数,然后将他作为百分比值进行比较,如果这个系数超过50%,那么这个响应必须被刷新
###################
squid缓存设置--控制页面的缓存时间
refresh_pattern的作用: 用于确定一个页面进入cache后,它在cache中停留的时间。

refresh_pattern 只对后端没设置Expires过期时间的页面起作用,比如论坛页面;而对类似apache mod_expires 设置过的页面不起作用。
语法:
refresh_pattern [-i] regexp min percent max [options
refresh_pattern -i \.css$ 1440 50% 129600 reload-into-ims
refresh_pattern -i \.xml$ 1440 50% 129600 reload-into-ims
refresh_pattern -i \.html$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.shtml$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.hml$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.jpg$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.png$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.gif$ 1440 90% 129600 ignore-reload
refresh_pattern -i \.bmp$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.js$ 1440 90% 129600 reload-into-ims

#指定hosts 文件
hosts_file /etc/hosts
request_header_max_size 128 KB
ipcache_size 1024
ipcache_low 90
ipcache_high 95


###在hosts中添加相应的解析
cp /etc/hosts /etc/hosts
echo -e "10.0.0.4      imag1.lvnian.org" >>/etc/hosts


##只有缓存完了,不管你在不在线,都会有。哪怕你把客户端端IE删除缓存,把后端的http服务关闭,也一样可以看到数据
offline_mode on


##定义日志格式
logformat squid %{X-Forwarded-For}>h %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st %tr "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
##应用日志格式
access_log /application/squid3.0/var/logs/access.log squid


###可以配置多个缓存目录
cache_dir aufs /data1/cache 94000 64 256
cache_dir aufs /data2/cache 94000 64 256
cache_dir aufs /data3/cache 94000 64 256


完整的配置文件如下:
[root@Squid etc]# cat squid.conf
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
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          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
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 deny all
icp_access allow localnet
icp_access deny all
htcp_access allow localnet
htcp_access deny all
###################################
http_port 80 accel vhost vport
#设置对哪里web做方向代理
cache_peer imag1.lvnian.org  parent 80 0 no-query no-digest max-conn=32 originserver
cache_mem 64 MB
cache_swap_low 90
cache_swap_high 95
maximum_object_size 8192 KB
minimum_object_size 0 KB
maximum_object_size_in_memory 4096 KB
emulate_httpd_log on
memory_replacement_policy lru
###################################
hierarchy_stoplist cgi-bin ?
cache_dir ufs /application/squid3.0/var/cache 100 16 256
access_log /application/squid3.0/var/logs/access.log squid
cache_log /application/squid3.0/var/logs/cache.log
cache_store_log /application/squid3.0/var/logs/store.log
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern (cgi-bin|\?)    0       0%      0
refresh_pattern .               0       20%     4320
cache_mgr 75685538@qq.com
cache_effective_user squid
cache_effective_group squid
visible_hostname img01.etiantian.org
icp_port 3130
coredump_dir /application/squid3.0/var/cache
#######################################
refresh_pattern -i \.css$ 1440 50% 129600 reload-into-ims
refresh_pattern -i \.xml$ 1440 50% 129600 reload-into-ims
refresh_pattern -i \.html$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.shtml$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.htm$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.jpg$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.png$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.gif$ 1440 90% 129600 ignore-reload
refresh_pattern -i \.bmp$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.js$ 1440 90% 129600 reload-into-ims
#指定hosts 文件
hosts_file  /etc/hosts
request_header_max_size 128 KB
ipcache_size 1024
ipcache_low 90
ipcache_high 95
cache_dir ufs /app/squid/cache 10000 16 256
offline_mode on
[root@Squid etc]#

###检查语法
[root@Squid etc]# squid -k parse
2015/09/08 16:54:32| Processing Configuration File: /application/squid3.0/etc/squid.conf (depth 0)
2015/09/08 16:54:32| WARNING: use of 'reload-into-ims' in 'refresh_pattern' violates HTTP
2015/09/08 16:54:32| Initializing https proxy context
[root@Squid etc]#
上面是没错误的
#启动
[root@Squid etc]# /etc/init.d/squid restart
squid restart...                                          [  OK  ]
---------- -------------check ing-----------------------------
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      55119/(squid)
udp        0      0 0.0.0.0:3130                0.0.0.0:*                               55119/(squid)
-----------------------check over-----------------------------
[root@Squid etc]#

#检查命中转态
curl -I -s 10.0.0.3/1.jpg|grep -i x-cache
[root@Squid ~]# curl -I -s 10.0.0.4/1.jpg|grep -i x-cache
X-Cache: HIT from img01.lvnian.org
[root@Squid ~]# curl -I -s 10.0.0.4/2.jpg|grep -i x-cache
X-Cache: HIT from img01.lvnian.org
[root@Squid ~]# curl -I -s 10.0.0.4/3.jpg|grep -i x-cache
X-Cache: HIT from img01.lvnian.org
[root@Squid ~]#
一边用windows浏览器访问下面地址,看能不能正确访问到10.0.0.4后端RS的内容。一边看10.0.0.3squid的日志文件。观察squid的代理情况
http://10.0.0.3/1.jpg http://10.0.0.3/2.jpg http://10.0.0.3/3.jpg[/code] 本文出自 “奋斗吧” 博客,请务必保留此出处http://lvnian.blog.51cto.com/7155281/1711494
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: