目录
一、动静分离
动态资源 与 静态资源 请求
两种方式:
动态资源代理
静态资源代理
案例:
二、Nginx集群 (高可用)
1、引言
2、安装keepalived
主从配置
主服务器
keepalived.conf
在/usr/local/src 添加检测脚本nginx_check.sh 检测脚本
从服务器
keepalived.conf
nginx_check.sh 检测脚本 【不变】
一、动静分离
Nginx的并发能力公式:
worker_processes * worker_connections / 4|2 = Nginx最终的并发能力
动态资源需要 /4,静态资源需要 /2.
Nginx通过动静分离,来提升Nginx的并发能力,更快的给用户响应。
动态资源 与 静态资源 请求
两种方式:
Nginx的并发能力公式:
worker_processes * worker_connections / 4|2 = Nginx最终的并发能力
动态资源需要 /4,静态资源需要 /2.
Nginx通过动静分离,来提升Nginx的并发能力,更快的给用户响应。
两种方式:
一种是纯粹把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;
另外一种方法就是动态跟静态文件混合在一起发布,通过nginx来分开。
动态资源代理
#配置如下
location / {
proxy_pass 路径;
}
静态资源代理
#配置如下location /{
root静态资源路径;
index默认访问路径下的什么资源;
autoindex on;#表示展示静态资源全的全部内容,以列表的形式展开。
}
案例:
#配置如下location /{
root静态资源路径;
index默认访问路径下的什么资源;
autoindex on;#表示展示静态资源全的全部内容,以列表的形式展开。
}
案例:
二、Nginx集群 (高可用)
1、引言
集群:将同一个业务,部署在多个服务器上,对外提供相同的业务
单点故障,避免nginx的宕机,导致整个程序的崩溃
准备多台Nginx。
准备keepalived软件,监听nginx的健康情况
准备haproxy,提供一个虚拟的路径,统一的去接收用户得请求。
2、安装keepalived
cd /usr
yum install keepalived -y
cd /usr
yum install keepalived -y
查看是否安装成功
rpm -aq keepalived
查看配置文件位置
[root@gh ~]# cd /etc [root@gh etc]# cd keepalived [root@gh keepalived]# [root@gh keepalived]# ls keepalived.conf
主从配置
(1) 修改 /etc/ keepalived /keepalived.conf 配置文件
(2)在/user / local / src 添加检测脚本
(3)启动nginx 和 keepalived
./nginx 关闭 ./nginx -s stop
systemctl start keepalived.service
查看是否启动成功 ps- ef | grep keepalived
主服务器
keepalived.conf
//global_defs 全局配置
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
// router路由 LVS_DEVEL 访问到主机 vi /etc/hosts 中添加主机的名字
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
//检测脚本和权重的参数 script 脚本文件
Evrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh" #(检测脚本执行的间隔) 路径
interval 2
weight 2
}
//虚拟ip的配置
vrrp_instance VI_1 {
state MASTER # 备份服务器上 将 MASTER 改为 BACKUP
interface eth0 // 网卡 查看网卡 ifconfig
virtual_router_id 51 # 主、备机 的 virtual_router_id 必须相同
priority 100 # 主、备机去不同的优先级 主机值大,备份机值小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16 // VRRP H 虚拟地址
192.168.200.17
192.168.200.18
}
}
//global_defs 全局配置
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
// router路由 LVS_DEVEL 访问到主机 vi /etc/hosts 中添加主机的名字
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
//检测脚本和权重的参数 script 脚本文件
Evrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh" #(检测脚本执行的间隔) 路径
interval 2
weight 2
}
//虚拟ip的配置
vrrp_instance VI_1 {
state MASTER # 备份服务器上 将 MASTER 改为 BACKUP
interface eth0 // 网卡 查看网卡 ifconfig
virtual_router_id 51 # 主、备机 的 virtual_router_id 必须相同
priority 100 # 主、备机去不同的优先级 主机值大,备份机值小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16 // VRRP H 虚拟地址
192.168.200.17
192.168.200.18
}
}
在/usr/local/src 添加检测脚本nginx_check.sh 检测脚本
#!/bin/bash2
A='ps -C nginx -no-header |wc -1'
if [ $A -eq 0 ];then
/usr/1ocal/nginx/sbin/nginx
sleep 2
if [ 'ps -C nginx --no-header |wc -1' -eq 0 ];then
killall keepalived
fi
fi
从服务器
keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
//检测脚本和权重的参数
Evrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh" #(检测脚本执行的间隔) 路径
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 备份服务器上 将 MASTER 改为 BACKUP
interface eth0 // 网卡 查看网卡 ifconfig
virtual_router_id 51 # 主、备机 的 virtual_router_id 必须相同
priority 90 # 主、备机去不同的优先级 主机值大,备份机值小
advert_int 1 # 心跳 1s
authentication {
auth_type PASS //校验方式 密码 1111
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16 // VRRP H 虚拟地址
192.168.200.17
192.168.200.18
}
}
nginx_check.sh 检测脚本 【不变】
#!/bin/bash2
A='ps -C nginx -no-header |wc -1'
if [ $A -eq 0 ];then
/usr/1ocal/nginx/sbin/nginx //nginx 启动脚本的位置
sleep 2
if [ 'ps -C nginx --no-header |wc -1' -eq 0 ];then
killall keepalived // 所有的都杀掉 主 挂 从 上
fi
fi
keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
//检测脚本和权重的参数
Evrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh" #(检测脚本执行的间隔) 路径
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 备份服务器上 将 MASTER 改为 BACKUP
interface eth0 // 网卡 查看网卡 ifconfig
virtual_router_id 51 # 主、备机 的 virtual_router_id 必须相同
priority 90 # 主、备机去不同的优先级 主机值大,备份机值小
advert_int 1 # 心跳 1s
authentication {
auth_type PASS //校验方式 密码 1111
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16 // VRRP H 虚拟地址
192.168.200.17
192.168.200.18
}
}
nginx_check.sh 检测脚本 【不变】
#!/bin/bash2
A='ps -C nginx -no-header |wc -1'
if [ $A -eq 0 ];then
/usr/1ocal/nginx/sbin/nginx //nginx 启动脚本的位置
sleep 2
if [ 'ps -C nginx --no-header |wc -1' -eq 0 ];then
killall keepalived // 所有的都杀掉 主 挂 从 上
fi
fi
#!/bin/bash2 A='ps -C nginx -no-header |wc -1' if [ $A -eq 0 ];then /usr/1ocal/nginx/sbin/nginx //nginx 启动脚本的位置 sleep 2 if [ 'ps -C nginx --no-header |wc -1' -eq 0 ];then killall keepalived // 所有的都杀掉 主 挂 从 上 fi fi



