|
前言:
Nginx自身并没有对后端服务器做健康监测的功能,此功能模块是第三方提供的.哪怕是我们编译安装添加此模块也无法支持,我们必须对Nginx打补丁,然后它才能够支持此功能.尽管Nginx能够对后端web服务器做健康监测了,但是却不能实现高可用,没办法实现HA,HA有一个特点就是后端的每一个节点都能够监测对方服务器的健康状态.节点之间是能够知道彼此的工作状态的
1、Nginx反向代理原理;Nginx在做反向代理的时候,当用户的请求被Nginx服务器接收到,nginx将用户的请求保存至内存当中,然后以自己的身份,向后端的web Server 发送请求,当后端的web Server回应请求之后.nginx查看内存中的用户请求列表,然后依次回应给用户.其实种工作模型类似于LVS-NET模型,由于Nginx本身是基于worker的工作模型,并且它在多道IO模型中是一个进程处理多个用户请求的,避免上下文切换,从而也实现了更多用户资源共享的机制,所以Nginx对内存的使用率非常之小,并且Nginx能够将它从后端web服务器请求的结果缓存在本地,以便日后再收到同样的请求信息时,它会将本地缓存里的内容直接发送给用户,以减少后端web服务器的压力,提高响应速度.
2、Nginx 如何对后端服务器做健康监测的,以及Nginx在做反向代理时对后端web服务器的健康状态监测:由于Nginx在做web的反向代理的时候是基于轮询的方式对后端web Server请求的,并且不能对后端web Server 做健康监测,如果刚好请求到挂掉的那台Web Server 你懂得;Nginx服务器健康状态监测:如果后端服务器挂掉了的话,Nginx会在自己的服务器列表中将其删除;
如果后端服务器能够正常工作了,Nginx会在自己的服务列表中添加此web服务器;此功能就是服务器的健康状态监测
在此架构中,无法做到HA,并且相邻阶段之间的web服务器无法支持会话同步.session 同步 不知真正意义上的集群,只是负载均衡集群 并非高可用......
什么是服务器session同步:如果session不同步的时候:想象一个场景,你访问淘宝网购物的时候,将许多商品加入到购物车,一刷新,如果不支持session同步的话,购物车中的商品全就没啦!因为用户刷新,会再次发送请求,给Nginx反向代理服务器,由于Nginx是基于轮询的方式代理用户请求后端的web Server,所以有可能第二次请求无法发送到同一台web Server.才会导致:如果web不支持session同步的话,购物车中的商品全就没啦!
能够支持sessiont同步的服务器,会在自己的内存当中为用户的session创建一个缓冲区,里边专门存储用户的认证信息的.
客户端的session跟浏览器紧紧相关 cookie 你懂得..
编 译安装Nginx,前面提到过如果想要Nginx能够支持对后端WebSerber做服务器健康监测的功能,必须给Nginx打补丁... 参考网站: http://wiki.nginx.org/HttpHealthcheckModule 下载网站: https://github.com/yaoweibin/nginx_upstream_check_module/releases
# nginx_upstream_check_module-0.1.9.zip
# unzipnginx_upstream_check_module-0.1.9.zip
给Nginx打补丁,并且编译安装Nginx
# patch -p1 < ../nginx_upstream_check_module-0.1.9/check_1.2.6+.patch
./configure \ --prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--add-module=/root/nginx_upstream_check_module-0.1.9
# adding module in /root/nginx_upstream_check_module-0.1.9
# checking for ngx_http_upstream_check_module ... found
# + ngx_http_upstream_check_module was configured
# 出现如上信息就说明成功了
make && makeinstall
拓扑图:
Nginx 配置反向代理并对后端web Server 做健康状态监测
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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 0;
keepalive_timeout 65;
#gzip on;
#proxy_cache_path /nginx/cache levels=1:2 keys_zone=mycache:16m
# inactive=24h max_size=1g;
upstream backend {
server 172.16.251.208:80; # 后端web server 的ip地址
server 172.16.251.244:80;
server 172.16.251.191:80;
healthcheck_enabled; #启用此模块
healthcheck_delay 1000; #对同一台后端服务器两次检测之间的时间间隔,单位毫秒,默认为1000;
healthcheck_timeout 1000; #进行一次健康检测的超时时间,单位为毫秒,默认值2000;
healthcheck_failcount 1; # #对后端服务器检测失败多少次才能确认次服务器是不是挂掉了,相反服务器对后端服务监测成功了就将它写进可用的服务器列表中
healthcheck_expected 'I AM_ALIVE'; # 期望从后端服务器请求什么样的内容如果未设置,则表示从后端服务器收到200状态码即为正确;
healthcheck_send "GET /.health HTTP/1.0"; # 请求的页面在哪呀,使用http get方法请求 协议为1.0
healthcheck_buffer##健康状态检查所使用的buffer空间大小;
}
}
server {
listen 80;
server_name www.nginx.com ;
location / {
proxy_pass http://backend;
proxy_set_header Host $http_host;
proxy_connect_timeout 3;
}
location / {
healcheck_status; # 通过类似stub_status的方式输出检测信息
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 172.16.251.215:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
|
|
|