阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

载均衡集群之Nginx

133次阅读
没有评论

共计 8728 个字符,预计需要花费 22 分钟才能阅读完成。

前言:

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 服务器的压力, 提高响应速度.

载均衡集群之 Nginx

2、Nginx 如何对后端服务器做健康监测的,以及 Nginx 在做反向代理时对后端 web 服务器的健康状态监测:由于 Nginx 在做 web 的反向代理的时候是基于轮询的方式对后端 web Server 请求的, 并且不能对后端 web Server 做健康监测, 如果刚好请求到挂掉的那台 Web Server 你懂得;Nginx 服务器健康状态监测:如果后端服务器挂掉了的话,Nginx 会在自己的服务器列表中将其删除;

如果后端服务器能够正常工作了,Nginx 会在自己的服务列表中添加此 web 服务器;此功能就是服务器的健康状态监测

载均衡集群之 Nginx

在此架构中, 无法做到 HA,并且相邻阶段之间的 web 服务器无法支持会话同步.session 同步 不知真正意义上的集群, 只是负载均衡集群 并非高可用 ……

什么是服务器 session 同步:如果 session 不同步的时候:想象一个场景, 你访问淘宝网购物的时候,将许多商品加入到购物车, 一刷新,如果不支持 session 同步的话, 购物车中的商品全就没啦!因为用户刷新, 会再次发送请求,给 Nginx 反向代理服务器, 由于 Nginx 是基于轮询的方式代理用户请求后端的 web Server,所以有可能第二次请求无法发送到同一台 web Server. 才会导致:如果 web 不支持 session 同步的话, 购物车中的商品全就没啦!

能够支持 sessiont 同步的服务器, 会在自己的内存当中为用户的 session 创建一个缓冲区,里边专门存储用户的认证信息的.

客户端的 session 跟浏览器紧紧相关 cookie 你懂得..

载均衡集群之 Nginx

编译安装 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

Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里

相关阅读

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 搭建视频点播服务器(仿真专业流媒体软件)http://www.linuxidc.com/Linux/2012-08/69151.htm

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;

    #    }

    #}

}

前言:

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 服务器的压力, 提高响应速度.

载均衡集群之 Nginx

2、Nginx 如何对后端服务器做健康监测的,以及 Nginx 在做反向代理时对后端 web 服务器的健康状态监测:由于 Nginx 在做 web 的反向代理的时候是基于轮询的方式对后端 web Server 请求的, 并且不能对后端 web Server 做健康监测, 如果刚好请求到挂掉的那台 Web Server 你懂得;Nginx 服务器健康状态监测:如果后端服务器挂掉了的话,Nginx 会在自己的服务器列表中将其删除;

如果后端服务器能够正常工作了,Nginx 会在自己的服务列表中添加此 web 服务器;此功能就是服务器的健康状态监测

载均衡集群之 Nginx

在此架构中, 无法做到 HA,并且相邻阶段之间的 web 服务器无法支持会话同步.session 同步 不知真正意义上的集群, 只是负载均衡集群 并非高可用 ……

什么是服务器 session 同步:如果 session 不同步的时候:想象一个场景, 你访问淘宝网购物的时候,将许多商品加入到购物车, 一刷新,如果不支持 session 同步的话, 购物车中的商品全就没啦!因为用户刷新, 会再次发送请求,给 Nginx 反向代理服务器, 由于 Nginx 是基于轮询的方式代理用户请求后端的 web Server,所以有可能第二次请求无法发送到同一台 web Server. 才会导致:如果 web 不支持 session 同步的话, 购物车中的商品全就没啦!

能够支持 sessiont 同步的服务器, 会在自己的内存当中为用户的 session 创建一个缓冲区,里边专门存储用户的认证信息的.

客户端的 session 跟浏览器紧紧相关 cookie 你懂得..

载均衡集群之 Nginx

编译安装 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

Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里

相关阅读

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 搭建视频点播服务器(仿真专业流媒体软件)http://www.linuxidc.com/Linux/2012-08/69151.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计8728字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中