Nginx一撸到底:安装到负载均衡、双机主备、集群高可用!

wget https://nginx.org/download/nginx-1.21.6.tar.gz  
yum install --downloadonly --downloaddir=/soft/nginx/ gcc-c++yum install --downloadonly --downloaddir=/soft/nginx/ pcre pcre-devel4yum install --downloadonly --downloaddir=/soft/nginx/ zlib zlib-develyum install --downloadonly --downloaddir=/soft/nginx/ openssl openssl-devel
也可yum命令一键安装:
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel  
/usr/local/nginx/sbin/nginx -c conf/nginx.confps -ef| grep nginx

firewall-cmd --zone=public --add-port=80/tcp --permanentfirewall-cmd --reloadfirewall-cmd --zone=public --list-ports

./configure--prefix=/usr/local/nginx--pid-path=/var/run/nginx/nginx.pid--lock-path=/var/lock/nginx.lock--error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log--with-http_gzip_static_module--http-client-body-temp-path=/var/temp/nginx/client--http-proxy-temp-path=/var/temp/nginx/proxy--http-fastcgi-temp-path=/var/temp/nginx/fastcgi--http-uwsgi-temp-path=/var/temp/nginx/uwsgi--http-scgi-temp-path=/var/temp/nginx/scgi--with-http_ssl_module

server {listen 443;server_name www.test.com;# 开启sslssl on;# 配置ssl证书ssl_certificate 1_www.test.com_bundle.crt;# 配置证书秘钥ssl_certificate_key 2_www.test.com.key;# ssl会话cachessl_session_cache shared:SSL:1m;# ssl会话超时时间ssl_session_timeout 5m;# 配置加密套件,写法遵循 openssl 标准ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;ssl_prefer_server_ciphers on;location / {proxy_pass http://tomcats/;index index.html index.htm;}}
3 upstream命令配置参数
slow_start:单位秒,权重在指定时间内从1上升到指定值,不适用于hash负载均衡、随机负载均衡; 如果在 upstream 中只有一台 server,则该参数失效max_conns:限制最大同时连接数 1.11.5之前只能用于商业版down:禁止访问max_fails:表示失败几次,则标记server已宕机,剔除上游服务 默认值1fail_timeout:表示失败的重试时间 默认值10backup:备用机 只有在其他服务器无法访问的时候才能访问到 不适用于hash负载均衡、随机负载均衡


反向代理缓存





prefix:keepalived安装的位置sysconf:keepalived核心配置文件所在位置,固定位置,改成其他位置则keepalived启动不了,/var/log/messages中会报错sysconf:keepalived核心配置文件所在位置,固定位置,改成其他位置则keepalived启动不了,/var/log/messages中会报错
配置中可能会出现警告信息,如下:
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.# 安装libnl/libnl-3依赖yum -y install libnl libnl-devel

在/etc/keepalived/下创建脚本check_nginx_alive_or_not
#!/bin/bashA=`ps -C nginx --no-header |wc -l`# 判断nginx是否宕机,如果宕机了,尝试重启if [ $A -eq 0 ];then/usr/local/nginx/sbin/nginx# 等待一小会再次检查nginx,如果没有启动成功,则停止keepalived,使其启动备用机sleep 3if [ `ps -C nginx --no-header |wc -l` -eq 0 ];thenkillall keepalivedfifi
赋予运行权限
chmod +x /etc/keepalived/check_nginx_alive_or_not.sh
keepalived 主机配置
vim keepalived.conf 编辑keepalived配置文件
global_defs {# 路由id:当前安装keepalived的节点主机标识符,保证全局唯一router_id keep_171}vrrp_instance VI_1 {# 表示状态是MASTER主机还是备用机BACKUPstate MASTER# 该实例绑定的网卡interface ens33# 保证主备节点一致即可virtual_router_id 51# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选priority 100# 主备之间同步检查时间间隔,单位秒advert_int 2# 认证权限密码,防止非法节点进入authentication {auth_type PASSauth_pass 1111}# 虚拟出来的ip,可以有多个(vip)virtual_ipaddress {192.168.1.161}}


双机主备就是配置两台nginx,互相为主备,当主机宕机了,自动启用备机。
修改备机配置
global_defs {router_id keep_172}vrrp_instance VI_1 {# 备用机设置为BACKUPstate BACKUPinterface ens33virtual_router_id 51# 权重低于MASTERpriority 80advert_int 2authentication {auth_type PASS auth_pass 1111}virtual_ipaddress {# 注意:主备两台的vip都是一样的,绑定到同一个vip192.168.110.110}}

global_defs {router_id keep_171}vrrp_instance VI_1 {state MASTER interface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.110.110}}vrrp_instance VI_2 {state BACKUPinterface ens33virtual_router_id 52priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.110.111}}
第二台主机配置如下
global_defs {router_id keep_172}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 51priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.110.110}}vrrp_instance VI_2 {state MASTERinterface ens33virtual_router_id 52priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.110.111}}
重启两台Keepalived
systemctl restart keepalived4 LVS实现Nginx高可用负载均衡
LVS是Linux Virtual Server简称。
LVS+Nginx的好处
1、Nginx接收请求来回,LVS可以只接受不响应2、lvs基于四层负载均衡,工作效率较Nginx的七层负载更高,使用LVS搭建Nginx集群,可以提高性能3、四层负载均衡无法对信息处理,只能通过ip+端口的形式转发,所以需要七成负载进行数据的处理
LVS的三种模式




DEVICE="lo:1"IPADDR=192.168.110.110NETMASK=255.255.255.255NETWORK=127.0.0.0BROADCAST=127.255.255.255ONBOOT="yes"NAME=loopback



-A:添加集群-t:tcp协议ip地址:设定集群的访问ip:也就是LVS的虚拟ip-s:设置负载均衡的算法,rr:表示轮询-p:设置连接持久化的时间,在指定时间内同一个用户的请求会访问到同一个服务器中


LVS的负载均衡算法


global_defs {router_id keep_151}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 41priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.110.110}}#配置集群访问的ip+端口,端口和nginx保持一致virtual_server 192.168.110.110 80{#健康检查的时间,单位:秒delay_loop 6#配置负载均衡的算法,默认的轮询lb_algo rr#设置LVS的模式 NAT|TUN|DRlb-kind DR#设置会话持久化的时间persistence_timeout 5#协议protocol TCP#配置负载均衡的真实服务器,也就是nginx节点的具体的ip地址real_server 192.168.110.111 80{#轮询权重配比weight 1#设置健康检查TCP_CHECK {#检查80端口connect_port 80#超时时间connect_timeout 2#重试次数nb_get_retry 2#重试间隔时间delay_before_retry 3}}real_server 192.168.110.111 80{weight 1TCP_CHECK {connect_port 80connect_timeout 2nb_get_retry 2delay_before_retry 3}}}

global_defs {router_id keep_152}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 41priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.110.110}}#配置集群访问的ip+端口,端口和nginx保持一致virtual_server 192.168.110.110 80{#健康检查的时间,单位:秒delay_loop 6#配置负载均衡的算法,默认的轮询lb_algo rr#设置LVS的模式 NAT|TUN|DRlb-kind DR#设置会话持久化的时间persistence_timeout 5#协议protocol TCP#配置负载均衡的真实服务器,也就是nginx节点的具体的ip地址real_server 192.168.110.111 80{#轮询权重配比weight 1#设置健康检查TCP_CHECK {#检查80端口connect_port 80#超时时间connect_timeout 2#重试次数nb_get_retry 2#重试间隔时间delay_before_retry 3}}real_server 192.168.110.111 80{weight 1TCP_CHECK {connect_port 80connect_timeout 2nb_get_retry 2delay_before_retry 3}}}
 
 
                     
                     
                     
                    