Nginx实现 反代并开启缓存+url地址重写+RS健康状态检查的负载均衡

规划图如下

 

nginx反向代理并开启缓存+url重写+带健康检测的负载均衡

1.RS的准备

 方便实验,rpm软件包安装,能使用ip访问网页即可

2.nginx的编译安装

# yum -y install pcre-devel

2.1首先添加用户nginx,实现以之运行nginx服务进程:

# groupadd -r nginx

# useradd -r -g nginx -s /bin/false -M nginx

2.2接着开始编译和安装:

 


  1. # ./configure \ 
  2.   --prefix=/usr \ 
  3.   --sbin-path=/usr/sbin/nginx \ 
  4.   --conf-path=/etc/nginx/nginx.conf \ 
  5.   --error-log-path=/var/log/nginx/error.log \ 
  6.   --http-log-path=/var/log/nginx/access.log \ 
  7.   --pid-path=/var/run/nginx/nginx.pid  \ 
  8.   --lock-path=/var/lock/nginx.lock \ 
  9.   --user=nginx \ 
  10.   --group=nginx \ 
  11.   --with-http_ssl_module \ 
  12.   --with-http_flv_module \ 
  13.   --with-http_stub_status_module \ 
  14.   --with-http_gzip_static_module \ 
  15.   --http-client-body-temp-path=/var/tmp/nginx/client/ \ 
  16.   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ 
  17.   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ 
  18.   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ 
  19.   --http-scgi-temp-path=/var/tmp/nginx/scgi \ 
  20.   --with-pcre 
  21.  
  22. # make && make install 

 

3.反代的实现,和缓存的开启

3.1配置步骤

 

 


  1. #cd /etc/nginx 
  2. # sed -i '/^[[:space:]]*#.*/d' nginx.conf     
  3. # sed -i '/^$/d' nginx.conf 
  4. # vim /etc/nginx/nginx.conf 
  5. worker_processes  1; 
  6. events { 
  7.     worker_connections  1024; 
  8. http { 
  9.     include       mime.types; 
  10.     default_type  application/octet-stream; 
  11.     sendfile        on; 
  12.   keepalive_timeout  65; 
  13.   开启缓存添加如下三行: 
  14.   proxy_cache_path /var/www/cache  levels=1:2 keys_zone=mycache:20m  
  15.   *$1:定义缓存存储目录,手动创建 
  16.   *$2:缓存级别,表示缓存目录的第一级目录是1个字符,第二级目录是2个字符 
  17.   *$3:内核中建立用于缓存缓存数据源数据的空间,查找缓存的时候,先从这个内核空间中找到,缓存数据的源数据,然后再到对应目录中查找缓存。 
  18.   max_size=2048m inactive=60m
  19.   *$1:缓存空间最大值 
  20.   *$2:缓存的数据,60分钟内没有被访问过就删除 
  21.   proxy_temp_path /var/www/cache/tmp; 
  22.   *创建缓存的时候可能生成一些临时文件存放的位置,自动创建 
  23.     server { 
  24.         listen       80; 
  25.         server_name  localhost; 
  26.         
  27.  location / { 
  28.         注释掉下面两行: 
  29.             #root   html; 
  30.             #index  index.html index.htm; 
  31.   添加如下内容: 
  32.   proxy_pass http://192.168.1.104/;     代理哪个web服务器 
  33.   proxy_cache mycache;             内存缓存源数据空间名字,对应我们前面的设定 
  34.   proxy_cache_valid 200 302 60m;     页面返回码为200 302 的缓存60分 
  35.   proxy_cache_valid 404 1m;          页面错误响应吗404缓存时间1分 
  36.         } 
  37.         error_page   500 502 503 504  /50x.html; 
  38.         location = /50x.html { 
  39.             root   html; 
  40.         } 
  41.   } 
  42.   #mkdir /var/www/cache 
  43.   #/usr/sbin/nginx  /etc/nginx/nginx.conf 

 


 

 

3.2验证结果

1)实验之前nginx的页面

 

nginx反向代理并开启缓存+url重写+带健康检测的负载均衡

2)反代192.168.1.104的页面

 

nginx反向代理并开启缓存+url重写+带健康检测的负载均衡

4.url的重写

4.1url重写的格式,写在配置文件中

rewrite regex replacement [flag] 

      $2 Regex:被代替的原URL路径,可以是莫须有的,不存在的,支持正则表达式

      $3 Replacement:用来实现代替的URL路径,必须真实存在的

      $4 Flag:标志位,定义URL重写后进行的操作,有4种,分别是:

             1)last:匹配重写后的URL,再一次对URL重写规则进行匹配,当使用last的需要注意的是如下:

      rewrite   /p_w_picpaths/.*\.jpg   /p_w_picpaths/a.jpg  last;

      这样写的话,将会造成死循环。

 2break:匹配重写URL后,终止匹配,直接使用

 3redirect:临时重定向,返回代码302

 4permanent:永久重定向,返回代码301

4.2简单实现url的重写

配置文件中的配置

 

 


  1. #vim /etc/nginx/nginx.conf    
  2.         location / { 
  3.            root   html; 
  4.            index  index.html index.htm; 
  5.            rewrite /abc  http://192.168.1.104 break ; 
  6. *根下并不创建abc这个目录,对着虚拟目录的访问都重写到192.168.1.104 
  7.         } 
  8.        location /text { 
  9.        rewrite /    http://192.168.1.104 break; 
  10. *在根下创建text目录,对其的访问都重写到192.168.1.104 
  11.        } 
  12. kdir /var/html/text 

 

 

5.nginx实现带健康状态检测的负载均衡

5.1nginx 要能够检测后端nginx的健康状态,需要新的模块,重新编译nginx

5.1.1模块的使用

healthcheck_nginx_upstreams.zip 

 

 


  1. #unzip healthcheck_nginx_upstreams.zip  
  2. # ln -s cep21-healthcheck_nginx_upstreams-16d6ae7  health 
  3. *将解压出来的模块所在目录重命名,查看目录下README文件,了解此模块支持的nginx版本,我使用的这个模块对于nginx1.2.0以上的版本无效。 
  4. #tar xf nginx-1.0.14.tar.gz 
  5. #cd nginx-1.0.14 
  6. 打上补丁: 
  7. # patch -p1 < /root/health/nginx.patch  
  8. patching file src/http/ngx_http_upstream.c 
  9. Hunk #1 succeeded at 4296 (offset 3 lines). 
  10. patching file src/http/ngx_http_upstream.h 
  11. Hunk #1 succeeded at 110 (offset 1 line). 
  12. patching file src/http/ngx_http_upstream_round_robin.c 
  13. Hunk #1 succeeded at 5 (offset 1 line). 
  14. Hunk #3 succeeded at 36 (offset 1 line). 
  15. Hunk #5 succeeded at 390 (offset 1 line). 
  16. Hunk #7 succeeded at 497 (offset 1 line). 
  17. Hunk #9 succeeded at 633 (offset 1 line). 
  18. patching file src/http/ngx_http_upstream_round_robin.h 
  19. Hunk #1 succeeded at 27 (offset 1 line). 

 

5.1.2进行重新编译安装

 

 


  1. # ./configure \ 
  2.   --prefix=/usr \ 
  3.   --sbin-path=/usr/sbin/nginx \ 
  4.   --conf-path=/etc/nginx/nginx.conf \ 
  5.   --error-log-path=/var/log/nginx/error.log \ 
  6.   --http-log-path=/var/log/nginx/access.log \ 
  7.   --pid-path=/var/run/nginx/nginx.pid  \ 
  8.   --lock-path=/var/lock/nginx.lock \ 
  9.   --user=nginx \ 
  10.   --group=nginx \ 
  11.   --with-http_ssl_module \ 
  12.   --with-http_flv_module \ 
  13.   --with-http_stub_status_module \ 
  14.   --with-http_gzip_static_module \ 
  15.   --http-client-body-temp-path=/var/tmp/nginx/client/ \ 
  16.   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ 
  17.   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ 
  18.   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ 
  19.   --http-scgi-temp-path=/var/tmp/nginx/scgi \ 
  20.   --with-pcre \ 
  21.   --add-module=/root/health       指定模块所在的目录 
  22. # make && make install 

 

5.2带健康状态的负载均衡的实现

 

 


  1. #vim /etc/nginx/nginx.conf 
  2. http { 
  3.  
  4.   upstream cluster { 
  5.     server 192.168.1.104 ; 
  6.   server 192.168.1.105; 
  7.   *指定RS,后边可以跟weight N,添加权重,也可以IP:PORT指定端口实现端口映射 
  8.   模块参数的配置: 
  9.   healthcheck_enabled;             
  10.   *启用此模块 
  11.   healthcheck_delay 1000; 
  12.   *对同一台RS两次检测之间的时间间隔,单位毫秒,默认为1000 
  13.   healthcheck_timeout 1000; 
  14.   *进行一次健康检测的超时时间,单位为毫秒,默认值2000 
  15.   healthcheck_failcount 3; 
  16.   *对同一台RS检测成功或失败多少次,才决定其成功或失败,并实现启用和禁用此服务 
  17.   healthcheck_send "GET /.health HTTP/1.0"; 
  18.   *从RS上获得用于检测健康状态的文件,默认只支持http 1.0协议 
  19.   #healthcheck_expected 'I_AM_ALIVE'; 
  20.   *从RS上收到的http body部分的响应内容,如果未设置,则表示从后端服务器收到200状态码即可,这里我们不启用 
  21.     # Optional supervisord module support 
  22.     #supervisord none; 
  23.     #supervisord_inherit_backend_status; 
  24.   } 
  25.   server { 
  26.     listen 80; 
  27.     location / { 
  28.       #proxy_set_header Host $http_host;  
  29.      *开启使用cip作为代理的请求地址,可省略 
  30.       proxy_pass http://cluster; 
  31.      *前面定义的upstream cluster 
  32.       #proxy_connect_timeout 3; 
  33.    *代理连接超时时间,可省略 
  34.     } 
  35.     location /stat { 
  36.       healthcheck_status; 
  37.      *可以通过查看这个目录,参看RS的状态信息 
  38.     } 
  39.   } 
  40. #mkdir /usr/html/stat 

 

5.3.1分别在RS上创建文件,内容中的大小写要和配置文件中的一致

# echo 'ok' > /var/www/html/.health

5.3.2重启服务,进行验证

1)访问192.168.1.103/stat,

2)停止192.168.1.104上的web服务,超过配置文件中时间(超时时间*失败次数)后再进行查看192.168.1.103/stat