nginx反向代理+nginx缓存功能及squid缓存架构实现
nginx的反向代理
首先根据nginx的web安装方式来安装nginx的反向代理(略)
upstream模块的调度算法:
①. 定义轮询调度算法-rr-默认调度算法
②. 定义权重调度算法-wrr
③. 定义静态调度算法-ip_hash
④. 定义最小的连接数-least_conn
一个实现nginx反向代理的简单实例:
#编辑nginx.conf配置文件如下:
[root@linux-node1 conf]# vim nginx.conf
worker_processes 2;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream servers_pools {
server 192.168.80.172 max_fails=2 fail_timeout=10s weight=5;
server 192.168.80.173 max_fails=2 fail_timeout=10s weight=5;
}
include extra/www.conf;
include extra/bbs.conf;
}
#创建www.goser.com域名的配置文件如下:
[root@linux-node1 conf]# vim extra/www.conf
server {
listen 80;
server_name www.goser.com;
access_log logs/www-access.log main;
location / {
proxy_pass http://servers_pools;
include proxy.conf;
}
}
#www.conf文件引用一个通用的配置文件proxy.conf,配置如下:
#proxy_set_header Host $host根据代理向后请求携带客户端请求的域名
#proxy_set_header X-Forwarded-For $remote_addr代理携带客户端访问的ip
[root@linux-node1 conf]# vim proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
upsrteam部分参数说明
ip_hash参数说明;
每个请求按客户端 IP的 hash结果分配,当新的请求到达时,先将其客户端 IP通过哈希算法哈希出一个值,在随后的客户端请求中,客户IP的咍希值只要相同,就会被分配至同一台服务器,该调度算法可以解决动态网页的session共享问题,但有时会导致请求分配不均,即无法保证1:1的负载均衡,因为在国内大多数公司都是NAT上网横式,多个客户端会对应_个外部IP ,所以,这些客户端都会被分配到同一节点服务器,从而导致请求分配不均。
upstream servers_pools {
ip_hash;
server 192.168.80.172 max_fails=2 fail_timeout=10s;
server 192.168.80.173 max_fails=2 fail_timeout=10s;
}
least_conn 参数
看谁闲,谁闲发送给谁。least_conn算法会根据后端节点的连接数来决定分配情况,哪个机器连接数少就分发。
fair 参数
此算法会根据后端节点服务器的响应时间来分配请求,响应时间短的优先分配。这是更加智能的调度算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衝,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx本身不支持fair调度算法,如果需要使用这种调度算法,必须下载Nginx的相关模块upstream_fair。
upstream servers_pools {
fair;
server 192.168.80.172 max_fails=2 fail_timeout=10s;
server 192.168.80.173 max_fails=2 fail_timeout=10s;
}
proxy_next_uptream 参数
当nginx接收后端服务器返回proxy_next_upstream 参数定义的状态码时,会将这个请求转发给正常工作的后端服务器,例如500,502,503,504,此参数可以提升用户的访问体验。
根据用户访问url反向代理到后端web服务
比如web服务站点有upload功能,那么根据用户url中是否有upload让nginx反向代理判断是代理到哪个web服务
[root@linux-node1 conf]# vim nginx.conf
worker_processes 2;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream servers_pools {
server 192.168.80.172 max_fails=2 fail_timeout=10s weight=5;
server 192.168.80.173 max_fails=2 fail_timeout=10s weight=5;
}
upstream upload_pools {
server 192.168.80.172 max_fails=2 fail_timeout=10s weight=5;
}
include extra/www.conf;
include extra/bbs.conf;
}
#www.conf内容如下:
[root@linux-node1 conf]# vim extra/www.conf
server {
listen 80;
server_name www.goser.com;
access_log logs/www-access.log main;
location / {
proxy_pass http://servers_pools;
include proxy.conf;
}
location /upload {
proxy_pass http://upload_pools;
include proxy.conf;
}
}
根据客户端浏览器实现反向代理的web定位
企业中开发的web服务有可能对某些浏览器不支持,我们可以通过反向代理的方式,将客户使用的浏览器访问web站点的时候定位到某些web服务上,展现给客户提示更换浏览器或升级浏览器等操作。
[root@linux-node1 conf]# vim nginx.conf
worker_processes 2;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream servers_pools {
server 192.168.80.172 max_fails=2 fail_timeout=10s weight=5;
server 192.168.80.173 max_fails=2 fail_timeout=10s weight=5;
}
upstream upload_pools {
server 192.168.80.172 max_fails=2 fail_timeout=10s weight=5;
}
upstream static_pools {
server 192.168.80.173 max_fails=2 fail_timeout=10s weight=5;
}
include extra/www.conf;
include extra/bbs.conf;
}
#www.conf配置如下:
[root@linux-node1 conf]# cat extra/www.conf
server {
listen 80;
server_name www.goser.com;
access_log logs/www-access.log main;
location / {
if ($http_user_agent ~* "MSIE|Firefox")
{
proxy_pass http://static_pools;
}
if ($http_user_agent ~* "Chrome")
{
proxy_pass http://upload_pools;
}
proxy_pass http://servers_pools;
include proxy.conf;
}
}
根据客户端使用的是移动端还是PC端设备让nginx反向代理定位到哪些web服务上
[root@linux-node1 conf]# vim nginx.conf
worker_processes 2;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream servers_pools {
server 192.168.80.172 max_fails=2 fail_timeout=10s weight=5;
server 192.168.80.173 max_fails=2 fail_timeout=10s weight=5;
}
upstream andriod_pools {
server 192.168.80.172 max_fails=2 fail_timeout=10s weight=5;
}
upstream iphone_pools {
server 192.168.80.173 max_fails=2 fail_timeout=10s weight=5;
}
include extra/www.conf;
include extra/bbs.conf;
}
#www.conf配置文件配置如下:
[root@linux-node1 conf]# vim extra/www.conf
server {
listen 80;
server_name www.goser.com;
access_log logs/www-access.log main;
location / {
if ($http_user_agent ~* "andriod")
{
proxy_pass http://andriod_pools;
}
if ($http_user_agent ~* "ipad|iphone")
{
proxy_pass http://iphone_pools;
}
proxy_pass http://servers_pools;
include proxy.conf;
}
}
根据客户访问的静态地址的扩展名让nginx反向代理定位到哪些web服务上
[root@linux-node1 conf]# vim nginx.conf
worker_processes 2;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream servers_pools {
server 192.168.80.172 max_fails=2 fail_timeout=10s weight=5;
server 192.168.80.173 max_fails=2 fail_timeout=10s weight=5;
}
upstream images_pools {
server 192.168.80.172 max_fails=2 fail_timeout=10s weight=5;
}
include extra/www.conf;
include extra/bbs.conf;
}
#www.conf配置文件配置如下:
[root@linux-node1 conf]# cat extra/www.conf
server {
listen 80;
server_name www.goser.com;
access_log logs/www-access.log main;
location / {
proxy_pass http://servers_pools;
include proxy.conf;
}
location ~.*.(gif|jpg|png|bmp|swf|jpeg|css|js)$ {
proxy_pass http://images_pools;
include proxy.conf;
}
}
通过安装nginx_upstream_check_module模块监控后端服务器健康状况
#下载nginx_upstream_check_module模块
#方式一下载:git方式
[root@linux-node1 ~]# cd /tools
[root@linux-node1 tools]# git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
#方式二:wget
wget [root@linux-node1 tools]# https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
#添加nginx_upstream_check_module模块,功能为给nginx打补丁
[root@linux-node1 tools]# cd nginx-1.12.2
[root@linux-node1 nginx-1.12.2]# patch -p1 < /tools/nginx_upstream_check_module/check_1.12.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h
#对nginx程序重新编译
[root@linux-node1 nginx-1.12.2]# ./configure --add-module=/tools/nginx_upstream_check_module/ --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
#覆盖编译后的二进制文件
[root@linux-node1 ~]# /application/nginx/sbin/nginx -s stop
[root@linux-node1 ~]# cd /application/nginx/sbin/
[root@linux-node1 sbin]# cp nginx nginx.bak
[root@linux-node1 nginx-1.12.2]# cd /application/nginx-1.12.2/
[root@linux-node1 nginx-1.12.2]# cp objs/nginx /application/nginx/sbin/
[root@linux-node1 nginx-1.12.2]# /application/nginx/sbin/nginx
#修改配置文件,让nginx_upstream_check_module模块生效
[root@linux-node1 conf]# vim nginx.conf
worker_processes 2;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream servers_pools {
server 192.168.80.172 max_fails=2 fail_timeout=10s weight=5;
server 192.168.80.173 max_fails=2 fail_timeout=10s weight=5;
check interval=5000 rise=2 fall=3 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
include extra/www.conf;
include extra/bbs.conf;
}
#修改www.conf文件
[root@linux-node1 conf]# vim extra/www.conf
server {
listen 80;
server_name www.goser.com;
access_log logs/www-access.log main;
location / {
proxy_pass http://servers_pools;
include proxy.conf;
}
location /nstatus {
check_status;
access_log off;
}
}
上面配置的意思是,对name这个负载均衡条目中的所有节点,每个5秒检测一次,请求2次正常则标记 realserver状态为up,如果检测 3 次都失败,则标记 realserver的状态为down,超时时间为1秒。
check指令只能出现在upstream中:
interval: 向后端发送的健康检查包的间隔。fall: 如果连续失败次数达到fall_count,服务器就被认为是down。rise: 如果连续成功次数达到rise_count,服务器就被认为是up。timeout: 后端健康请求的超时时间。default_down: 设定初始时服务器的状态,如果是true,就说明默认是down的,如果是false,就是up的。默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后才会被认为是健康的。type:健康检查包的类型,现在支持以下多种类型:tcp http ajp ssl_hello mysql fastcgiport: 指定后端服务器的检查端口。你可以指定不同于真实服务的后端服务器的端口,比如后端提供的是443端口的应用,你可以去检查80端口的状态来判断后端健康状况。默认是0,表示跟后端server提供真实服务的端口一样。该选项出现于Tengine-1.4.0。
如果 type 为 http ,你还可以使用check_http_send来配置http监控检查包发送的请求内容,为了减少传输数据量,推荐采用 HEAD 方法。当采用长连接进行健康检查时,需在该指令中添加keep-alive请求头,如: HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n 。当采用 GET 方法的情况下,请求uri的size不宜过大,确保可以在1个interval内传输完成,否则会被健康检查模块视为后端服务器或网络异常。
通过浏览器访问测试nginx_upstream_check_module是否生效,关闭一个后端服务器

利用nginx的反向代理做后端web站点缓存服务
Nginx从0.7.48版本开始,支持了类似Squid的缓存功能。这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302这样的非200状态码。虽然目前官方的Nginx Web缓存服务只能为指定URL或状态码设置过期时间,不支持类似Squid的PURGE指令,手动清除指定缓存页面,但是,通过一个第三方的Nginx模块,可以清除指定URL的缓存。
Nginx的Web缓存服务主要由proxy_cache相关指令集和fastcgi_cache相关指令集构成,前者用于反向代理时,对后端内容源服务器进行缓存,后者主要用于对FastCGI的动态程序进行缓存。两者的功能基本上一样。
最新的Nginx 0.8.32版本,proxy_cache和fastcgi_cache已经比较完善,加上第三方的ngx_cache_purge模块(用于清除指定URL的缓存),已经可以完全取代Squid。我们已经在生产环境使用了 Nginx 的 proxy_cache 缓存功能超过两个月,十分稳定,速度不逊于 Squid。
在功能上,Nginx已经具备Squid所拥有的Web缓存加速功能、清除指定URL缓存的功能。而在性能上,Nginx对多核CPU的利用,胜过Squid不少。另外,在反向代理、负载均衡、健康检查、后端服务器故障转移、Rewrite重写、易用性上,Nginx也比Squid强大得多。这使得一台Nginx可以同时作为“负载均衡服务器”与“Web缓存服务器”来使用。
下面就以已部署好的nginx反向代理来做后端web站点的缓存服务
[root@linux-node1 tools]# wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz [root@linux-node1 tools]# tar xf ngx_cache_purge-2.3.tar.gz
重新编译nginx
#首先停止nginx服务 [root@linux-node1 tools]# /application/nginx/sbin/nginx -s stop #开始重新部署ngnix,添加purge模块 [root@linux-node1 nginx-1.12.2]# ./configure --add-module=/tools/nginx_upstream_check_module/ --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --add-module=/tools/ngx_cache_purge-2.3 [root@linux-node1 nginx-1.12.2]# make && make install
修改nginx.conf配置,实现缓存功能:主要添加如下参数:
#设置proxy_temp_path和proxy_cache_path指定的路径必须在同一分区 proxy_temp_path /data0/proxy_temp_dir; #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。 proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; #开启cache功能,设置cache的缓存区 proxy_cache cache_one; #对不同的HTTP状态码设置不同的缓存时间 proxy_cache_valid 200 304 12h; #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内 proxy_cache_key $host$uri$is_args$args; expires 1d;
[root@linux-node1 conf]# cat nginx.conf worker_processes 2; error_log logs/error.log error; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; proxy_temp_path /data0/proxy_temp_dir; proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g; upstream servers_pools { server 192.168.80.172 max_fails=2 fail_timeout=10s weight=5; server 192.168.80.173 max_fails=2 fail_timeout=10s weight=5; check interval=5000 rise=2 fall=3 timeout=1000 type=http; } include extra/www.conf; include extra/bbs.conf; } #修改www.conf配置文件如下,添加cache功能 [root@linux-node1 conf]# vim extra/www.conf server { listen 80; server_name www.goser.com; access_log logs/www-access.log main; location / { proxy_pass http://servers_pools; include proxy.conf; #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。 proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; proxy_cache_valid 200 304 12h; proxy_cache_key $host$uri$is_args$args; expires 1d; } location /nstatus { check_status; access_log off; } #用于清除缓存,假设一个URL为http://192.168.80.171/server-error.jpg,通过访问http://192.168.80.171/purge/server-error.jpg就可以清除该URL的缓存。 location ~ /purge(/.*) { #设置只允许指定的IP或IP段才可以清除URL缓存。 allow 127.0.0.1; allow 192.168.0.0/16; deny all; proxy_cache_purge cache_one $host$1$is_args$args; } #扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。 location ~ .*\.(php|jsp|cgi)?$ { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://backend_server; } }
创建proxy_temp_path目录并启动nginx
[root@linux-node1 conf]# mkdir -p /data0/proxy_temp_dir [root@linux-node1 conf]# /application/nginx/sbin/nginx -t [root@linux-node1 conf]# /application/nginx/sbin/nginx
验证nginx缓存功能是否正常使用,访问一个uri (www.goser.com/upload)然后在proxy_cache_path目录中查看是否有缓存的内容

查看proxy_cache_path目录的内容如下:

清除www.goser.com/upload缓存,也就是清除缓存目录下的对应的key。使用www.goser.com/purge/upload来清除www.goser.com/upload这个url对应的key
下面是清除www.goser.com/upload对应的缓存key
[root@linux-node1 ~]# ls /data0/proxy_cache_dir/5/5f/ 4df191dbf3c7ec84555d7d5da9c745f5
使用www.goser.com/purge/upload来清除这个key

再次查看www.goser.com/upload对应的缓存key,此key便被清除了
[root@linux-node1 ~]# ls /data0/proxy_cache_dir/5/5f/ [root@linux-node1 ~]#
通过squid缓存功能做web的静态页面缓存
squid实现的web缓存架构图如下:

squid简介
Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据。当一个用户想要下载一个主页时,可以向Squid 发出一个申请,要Squid 代替其进行下载,然后Squid 连接所申请网站并请求该主页,接着把该主页传给用户同时保留一个备份,当别的用户申请同样的页面时,Squid 把保存的备份立即传给用户,使用户觉得速度相当快。Squid 可以代理HTTP、FTP、GOPHER、SSL和WAIS等协议并且Squid 可以自动地进行处理,可以根据自己的需要设置Squid,使之过滤掉不想要的东西。
官方地址:http://www.squid-cache.org/
参考文档:http://www.squid-cache.org/Doc/config/
配置squid缓存服务
首先了解下squid的配置文件的参数说明
http_port 3128 //设置监听的IP与端口号
cache_mem 64 MB //额外提供给squid使用的内存,squid的内存总占用为 X * 10+15+“cache_mem”,其中X为squid的cache占用的容量(以GB为单位),
//比如下面的cache大小是100M,即0.1GB,则内存总占用为0.1*10+15+64=80M,推荐大小为物理内存的1/3-1/2或更多。
maximum_object_size 4 MB //设置squid磁盘缓存最大文件,超过4M的文件不保存到硬盘
minimum_object_size 0 KB //设置squid磁盘缓存最小文件
maximum_object_size_in_memory 4096 KB //设置squid内存缓存最大文件,超过4M的文件不保存到内存
cache_dir ufs /var/spool/squid 100 16 256 //定义squid的cache存放路径 、cache目录容量(单位M)、一级缓存目录数量、二级缓存目录数量
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st "%{Referer}>h" "%{User-Agent}&>h" %Ss:%Sh //log文件日志格式
access_log /var/log/squid/access.log combined //log文件存放路径和日志格式
cache_log /var/log/squid/cache.log //设置缓存日志
logfile_rotate 60 //log轮循 60天
cache_swap_high 95 //cache目录使用量大于95%时,开始清理旧的cache
cache_swap_low 90 //cache目录清理到90%时停止。
acl localnet src 192.168.0.0/16 //定义本地网段
http_access allow localnet //允许本地网段使用
http_access deny all //拒绝所有
visible_hostname linux-node4.goser.com //主机名
cache_mgr goser1@163.com //管理员邮箱
offline_mode on //表示不会后台验证资源是否有效 直接从缓存中获取
cache_peer 192.168.80.172 parent 80 0 originserver no-query round-robin //指定后端真实的web服务
安装squid服务
[root@linux-node4 ~]# yum -y install squid [root@linux-node4 ~]# chkconfig --level 35 squid on [root@linux-node4 ~]# squid -v Squid Cache: Version 3.1.23
配置squid的配置文件,修改squid监听端口改成非3128的端口,因3128端口作为squid的普通代理和透明代理来使用,而我们这里是让squid做缓存和反向代理来使用,当客户端访问squid的服务的时候,如果squid缓存上没有缓存客户需要的数据,那么squid就会反向代理向后端的web集群拉取数据给客户端并缓存到本地一份。
#首先备份原始的squid.conf配置文件
[root@linux-node4 ~]# cd /etc/squid/
[root@linux-node4 squid]# mv squid.conf squid.conf.ori
#创建一个自定义的squid.conf配置文件
[root@linux-node4 squid]# vim squid.conf
http_port 80 accel vhost
cache_mem 64 MB
maximum_object_size 4 MB
minimum_object_size 0 KB
maximum_object_size_in_memory 4096 KB
cache_dir ufs /var/spool/squid 100 16 256
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
access_log /var/log/squid/access.log combined
cache_log /var/log/squid/cache.log
logfile_rotate 60
cache_swap_high 95
cache_swap_low 90
forwarded_for on
cache_effective_user squid
cache_effective_group squid
tcp_recv_bufsize 65535 bytes
# acl localnet src 192.168.0.0/16
# http_access allow localnet
# http_access deny all
http_access allow all
icp_access allow all
visible_hostname linux-node4.goser.com
cache_mgr goser1@163.com
cache_peer 192.168.80.172 parent 80 0 originserver no-query round-robin
cache_peer 192.168.80.173 parent 80 0 originserver no-query round-robin
offline_mode on
另一台squid服务的配置文件squid.conf和上面一样,只需要更改visible_hostname linux-node5.goser.com
启动两台squid服务
#初始化squid [root@linux-node4 squid]# squid -z [root@linux-node4 squid]# /etc/init.d/squid start #如果对squid配置文件更改后,可以使用下面命令使squid重新加载配置文件,而不需要重启squid [root@linux-node4 squid]# squid -krec #等同于squid -k reconfigure #squid其他命名,比如重载,重启,启动 停止等 /etc/inint.d/squid start |stop |restart|reload
接下来修改nginx的反向代理配置,当客户端访问nginx反向代理vip的时候,根据客户端访问的域名抛向upstream模块指定squid的地址
#nginx配置文件如下
[root@linux-node1 conf]# vim nginx.conf
worker_processes 2;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream servers_pools {
server 192.168.80.174;
server 192.168.80.175;
}
include extra/www.conf;
include extra/bbs.conf;
}
#www.conf配置文件如下:
[root@linux-node1 conf]# vim extra/www.conf
server {
listen 80;
server_name www.goser.com;
access_log logs/www-access.log main;
location / {
proxy_pass http://servers_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
#重载nginx服务
[root@linux-node1 conf]# /application/nginx/sbin/nginx -s reload
这样通过浏览器访问站点www.goser.com域名的时候就会将请求发到squid缓存服务上,squid缓存服务根据本地是否已有客户需要的缓存数据确定是否要向后找web集群获取。
部署一致性hash
既然squid能实现web静态页面的缓存功能,那么客户端获取资源的速度就会大大的提升,生产场景中如果有台squid服务器宕机,那么通过squid缓存的数据就会重新分布在各个squid服务上,这样就会导致squid缓存服务的震荡,如何才能缓解这个问题呢,这就需要用到一致性hash算法,通过一致性hash,nginx反向代理端就会根据客户端上一次访问哪个缓存服务器而保持客户端每次访问url指向同一个squid服务器,即使莫一台squid服务宕机了,也仅仅是这个宕机的squid服务器上面缓存的数据重新分布到其他的squid服务上,而不至于整个squid服务都要重新分布缓存数据,这样就可以减少缓存数据的震荡。
在nginx反向代理上部署一致性hash
#下载第三方的一致性hash模块ngx_http_consistent_hash
[root@linux-node1 tools]# wget https://github.com/replay/ngx_http_consistent_hash/archive/master.zip
#解压这个一致性hash模块
[root@linux-node1 tools]# unzip ngx_http_consistent_hash-master.zip
#首先停止ngixn服务
[root@linux-node1 tools]# /application/nginx/sbin/nginx -s stop
#重新编译ngixn,添加一致性hash模块
[root@linux-node1 ~]# cd /tools/nginx-1.12.2
[root@linux-node1 nginx-1.12.2]# ./configure --add-module=/tools/nginx_upstream_check_module/ --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --add-module=/tools/ngx_http_consistent_hash-master
[root@linux-node1 nginx-1.12.2]# make && make install
#修改ngixn配置文件,在配置文件中添加一致性hash参数consistent_hash $request_uri;
[root@linux-node1 conf]# vim nginx.conf
worker_processes 2;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream servers_pools {
consistent_hash $request_uri;
server 192.168.80.174;
server 192.168.80.175;
}
include extra/www.conf;
include extra/bbs.conf;
}
#启动ngixn服务
[root@linux-node1 conf]# /application/nginx/sbin/nginx
接下来验证一致性hash:通过在squid服务上tail -f /var/log/squid/access.log 来观察访问日志。比如访问www.goser.com,重复刷新,只在一台squid服务上有访问日志变化,其他squid服务日志无变化。
基于nginx反向代理上游服务的动态上下线
在自动化部署代码的时候,我们经常在部署代码前后对集群中的后端web-server做动态加入或移除集群操作。如果我们前端用的是nginx反向代理的话,而且需要做上游服务的动态上下线操作,那么就需要在nginx反向代理端部署可以实现上游服务动态上下线的模块
参考文章为:https://www.cnblogs.com/beyondbit/p/6063132.html https://github.com/cubicdaiya/ngx_dynamic_upstream
https://github.com/firstep/grayscale https://github.com/openresty/lua-upstream-nginx-module
浙公网安备 33010602011771号