nginx
1.安装nginx
yum install nginx -y
systemctl restart nginx
systemctl enable nginx
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
2.反向代理
(1)实例1
nginx服务器 | 172.25.250.9 |
httpd服务器 | 172.25.250.10 |
①nginx服务器
vim /etc/nginx/nginx.conf
# location 处添加 proxy_pass http://172.25.250.10:80;
location / {
proxy_pass http://172.25.250.10:80;
}
②httpd服务器
vim /var/www/html/index.html
this is 172.25.250.10 index.html
重启服务
systemctl restart nginx
③检验代理
windows物理机访问nginx服务器(172.25.250.9)
(2)实例2
nginx服务器 | 172.25.250.9 |
httpd服务器 | 172.25.250.10 |
httpd服务器 | 172.25.250.11 |
①nginx服务器
vim /etc/nginx/nginx.conf
# 新增一个server
server {
listen 9001;
server_name _;
location ~ /agent1/ {
proxy_pass http://172.25.250.10:80;
}
location ~ /agent2/ {
proxy_pass http://172.25.250.11:80;
}
}
②httpd 服务器1
ip 172.25.250.10
mkdir /var/www/html/agent1
vim /var/www/html/agent1/index1.html
# 写入
172.25.250.10 hello agent1 !
③httpd 服务器2
ip 172.25.250.11
mkdir /var/www/html/agent2
vim /var/www/html/agent1/index2.html
# 写入
172.25.250.11 hello agent2 !
④检验代理
3.负载均衡
nginx服务器 | 172.25.250.9 |
httpd服务器 | 172.25.250.10 |
httpd服务器 | 172.25.250.11 |
(1)新增负载均衡规则
vim /etc/nginx/nginx.conf
# 新增负载均衡规则
upstream myserver {
server 172.25.250.10:80;
server 172.25.250.11:80;
}
(2)添加反向代理参数
vim /etc/nginx/nginx.conf
# location 处添加 proxy_pass http://myserver;
location / {
proxy_pass http://myserver;
}
(3)检验
172.25.250.10
vim /var/www/html/index.html
# 写入
ip is 172.25.250.10
172.25.250.11
vim /var/www/html/index.html
# 写入
ip is 172.25.250.11
请求代理服务器172.25.250.9进行检验
第一次刷新
第二次刷新
(4)nginx负载均衡策略
①轮询(默认)策略
每个请求按照时间顺序逐一分配到不同的服务器中,如果后端服务器down掉,则自动剔除
②权重策略(weight)
权重,weight,默认为1,权重越高被分配的客户请求就越多
在upstream中添加
# 被分配到172.25.250.11的客户请求要比被分配到172.25.250.10的多出一倍
upstream myserver {
server 172.25.250.10:80 weight=5;
server 172.25.250.11:80 weight=10;
}
③ip_hash策略
使每个客户请求按ip的hash结果分配固定的访问一个服务器,以解决session问题
# 添加 ip_hash;
upstream myserver {
ip_hash;
server 172.25.250.10:80;
server 172.25.250.11:80;
}
④fair方式
按照后端服务器的响应时间分配,越短越优先分配
# 添加 fair;
upstream myserver {
server 172.25.250.10:80;
server 172.25.250.11:80;
fair;
}
4.高可用(keepalived)
(1)高可用理解
由于以上配置中httpd服务器宕机有提供相同服务的服务器继续提供服务,但是nginx服务器只有一台,若nginx服务器宕机则请求无法转发到httpd服务器
所以需增加一台nginx从服务器,当主服务器宕机后自动顶替主服务器进行请求转发功能
两台nginx服务器,一主一从
nginx服务器都需安装keepalived,keepalived作为路由作用,通过脚本检测当前的nginx是否存活,若宕机则切换到从nginx服务器
一个对外的虚拟ip,用于绑定两台nginx服务器
(2) 实例1
①准备工作
虚拟ip | 172.25.250.111 |
nginx主服务器 | 172.25.250.9 |
nginx从服务器 | 172.25.250.99 |
httpd服务器 | 172.25.250.10 |
httpd服务器 | 172.25.250.11 |
②主/从服务器安装nginx
yum install nginx -y
③主/从服务器安装keepalived
yum install keeplived-y
④配置keepalived
vim /etc/keepalived/keepalived.conf
# 修改
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 172.25.250.9 # 此处修改
smtp_connect_timeout 30
router_id 172.25.250.9 # 此处修改为主机的ip
vrrp_skip_check_adv_addr
#vrrp_strict # 注释此行
vrrp_garp_interval 0
vrrp_gna_interval 0
}
# 修改
vrrp_instance VI_1 {
state MASTER # 从服务器上改为 BACKUP
interface ens33 # 绑定网卡的名称
virtual_router_id 51 # 主从服务器id值需相同
priority 100 # 主备机优先级,主服务器需比从服务器大
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.250.111 # 虚拟ip
}
}
⑤检验
访问虚拟ip 172.25.250.111
down掉主服务器看是否可以继续访问
systemctl stop keepalived.service
systemctl stop nginx.service