Nginx学习笔记 - 新

跟着 https://www.bilibili.com/video/BV1yS4y1N76R 视频学的
安装教程 nginx环境搭建

通过不同域名相同端口访问不同页面

首先添加hosts,映射下域名到装nginx的主机IP地址,我这里是虚拟机,且没有买域名就自己本地这样玩
在这里插入图片描述

进入nginx安装目录,打开nginx.conf,添加俩虚拟主机server

	#代表如果是www.sb.com:81访问的,则会访问/usr/local/nginx/www/www/www.html页面
    server {
        listen       81; #监听81端口
        server_name  www.sb.com;#通过域名www.sb.com访问

        location / {
            root	www/www;#开头没斜杠,代表相对路径,即nginx根目录下的www/www目录
           	index	www.html;#index 的作用就是当没有访问任何文件时,则默认访问 index.html
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
    
	#代表如果是sss.sb.com:81访问的,则会访问/usr/local/nginx/www/sss/sss.html页面
    server {
        listen       81;
        server_name  sss.sb.com;

        location / {
            root	www/sss;
            index sss.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

配置完重启服务生效:systemctl reload nginx
访问,如果中文乱码按照这个来nginx乱码问题文章解决
在这里插入图片描述
在这里插入图片描述

反向代理 - 请求外网或内网主机

使用 proxy_pass 配置

到外网

比如访问nginx的ip:192.168.80.131,会重定向到百度,浏览器地址也会调到百度

location / {
                proxy_pass http://www.baidu.com/; 表示页面重定向到百度
                index  index.html index.htm;
}

到内网其他主机

比如又开了一台nginx,IP是192.168.80.132
我想从192.168.80.131访问到192.168.80.132

    location / {
                proxy_pass http://192.168.80.132;
                index  index.html index.htm;
            }

在这里插入图片描述

负载均衡

示例

使用upstream + proxy_pass 配置组合,默认轮询配置的几个负载server

worker_processes  1;

events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
        
    keepalive_timeout  65;

    upstream myserver{
    
    
        server 192.168.80.132;
        server 192.168.80.133;
    }

    server {
    
    
        charset utf-8; #解决乱码,多个server每个都要加
        listen       80;
        server_name  localhost;

        location / {
    
    
            proxy_pass http://myserver;
            index  index.html index.htm;
        }

    error_page   500 502 503 504  /50x.html;
        location = /50x.html {
    
    
            root   html;
        }
    }
}

权重

权重数值可以是任意正整数,Nginx会根据给定的权重数值将请求分配给后端服务器

    upstream myserver{
    
    
        server 192.168.80.132 weight=2;#2/17概率
        server 192.168.80.133 weight=10;10/17概率
	    server 192.168.80.134 weight=5;5/17概率
    }

动静分离 – 访问静态文件

/usr/webTest下创建static.html文件
配置location

 location /webTest/ {
            root /usr/;
            index  index.html index.htm;
 }

访问 http://192.168.80.131/webTest/static.html
在这里插入图片描述
解析过程:解析到url中的/webTest/和/webTest/匹配,url中的/webTest/被替换成root的/usr/+location的/webTest/即/usr/webTest/,最后成http://192.168.80.131/usr/webTest/static.html

Keepalived

一文带你浅入浅出Keepalived
一句话就是,为了nginx集群用的,一个主节点,剩下都是从节点,分配一个虚拟IP到各个节点上,每个节点的虚拟IP都是一致的,用户访问这个虚拟IP(浮动IP),就是访问主nginx,当主nginx挂了,从节点就会升级成主节点,保证不会挂,用户也不用换IP访问,当然这个需要写个脚本赖判断主节点nginx是否挂了
在这里插入图片描述

Nginx+Keepalived案例

我这里不知道为什么nginx挂了,脚本没执行导致无法杀死Keepalived没法切换到其他节点,但是手动杀死Keepalived可以切换到另一台
yum安装Keepalived

yum install keepalived

配置
使用yum安装后配置文件在

/etc/keepalived/keepalived.conf

keepalived中vrrp_script,track_script,notify的使用方法

第一台机子(主)

keepalived.conf

! Configuration File for keepalived

global_defs {
    
    
   router_id LB_200 #运行 keepalived 服务器的标识,集群的用同一个
 script_user root                #指定运行脚本的用户名和组。默认使用用户的默认组。如未指定,默认为keepalived_script 用户,如无此用户,则使用root
    enable_script_security                        #如过路径为非root可写,不要配置脚本为root用户执行。
}

vrrp_instance VI_200 {
    
    
    state MASTER #设置 lvs 的状态,MASTER 和 BACKUP 两种,必须大写
    interface ens33 #网卡名称,使用命令ip addr 查出第二个的网卡名
    virtual_router_id 51 #设置虚拟路由标示,这个标示是一个数字,同一个 vrrp 实例使用唯一标示
    priority 100 #定义优先级,数字越大优先级越高,在一个 vrrp——instance 下,master 的优先级必须大于 backup
    advert_int 1 #设定 master 与 backup 负载均衡器之间同步检查的时间间隔,单位是秒
	nopreempt
    authentication {
    
     #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信
        auth_type PASS
        auth_pass 1111 
    }
    virtual_ipaddress {
    
     #设置虚拟 ip 地址,可以设置多个,每行一个
        192.168.80.200
    }
    track_script {
    
      #添加跟踪(执行脚本)
    	nginx_check
    }
}

vrrp_script nginx_check 
{
    
    #括号得另起一行否则报错
  script "/etc/keepalived/nginx_check.sh" #心跳执行的脚本,检测nginx是否启动
  interval 5 #检测脚本执行的间隔,单位是)
}


}


配置完执行systemctl start keepalived启动
然后查看状态如下

[root@admin conf]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 一 2023-04-24 22:52:33 CST; 34s ago
  Process: 1433 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1434 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─1434 /usr/sbin/keepalived -D
           ├─1435 /usr/sbin/keepalived -D
           └─1436 /usr/sbin/keepalived -D

424 22:52:41 admin Keepalived_vrrp[1436]: Sending gratuitous ARP on ens33 for 192.168.80.200
424 22:52:41 admin Keepalived_vrrp[1436]: Sending gratuitous ARP on ens33 for 192.168.80.200
424 22:52:41 admin Keepalived_vrrp[1436]: Sending gratuitous ARP on ens33 for 192.168.80.200
424 22:52:41 admin Keepalived_vrrp[1436]: Sending gratuitous ARP on ens33 for 192.168.80.200
424 22:52:46 admin Keepalived_vrrp[1436]: Sending gratuitous ARP on ens33 for 192.168.80.200
424 22:52:46 admin Keepalived_vrrp[1436]: VRRP_Instance(VI_200) Sending/queueing gratuitous ARPs on ens33 for 192.168.80.200
424 22:52:46 admin Keepalived_vrrp[1436]: Sending gratuitous ARP on ens33 for 192.168.80.200
424 22:52:46 admin Keepalived_vrrp[1436]: Sending gratuitous ARP on ens33 for 192.168.80.200
424 22:52:46 admin Keepalived_vrrp[1436]: Sending gratuitous ARP on ens33 for 192.168.80.200
424 22:52:46 admin Keepalived_vrrp[1436]: Sending gratuitous ARP on ens33 for 192.168.80.200

第二台机子(备)

keepalived.conf

! Configuration File for keepalived

global_defs {
    
    
   router_id LB_200 #运行 keepalived 服务器的标识,集群的用同一个
    script_user root                #指定运行脚本的用户名和组。默认使用用户的默认组。如未指定,默认为keepalived_script 用户,如无此用户,则使用root
    enable_script_security                        #如过路径为非root可写,不要配置脚本为root用户执行。
}

vrrp_instance VI_200 {
    
    
    state BACKUP #设置 lvs 的状态,MASTER 和 BACKUP 两种,必须大写
    interface ens33 #网卡名称,使用命令ip addr 查出第二个的网卡名
    virtual_router_id 51 #设置虚拟路由标示,这个标示是一个数字,同一个 vrrp 实例使用唯一标示
    priority 99 #定义优先级,数字越大优先级越高,在一个 vrrp——instance 下,master 的优先级必须大于 backup
    advert_int 1 #设定 master 与 backup 负载均衡器之间同步检查的时间间隔,单位是秒
nopreempt
mcast_src_ip 192.168.80.132
    authentication {
    
     #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信
        auth_type PASS
        auth_pass 1111 
    }
    virtual_ipaddress {
    
     #设置虚拟 ip 地址,可以设置多个,每行一个
        192.168.80.200
    }
    track_script {
    
      #添加跟踪(执行脚本)
    	nginx_check
    }
}
vrrp_script nginx_check 
{
    
    #括号得另起一行否则报错
  script "/etc/keepalived/nginx_check.sh" #心跳执行的脚本,检测nginx是否启动
  interval 5 #检测脚本执行的间隔,单位是)
}


配置完执行systemctl start keepalived启动
然后查看状态如下

[root@admin conf]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2023-04-25 22:45:57 CST; 2s ago
  Process: 1177 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1178 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─1178 /usr/sbin/keepalived -D
           ├─1179 /usr/sbin/keepalived -D
           └─1180 /usr/sbin/keepalived -D

425 22:45:57 admin Keepalived_vrrp[1180]: Registering Kernel netlink reflector
425 22:45:57 admin Keepalived_vrrp[1180]: Registering Kernel netlink command channel
425 22:45:57 admin Keepalived_vrrp[1180]: Registering gratuitous ARP shared channel
425 22:45:57 admin Keepalived_vrrp[1180]: Opening file '/etc/keepalived/keepalived.conf'.
425 22:45:57 admin Keepalived_vrrp[1180]: VRRP_Instance(VI_200) removing protocol VIPs.
425 22:45:57 admin Keepalived_vrrp[1180]: Using LinkWatch kernel netlink reflector...
425 22:45:57 admin Keepalived_vrrp[1180]: VRRP_Instance(VI_200) Entering BACKUP STATE
425 22:45:57 admin Keepalived_vrrp[1180]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
425 22:45:57 admin Keepalived_vrrp[1180]: VRRP_Script(nginx_check) succeeded
425 22:45:57 admin Keepalived_healthcheckers[1179]: Opening file '/etc/keepalived/keepalived.conf'.

两台都配置的nginx脚本nginx_check.sh,在/etc/keepalived

#!/bin/bash
#上面这句注释不可删除
#检查是否有nginx相关的进程
A=`ps -C nginx --no-header |wc -l`
#如果没有
if [ $A -eq 0 ];then
# 重启nginx,延迟2秒
	systemctl start nginx
	sleep 2
	# 重新检查是否有nginx相关的进程
	if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
	# 仍然没有nginx相关的进程,杀死当前keepalived,切换到备用机
	systemctl stop keepalived
    fi
fi

写完,手动执行下,看看有没有报错sh nginx_check.sh

手动杀死主keepalived,自动切换到备keepalived

systemctl stop keepalived

杀死后在访问vip 192.168.80.200,就会发现访问的是备的nginx

出现的问题

Unsafe permissions found for script ‘/etc/keepalived/bin/recover.sh’.

chmod 744 /etc/keepalived/nginx_check.sh

猜你喜欢

转载自blog.csdn.net/Fire_Sky_Ho/article/details/130141347
今日推荐