配置squid 反向代理提高网站性能

本文旨在介绍 squid 反向代理的工作原理的基础上,指出反向代理技术在提高网站访问速度,增强网站可用性、安全性方面有很好的用途。

现在有许多大型的门户网站都采用 squid 反向代理技术来加速网站的访问速度,可将不同的URL请求分发到后台不同的 WEB 服务器上,同时互联网用户只能看到反向代理服务器的地址,加强了网站的访问安全。

本文在具体的实验环境下,利用 DNS 轮询和 Squid 反向代理技术,实现了网站的负载均衡,从而提高了网站的可用性和可靠性。

1、什么是反向代理?

反向代理服务器又称为 WEB 加速服务器,它位于 WEB 服务器的前端,充当 WEB 服务器的内容缓存器。

反向代理服务器是针对 WEB 服务器设置的,后台 WEB 服务器对互联网用户是透明的,用户只能看到反向代理服务器的地址,不清楚后台 WEB 服务器是如何组织架构的。当互联网用户请求 WEB 服务时,DNS 将请求的域名解析为反向代理服务器的 IP 地址,这样 URL 请求将被发送到反向代理服务器,由反向代理服务器负责处理用户的请求与应答、与后台 WEB 服务器交互。利用反向代理服务器减轻了后台 WEB 服务器的负载,提高了访问速度,同时避免了因用户直接与 WEB 服务器通信带来的安全隐患。

反向代理架构

说明:

客户端请求访问 WEB 服务时,DNS 将访问的域名解析为 Squid 反向代理服务器的 IP 地址,这样客户端的 URL 请求将被发送到反向代理服务器。如果 Squid 反向代理服务器中缓存了该请求的资源,则将该请求的资源直接返回给客户端,否则反向代理服务器将向后台的 WEB 服务器请求资源,然后将请求的应答返回给客户端,同时也将该应答缓存在本地,供下一个请求者使用。

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

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

3、配置实例

本实例的域名是www.lampbo.org,通过DNS的轮询技术,将客户端的请求分发给其中一台 Squid 反向代理服务器处理,如果这台 Squid 缓存了用户的请求资源,则将请求的资源直接返回给用户,否则这台 Squid 将没有缓存的请求根据配置的规则发送给邻居 Squid 和后台的 WEB 服务器处理,这样既减轻后台 WEB 服务器的负载,又提高整个网站的性能和安全性。

<a href="http://www.lampbo.org/wp-content/uploads/2012/09/toplogic.jpg" class="cboxElement" rel="example4" 1846"="" style="text-decoration: initial; color: rgb(1, 150, 227);">实验拓扑图

(1)、配置的系统环境:

  • 一台 DNS 服务器:操作系统 Centos6.3,软件 BIND 9.8,IP 192.168.1.10 ;
  • 三台 Squid 服务器:操作系统 Centos6.3,软件 Squid 3.2.1,相应的 IP 如下:
Squid1:192.168.1.15
Squid2:192.168.1.16 
Squid3:192.168.1.17

三台 WEB 服务器:操作系统 Centos6.3,应用软件 Apache+PHP+Mysql,相应的 IP 地址如下:

webServer1:192.168.100.100/24 
webServer2:192.168.101.100/24
webServer1:192.168.102.100/24

(2)、DNS服务器配置

利用Centos6.3自带的 bind 9.8 。然后针对该系统配置 bind,首先修改 bind 的配置文件 /etc/named.conf,在文件中添加:

zone "lampbo.org"{ 
        type master; 
        file "lampbo.org "; 
 };

在/var/named 目录下添加 lampbo.org文件,该文件的内容如下:

$TTL    3600 
 @       IN      SOA     oracle.lampbo.org. root.lampbo.org  ( 
                                20080807        ; Serial 
                                3600    ; Refresh 
                                900     ; Retry 
                                3600000 ; Expire 
                                3600 )  ; Minimum 
         IN      NS      oracle.lampbo.org. 
 www  IN      A       192.168.100.100 
 www  IN      A       192.168.101.100
 www  IN      A       192.168.102.100

这样当用户请求的时候,DNS 通过轮询机制将 www.lampbo.org的域名解析为 192.168.100.100、192.168.101.100和 192.168.102.100 其中之一。

使用命令service named start 启动bind服务。

用 nslookup www.lampbo.org 测试 bind 服务是否正常运行。

(3)、配置 Squid 服务器

下载squid源码包:squid-3.2.1.tar.bz2

解压:

[root@Centos6 ~]# tar  jxvf squid-3.2.1.tar.bz2

编译:

[root@Centos6 squid-3.2.1]# ./configure --prefix=/usr/local/squid --enable-dlmalloc --enable-debug-cbdata --enable-async-io=100 --with-pthreads --enable-storeio="aufs,diskd,ufs" --enable-removal-policies="heap,lru" --enable-icmp --enable-delay-pools --enable-useragent-log --enable-referer-log --disable-wccp --disable-wccpv2 --enable-kill-parent-hack --enable-arp-acl --enable-snmp --enable-default-err-language=Simplify_Chinese --enable-err-languages="Simplify_Chinese English" --disable-poll --enable-epoll --disable-ident-lookups --disable-internal-dns --enable-truncate --enable-underscores --enable-basic-auth-helpers="NCSA" --enable-stacktrace --with-winbind-auth-challenge --enable-large-cache-files --with-large-files --with-maxfd=65535 --enable-ssl --enable-x-accelerator-vary

[root@Centos6 ~]#make

[root@Centos6 ~]#make install

创建用户

[root@Centos6 ~]#useradd -r -s /sbin/nologin squid

授权

[root@Centos6 ~]# chown -R squid:squid /usr/local/squid/var/cache   #授权给squid
[root@Centos6 ~]# chown -R squid:squid /usr/local/squid/var/logs

编辑squid配置文件:

  1. visible_hostname localhost  #设定 squid 的主机名 , 如无此项 squid 将无法启动   
  2. cache_effective_user squid   
  3. cache_effective_group squid    
  4.   
  5. ### 配置 squid 为加速模式 ###   
  6. http_port 80 accel vhost vport    
  7. icp_port 3130    
  8. ### 配置 squid2、squid3 为其邻居,当squid1在其缓存中没有找到请求的资源时,通过 ICP 查询去其邻居中取得缓存   
  9. cache_peer squid2.lampbo.org sibling 80 3130    
  10. cache_peer squid3.lampbo.org sibling 80 3130    
  11. ##### squid1 的三个父节点,originserver 参数指明是源服务器,round-robin  参数指明 squid 通过轮询方式将请求分发到其中一台父节点;squid 同时会对这些父节点的健康状态进行检查,如果父节点 down 了,那么 squid 会从剩余的 origin 服务器中抓取数据   
  12. cache_peer 192.168.100.100 parent 80 0 no-query originserver round-robin  name=webServer1    
  13. cache_peer 192.168.101.100 parent 80 0 no-query originserver round-robin name=webServer2    
  14. cache_peer 192.168.102.100 parent 80 0 no-query originserver round-robin name=webServer3    
  15. #### 将 www.lampbo.org 域的请求通过 RR 轮询方式转发到三个父节点中的一个   
  16.  cache_peer_domain webServer1 webServer2 webServer3 www.lampbo.org    
  17. ##### 下面是一些访问控制、日志和缓存目录的设置   
  18. cache_log /usr/local/squid/var/logs/cache.log    
  19. cache_access_log /usr/local/squid/var/logs/access.log  
  20. cache_dir aufs /usr/local/squid/var/cache/ 100 16 256   
  21. maximum_object_size 10240 KB      
  22. cache_mem 256 MB     
  23.   
  24. acl localnet src 10.0.0.0/8 # RFC1918 possible internal network   
  25. acl localnet src 172.16.0.0/12  # RFC1918 possible internal network   
  26. acl localnet src 192.168.0.0/16 # RFC1918 possible internal network   
  27. acl localnet src fc00::/7       # RFC 4193 local private network range   
  28. acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines   
  29.   
  30. acl SSL_ports port 443   
  31. acl Safe_ports port 80      # http   
  32. acl Safe_ports port 21      # ftp   
  33. acl Safe_ports port 443     # https   
  34. acl Safe_ports port 70      # gopher   
  35. acl Safe_ports port 210     # wais   
  36. acl Safe_ports port 1025-65535  # unregistered ports   
  37. acl Safe_ports port 280     # http-mgmt   
  38. acl Safe_ports port 488     # gss-http   
  39. acl Safe_ports port 591     # filemaker   
  40. acl Safe_ports port 777     # multiling http   
  41. acl CONNECT method CONNECT  
  42.   
  43.   
  44. # Recommended minimum Access Permission configuration:   
  45.   
  46. # Only allow cachemgr access from localhost   
  47. http_access allow all    
  48. #http_access allow localhost manager   
  49. http_access deny manager   
  50.   
  51. # Deny requests to certain unsafe ports   
  52. http_access deny !Safe_ports   
  53.   
  54. # Deny CONNECT to other than secure SSL ports   
  55. http_access deny CONNECT !SSL_ports   
  56.   
  57. # We strongly recommend the following be uncommented to protect innocent   
  58. # web applications running on the proxy server who think the only   
  59. # one who can access services on "localhost" is a local user   
  60. #http_access deny to_localhost   
  61.   
  62.   
  63. # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS   
  64.   
  65.   
  66. # Example rule allowing access from your local networks.   
  67. # Adapt localnet in the ACL section to list your (internal) IP networks   
  68. # from where browsing should be allowed   
  69. http_access allow localnet   
  70. http_access allow localhost   
  71.   
  72. # And finally deny all other access to this proxy   
  73. http_access deny all   
  74.   
  75. # Squid normally listens to port 3128   
  76. http_port 3128   
  77.   
  78. # Uncomment and adjust the following to add a disk cache directory.   
  79. #cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256   
  80.   
  81. # Leave coredumps in the first cache dir   
  82. coredump_dir /usr/local/squid/var/cache/squid   
  83.   
  84. # Add any of your own refresh_pattern entries above these.   
  85. refresh_pattern ^ftp:       1440    20% 10080   
  86. refresh_pattern ^gopher:    1440    0%  1440   
  87. refresh_pattern -i (/cgi-bin/|\?) 0 0%  0   
  88. refresh_pattern .       0   20% 4320   

保存退出:wq

检查 squid 配置文件正确与否:

[root@Centos6 ~]# /usr/local/squid/bin/squid – k parse

生成缓存目录

[root@Centos6 ~]# /usr/local/squid/bin/squid – z

启动squid:

[root@Centos6 ~]# /usr/local/squid/bin/squid -s

squid2 和 squid3 服务器的配置方法和配置参数和 squid1 基本,配置完成后,分别启动这两个服务器上的 squid 服务。

(4)、测试

测试之前,保证 DNS 服务、三台 squid 服务和三台 web 服务都正常起来。在客户端输入http://www.lampbo.org,则正确的显示该网页。服务器端的响应对客户端是透明的,客户端不知道请求是由哪台 WEB 服务器处理的;而且其中某台 Squid 服务器或 WEB 服务器发生故障,也不影响服务的正常运行。

Squid 是一个开源的软件,利用它的反向代理技术可以提高网站系统的访问速度。本文在真实的网络环境下,利用三台 squid 反向代理服务器加速了网站的性能,同时结合 DNS 轮询技术实现了网站的负载均衡。经过一段时间的测试和试运行,该网站的访问速度和可用性方面都有很大的提高,从未出现过网站服务中断情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于nginx和squid反向代理实验,我可以为您提供一些基本的指导和概念说明。 首先,nginx和squid都是常用的反向代理服务器软件。它们的主要功能是接收客户端请求,并将这些请求转发到后端的服务器上,然后将响应返回给客户端。这种反向代理的架构可以提供负载均衡、缓存、安全性等优势。 在进行实验之前,您需要确保已经安装了nginx和squid。安装方法可以参考官方文档或者相关的教程。 对于nginx,您可以通过修改nginx配置文件来配置反向代理。主要的配置项是`proxy_pass`,它指定了需要转发的后端服务器地址。您可以根据需要添加其他的配置项,如负载均衡策略、缓存设置等。 下面是一个简单的nginx反向代理配置示例: ```nginx server { listen 80; server_name example.com; location / { proxy_pass http://backend_server; } } ``` 在上面的示例中,所有请求都会被转发到`http://backend_server`这个后端服务器上。 对于squid,您可以通过编辑squid配置文件来配置反向代理。主要的配置项是`cache_peer`,它指定了需要转发的后端服务器地址。您可以根据需要添加其他的配置项,如缓存设置、访问控制等。 下面是一个简单的squid反向代理配置示例: ``` http_port 3128 http_access allow all cache_peer backend_server parent 80 0 no-query originserver acl our_sites dstdomain example.com http_access allow our_sites cache_peer_access backend_server allow our_sites ``` 在上面的示例中,squid监听在3128端口,所有请求都会被转发到`backend_server`这个后端服务器上。同时,针对example.com这个域名的请求会被允许访问。 请注意,以上只是简单的示例配置,实际的配置可能还涉及到其他方面的设置,如安全性、性能优化等。 希望以上信息能对您有所帮助!如果您有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值