bash
[root@oldboy ~]#yum -y install network-scripts lrzsz tree net-tools ntpdate wget zip
bash#1、优化网卡:
[root@oldboy ~]#cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
PREFIX=24
IPADDR=10.0.0.200
GATEWAY=10.0.0.2
DNS1=223.5.5.5
#2、重启网卡查看ip是否正常
[root@oldboy ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:0f:d0:2e brd ff:ff:ff:ff:ff:ff
inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe0f:d02e/64 scope link
valid_lft forever preferred_lft forever
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:0f:d0:38 brd ff:ff:ff:ff:ff:ff
#3、进入网卡目录拷贝一个配置文件改名为ens36
[root@oldboy /etc/sysconfig/network-scripts]#cp ifcfg-ens33 ifcfg-ens36
#4、修改ens36配置文件
[root@oldboy /etc/sysconfig/network-scripts]#cat ifcfg-ens36
TYPE=Ethernet
BOOTPROTO=none
NAME=ens36
DEVICE=ens36
ONBOOT=yes
PREFIX=24
IPADDR=172.16.1.200
GATEWAY=10.0.0.2
DNS1=223.5.5.5
#5、重启网卡生效
[root@oldboy /etc/sysconfig/network-scripts]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:0f:d0:2e brd ff:ff:ff:ff:ff:ff
inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe0f:d02e/64 scope link
valid_lft forever preferred_lft forever
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:0f:d0:38 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.200/24 brd 172.16.1.255 scope global noprefixroute ens36
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe0f:d038/64 scope link
valid_lft forever preferred_lft forever
bashwget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
bash[root@oldboy ~]#systemctl status firewalld
[root@oldboy ~]#systemctl disable firewalld
bash[root@oldboy ~]#cat /etc/locale.conf
LANG="en_US.UTF-8"
让配置文件生效
[root@oldboy ~]# source /etc/locale.conf
bash*/15 * * * * root ntpdate ntp1.aliyun.com &>/dev/null
bashexport PS1="[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\]\[\e[31;1m\] \w\[\e[0m\]]#"
bash[root@oldboy ~]#umount /tmp
[root@oldboy ~]#systemctl mask tmp.mount
bash[root@oldboy ~]#cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
#SELINUX=disabled
SELINUX=disable
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# ukmls - Multi Level Security protection.
# ukmcs -ukmcs variants of the SELinux policy.
#SELINUXTYPE=targeted
SELINUXTYPE=targeted
# SETLOCALDEFS= Check local definition changes
SETLOCALDEFS=0
bash[root@oldboy ~]#echo '* - nofile 65535 ' >>/etc/security/limits.conf
[root@oldboy ~]#tail -1 /etc/security/limits.conf
* - nofile 65535
bash1.克隆BACKUP
2.修改IP地址
[root@oldboy ~]#cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
PREFIX=24
IPADDR=10.0.0.41
GATEWAY=10.0.0.2
DNS1=223.5.5.5
[root@oldboy ~]#cat /etc/sysconfig/network-scripts/ifcfg-ens36
TYPE=Ethernet
BOOTPROTO=none
NAME=ens36
DEVICE=ens36
ONBOOT=yes
PREFIX=24
IPADDR=172.16.1.41
GATEWAY=10.0.0.2
DNS1=223.5.5.5
3.修改主机名称
[root@oldboy ~]#hostnamectl set-hostname backup
[root@oldboy ~]#cat /etc/hostname
backup
重启网卡生效
[root@backup ~]#systemctl restart network
完成后检查 IP 联网 主机名称
4.做快照
bash作用: 和定时任务相结合定时的备份重要的数据,rsync是可以实现增量备份的工具
bash增量备份
bash全量备份
bash本地模式: 类似cp命令 了解
#第一次拷贝,会把test拷贝到/opt目录下
[root@backup ~]#rsync -avz test /opt
sending incremental file list
test
sent 83 bytes received 35 bytes 236.00 bytes/sec
total size is 0 speedup is 0.00
#第二次拷贝是空的,因为/opt目录下已经有文件了
[root@backup ~]#rsync -avz test /opt
sending incremental file list
sent 43 bytes received 12 bytes 110.00 bytes/sec
total size is 0 speedup is 0.00
bash语法格式: 推送模式(类似上传文件到网盘)
rsync -avz 文件 用户名@主机:路径 #push 推送模式
rsync -avz:命令标准写法
文件:当前本地文件,也就是当前所在的主机
用户名:目标用户名
@:用户名跟主机分隔符
主机:主机名称、主机ip、域名
路径:目标路径
#注意,任何服务器都可以使用推送和拉取的功能。前提是知道对方的密码
#案例1.将本地web01的1.txt拷贝到41服务器的/root目录下
[root@web01 ~]#rsync -avz 1.txt root@10.0.0.41:/root/
The authenticity of host '10.0.0.41 (10.0.0.41)' can't be established.
ECDSA key fingerprint is SHA256:Fe+StTKc+1JoYwNC0wQDHkY4QAuKr+cB3wGKah/fzkM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.41' (ECDSA) to the list of known hosts.
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password: #输入41服务器的root密码
sending incremental file list
1.txt
sent 84 bytes received 35 bytes 18.31 bytes/sec
total size is 0 speedup is 0.00
#查看41服务器文件
[root@backup ~]#ll
total 0
-rw-r--r-- 1 root root 0 Apr 1 14:44 1.txt
#案例2.将本地web01的oldboy目录下的所有文件拷贝到41的家目录
[root@backup ~]#mkdir oldboy
[root@backup ~]#touch oldboy/{1..3}.log
#这种方法会把oldboy目录下的文件拷贝到41服务器的/root下,不包括oldboy目录
[root@web01 ~]#rsync -avz oldboy/ root@10.0.0.41:/root/
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
sending incremental file list
./
1.log
2.log
3.log
sent 207 bytes received 76 bytes 51.45 bytes/sec
total size is 0 speedup is 0.00
#查看41服务器
[root@backup ~]#ll
total 0
-rw-r--r-- 1 root root 0 Apr 1 14:53 1.log
-rw-r--r-- 1 root root 0 Apr 1 14:53 2.log
-rw-r--r-- 1 root root 0 Apr 1 14:53 3.log
#案例3.将本地web01的oldboy目录推送到41服务器的/root目录下
#这种方法会把oldboy目录及以下文件拷贝的/root下
[root@web01 ~]#rsync -avz oldboy root@10.0.0.41:/root/
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
sending incremental file list
oldboy/
oldboy/1.log
oldboy/2.log
oldboy/3.log
sent 224 bytes received 77 bytes 86.00 bytes/sec
total size is 0 speedup is 0.00
#查看41服务器
[root@backup ~]#ll
total 0
drwxr-xr-x 2 root root 45 Apr 1 14:53 oldboy
bash语法结构: 拉取模式(类似网盘下载)
rsync -avz root@10.0.0.41:/etc/hosts 本地的路径
#案例1.将41服务器的hosts文件拷贝到本地的家目录
[root@web01 ~]#rsync -avz root@10.0.0.41:/etc/hosts .
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password: # 需要41root的密码
receiving incremental file list
hosts
sent 43 bytes received 136 bytes 32.55 bytes/sec
total size is 158 speedup is 0.88
#案例2.将41服务器家目录的oldboy目录拷贝到当前目录
[root@web01 ~]#rsync -avz root@10.0.0.41:/root/oldboy .
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
receiving incremental file list
oldboy/
oldboy/1.log
oldboy/2.log
oldboy/3.log
sent 85 bytes received 224 bytes 68.67 bytes/sec
total size is 0 speedup is 0.00
#查看7服务器
[root@web01 ~]#ll
total 0
drwxr-xr-x 2 root root 45 Apr 1 14:53 oldboy
bashrsync [OPTION...] [USER@]HOST:SRC... [DEST]
rsync: 命令
-avz: 命令参数
USER: 用户名称 如果不指定用户则默认使用当前登录用户作为远程的用户。
@: 用户名和主机的分隔符
HOST: 主机 IP地址 域名 主机名称
SRC: 源文件
DEST: 目标路径
#案例1、拿普通用户举例
#一、在41服务器新建普通用户
[root@backup ~]#groupadd -g100 oldboy
groupadd: GID '100' already exists
[root@backup ~]#useradd -u100 -g100 oldboy
[root@backup ~]#id oldboy
uid=100(oldboy) gid=100(users) groups=100(users)
[root@backup ~]#echo "Sdms2022"|passwd --stdin oldboy 设置密码为Sdms2022
Changing password for user oldboy.
passwd: all authentication tokens updated successfully.
#二、将7服务器的oldboy文件放到41的root下,权限被拒绝,因为普通用户无法在root用户下创建文件
[root@web01 ~]#rsync -avz 1.txt oldboy@10.0.0.41:/root
Authorized users only. All activities may be monitored and reported.
oldboy@10.0.0.41's password:
Permission denied, please try again.
oldboy@10.0.0.41's password:
sending incremental file list
1.txt
rsync: mkstemp "/root/.1.txt.Q6Ta6t" failed: Permission denied (13)
#三、将root目录改为用户家目录,成功
[root@web01 ~]#rsync -avz 1.txt oldboy@10.0.0.41:/home/oldboy
Authorized users only. All activities may be monitored and reported.
oldboy@10.0.0.41's password:
sending incremental file list
1.txt
sent 83 bytes received 35 bytes 26.22 bytes/sec
total size is 0 speedup is 0.00
[root@web01 ~]#
#查看41服务器
[root@backup ~]#ll /home/oldboy
total 0
-rw-r--r-- 1 oldboy users 0 Apr 1 15:41 1.txt
#测试“如果不指定用户则默认使用当前登录用户作为远程的用户”,当前7服务器使用root登录,41服务器使用oldboy登录,不指定用户,会默认使用当前登录的用户作为远程的用户
[root@web01 ~]#rsync -avz 2.txt 10.0.0.41:/root/
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password: #默认使用41服务器root的用户
#如果使用oldboy用户,默认使用oldboy作为远程的用户,不管远程有没有这个用户
[oldboy@web01 ~]#rsync -avz 2.txt 10.0.0.41:/root/
The authenticity of host '10.0.0.41 (10.0.0.41)' can't be established.
ECDSA key fingerprint is SHA256:Fe+StTKc+1JoYwNC0wQDHkY4QAuKr+cB3wGKah/fzkM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.41' (ECDSA) to the list of known hosts.
Authorized users only. All activities may be monitored and reported.
oldboy@10.0.0.41's password: #默认使用41服务器oldboy的用户
#案例2、hosts使用域名,将阿里云服务器的hosts文件下载到当前的目录
#一、修改host文件进行域名劫持,将7服务器的www.test.com对应的ip设置为10.0.0.41
[root@web01 ~]#vim /etc/hosts
[root@web01 ~]#ping www.test.com
PING www.test.com (10.0.0.41) 56(84) bytes of data.
#二、将7服务器的的zhang.txt拷贝到41服务器,因为域名劫持,所以www.test.com的IP就是10.0.0.41
[root@web01 ~]#rsync -avz zhang.txt www.test.com:/root/
Authorized users only. All activities may be monitored and reported.
root@www.test.com's password:
sending incremental file list
zhang.txt
sent 88 bytes received 35 bytes 27.33 bytes/sec
total size is 0 speedup is 0.00
#查看41服务器
[root@backup ~]#ll
total 0
-rw-r--r-- 1 root root 0 Apr 1 16:06 zhang.txt
#案例3、HOST作为主机名
#一、修改host文件进行域名劫持,将7服务器的backup对应的ip设置为10.0.0.41
[root@web01 ~]#cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.41 www.test.com backup
#二、将7服务器的的zhang.txt拷贝到41服务器,因为域名劫持,所以backup的IP就是10.0.0.41
[root@web01 ~]#rsync -avz zhang.txt backup:/root
The authenticity of host 'backup (10.0.0.41)' can't be established.
ECDSA key fingerprint is SHA256:Fe+StTKc+1JoYwNC0wQDHkY4QAuKr+cB3wGKah/fzkM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'backup' (ECDSA) to the list of known hosts.
Authorized users only. All activities may be monitored and reported.
root@backup's password:
sending incremental file list
zhang.txt
sent 88 bytes received 35 bytes 18.92 bytes/sec
total size is 0 speedup is 0.00
#查看41服务器
[root@backup ~]#ll
total 0
-rw-r--r-- 1 root root 0 Apr 1 16:06 zhang.txt
#案例3.远程为增量拷贝 拷贝oldboy目录到41服务器的家目录。
#把oldboy目录下1、2、3txt拷贝过去了
[root@web01 ~]#rsync -avz oldboy 10.0.0.41:/root
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
sending incremental file list
oldboy/
oldboy/1.txt
oldboy/2.txt
oldboy/3.txt
sent 224 bytes received 77 bytes 86.00 bytes/sec
total size is 0 speedup is 0.00
#新建4.txt
[root@web01 ~]#touch oldboy/4.txt
#只把4.txt拷贝过去了
[root@web01 ~]#rsync -avz oldboy 10.0.0.41:/root
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
sending incremental file list
oldboy/
oldboy/4.txt
sent 177 bytes received 39 bytes 61.71 bytes/sec
total size is 0 speedup is 0.00
bash其他参数选项:
#参数1: 排除文件
--exclude=文件 # 排除文件
#把oldboy目录及文件拷贝到41服务器root目录下,排除1.txt
[root@web01 ~]#rsync -avz --exclude=1.txt oldboy 10.0.0.41:/root/
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
sending incremental file list
oldboy/
oldboy/2.txt
oldboy/3.txt
oldboy/4.txt
#查看41服务器文件
[root@backup ~]#tree
.
└── oldboy
├── 2.txt
├── 3.txt
└── 4.txt
1 directory, 3 files
sent 232 bytes received 77 bytes 88.29 bytes/sec
total size is 0 speedup is 0.00
#参数2:限制传输的速度
--bwlimit=1m # 限速为1M
#不限速的情况下,P为显示速率
[root@web01 ~]#rsync -avzP test.tar.gz 10.0.0.41:/root/
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
sending incremental file list
test.tar.gz
44,938,940 100% 19.03MB/s 0:00:02 (xfr#1, to-chk=0/1)
sent 44,962,459 bytes received 35 bytes 8,174,998.91 bytes/sec
total size is 44,938,940 speedup is 1.00
#设置限速为1m
[root@web01 ~]#rsync -avzP --bwlimit=1m test.tar.gz 10.0.0.41:/root/
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
sending incremental file list
test.tar.gz
44,938,940 100% 1.00MB/s 0:00:42 (xfr#1, to-chk=0/1)
sent 44,962,459 bytes received 35 bytes 988,186.68 bytes/sec
total size is 44,938,940 speedup is 1.00
#参数3:无差异同步数据 --delete # 谁在前以为为准
我有啥你就得有啥,以我为准
#一、推送目录
[root@web01 ~]#rsync -avz oldboy 10.0.0.41:/root
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
sending incremental file list
oldboy/
oldboy/1.txt
oldboy/2.txt
oldboy/3.txt
oldboy/4.txt
sent 285 bytes received 96 bytes 84.67 bytes/sec
total size is 0 speedup is 0.00
#查看41服务器文件
[root@backup ~]#tree
.
└── oldboy
├── 1.txt
├── 2.txt
├── 3.txt
└── 4.txt
#二、无差异同步
#删除1.txt,2.txt
[root@web01 ~]#rm -rf oldboy/{1..2}.txt
[root@web01 ~]#tree
.
└── oldboy
├── 3.txt
└── 4.txt
#执行无差异同步,因为本机的oldboy目录在前所以以本机为准,本机没有1.txt,2.txt,所以将41服务器的1.txt,2.txt进行了删除操作
[root@web01 ~]#rsync -avz --delete oldboy 10.0.0.41:/root/
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
sending incremental file list
deleting oldboy/2.txt
deleting oldboy/1.txt
sent 108 bytes received 49 bytes 44.86 bytes/sec
total size is 0 speedup is 0.00
#查看41服务器
[root@backup ~]#tree
.
└── oldboy
├── 3.txt
└── 4.txt
#案例,以对方为准,对方有啥我有啥
#将41服务器3.txt进行删除
[root@backup ~]#rm -rf oldboy/3.txt
[root@backup ~]#tree
.
└── oldboy
└── 4.txt
#以41服务器为主进行无差异同步,会将本机7服务器的3.txt进行删除
[root@web01 ~]#rsync -avz --delete 10.0.0.41:/root/oldboy .
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
receiving incremental file list
deleting oldboy/3.txt
oldboy/
sent 28 bytes received 86 bytes 17.54 bytes/sec
total size is 0 speedup is 0.00
[root@web01 ~]#tree
.
└── oldboy
└── 4.txt
1 directory, 1 file
企业案例: --delete
bashC/S架构 客户端/服务端
守护进程持续的在服务端运行。
客户端定时的将数据推送到服务端。
#1.安装服务
[root@backup ~]#yum -y install rsync
#2.配置服务
#查看服务
[root@backup ~]#rpm -qc rsync
/etc/rsyncd.conf
/etc/sysconfig/rsyncd
[root@backup ~]#vim /etc/rsyncd.conf
#进入vim界面,底层输入:set paste再按P会按照复制的格式粘贴
uid = zhang #运行进程的用户,虚拟用户,傀儡用户,存在系统中,不能登录系统运行进程
gid = zhang #运行进程的用户组
port = 873 #监听端口
#------------------------------------------蓝线中间不用改
fake super = yes #无需让rsync以root身份运行允许接受文件的完整属性
use chroot = no #禁锢推送的数据至某个目录,不允许跳出该目录
max connections = 200 #最大连接数
timeout = 600 #超时时间
ignore errors #忽略错误信息
read only = false #对备份的数据可读写
list = false #不允许查看模块信息
#-------------------------------------------蓝线中间不用改
auth users = rsync_backup #定义虚拟用户作为连接认证用户,不存在passwd文件中,只是作为连接登录用户(写在这文件中就可以用)
secrets file = /etc/rsync.passwd #定义rsync服务用户连接认证密码文件路径
log file = /var/log/rsyncd.log
#####################################
[backup] #定义模块信息
comment = welcome to oldboyedu backup! #模块注释信息
path = /backup #定义接受备份数据目录
#定义的用户 模块信息
rsync -avz rsync_backup@10.0.0.41::backup
bash#3.根据配置创建必要的数据
##一、新建用户
[root@backup ~]#id zhang
id: ‘rsync’: no such user
[root@backup ~]#groupadd -g101 zhang
[root@backup ~]#useradd -g101 -u101 -M -s /sbin/nologin zhang
[root@backup ~]#id zhang
uid=100(zhang) gid=100(users) groups=100(users)
#不允许登录的虚拟用户
[root@backup ~]#grep zhang /etc/passwd
zhang:x:100:100::/home/zhang:/sbin/nologin
##二、创建密码文件
[root@backup ~]#echo "rsync_backup:1">/etc/rsync.passwd
[root@backup ~]#cat /etc/rsync.passwd
rsync_backup:1
#密码文件给600权限
[root@backup ~]#chmod 600 /etc/rsync.passwd
[root@backup ~]#ll /etc/rsync.passwd
-rw------- 1 root root 15 Apr 3 16:27 /etc/rsync.passwd
##三、创建备份目录
[root@backup ~]#mkdir /backup
#没给/backup目录属主属组权限,报错
[root@backup ~]#rsync -avz /etc/hosts rsync_backup@10.0.0.41::backup
Password:
sending incremental file list
hosts
rsync: mkstemp ".hosts.vkP4gn" (in backup) failed: Permission denied (13)
sent 172 bytes received 121 bytes 30.84 bytes/sec
total size is 188 speedup is 0.64
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1191) [sender=3.1.3]
bashrsync_backup是连接的用户,需要通过身份验证,服务端让不让我连接进去 最终要写在/backup目录下,这个目录是root创建的,但是启动用户是rsync,所以要给目录属组属组改为rsync /etc/rsyncd.conf定义的用户是rsync,所以启动的服务就说rsync
bash
##属主属组修改成rsync启动用户,要不会报权限错误
[root@backup ~]##chown zhang.zhang /backup /data
[root@wbackup ~]#ll /
total 20
drwxr-xr-x 2 rsync rsync 19 Apr 1 19:39 backup
#4.启动服务
[root@backup ~]#systemctl start rsyncd
开机自启动
[root@backup ~]#systemctl enable rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
#5.检查服务
[root@backup ~]#netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1067/sshd: /usr/sbi
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 4321/rsync
#案例1.客户端执行测试
[root@web01 ~]#rsync -avz /etc/hosts rsync_backup@10.0.0.41::backup
Password:
sending incremental file list
hosts
sent 146 bytes received 43 bytes 54.00 bytes/sec
total size is 158 speedup is 0.84
#查看服务端
[root@backup /backup]#ll
total 4
-rw-r--r-- 1 zhang zhang 188 Apr 1 16:17 hosts
#案例2.将oldboy目录推送到bakcup模块
[root@backup ~]#rsync -avz oldboy rsync_backup@10.0.0.41::backup
Password:
sending incremental file list
oldboy/
oldboy/1.txt
oldboy/2.txt
oldboy/3.txt
sent 228 bytes received 85 bytes 89.43 bytes/sec
total size is 0 speedup is 0.00
#查看
[root@web01 ~]#tree /backup/oldboy/
/backup/oldboy/
├── 1.txt
├── 2.txt
└── 3.txt
0 directories, 3 files
#案例3.将服务端backup模块对应目录下所有的文件下载到当前为止
[root@web01 ~]#rsync -avz rsync_backup@backup::backup .
Password:
receiving incremental file list
./
hosts
oldboy/
oldboy/1.txt
oldboy/2.txt
oldboy/3.txt
sent 115 bytes received 421 bytes 153.14 bytes/sec
total size is 188 speedup is 0.35
[root@web01 ~]#ll
total 4
-rw-r--r-- 1 root root 188 Apr 1 16:17 hosts
drwxr-xr-x 2 root root 45 Apr 1 19:57 oldboy
#案例4.指定文件下载 下载hosts文件到当前的路径
[root@web01 ~]#rsync -avz rsync_backup@backup::backup/hosts .
Password:
receiving incremental file list
hosts
sent 43 bytes received 176 bytes 62.57 bytes/sec
total size is 188 speedup is 0.86
[root@web01 ~]#ll
total 4
-rw-r--r-- 1 root root 188 Apr 1 16:17 hosts
#案例5.远程模式下路径注意事项 加不加oldboy区别很大
#将本地oldboy目录拷贝到/backup/oldboy目录下
[root@web01 ~]#rsync -avz oldboy 10.0.0.41:/backup/oldboy
[root@backup ~]#tree /backup/
/backup/
├── hosts
└── oldboy
├── 1.txt
├── 2.txt
├── 3.txt
└── oldboy
2 directories, 4 files
#将本地oldboy目录拷贝到/backup/oldboy目录
[root@web01 ~]#rsync -avz --delete oldboy 10.0.0.41:/backup/
[root@backup ~]#tree /backup/
/backup/
├── hosts
└── oldboy
bash1.客户端指定密码文件免交互
创建密码文件 # 密码相关的放到/etc目录下
#创建密码文件
[root@web01 ~]#echo "1"> /etc/pass.txt
[root@web01 ~]#cat /etc/pass
pass.txt passwd passwd-
[root@web01 ~]#cat /etc/pass.txt
123456
#给600权限
[root@web01 ~]#chmod 600 /etc/pass.txt
[root@web01 ~]#ll /etc/pass.txt
-rw------- 1 root root 7 Apr 1 20:59 /etc/pass.txt
推送时指定密码文件
[root@web01 ~]#rsync -avz /etc/passwd rsync_backup@backup::backup --password-file=/etc/pass.txt
sending incremental file list
passwd
sent 873 bytes received 43 bytes 1,832.00 bytes/sec
total size is 1,929 speedup is 2.11
2、使用Rsync的内置变量
[root@web01 ~]#export RSYNC_PASSWORD=1
[root@web01 ~]#rsync -avz /root/ rsync_backup@backup::backup
sending incremental file list
sent 556 bytes received 26 bytes 1,164.00 bytes/sec
total size is 35,608 speedup is 61.18
bash客户端需求:
1.客户端提前准备存放的备份的目录,目录规则如下:/backup/web01_172.16.1.7_2018-09-02
[root@web01 ~]#mkdir -p /backup/`hostname`_`hostname -I|awk '{print $2}'`_`date +%F`
/backup/web01_172.16.1.7_2025-04-01
2.客户端在本地打包备份(系统配置文件、应用配置等)拷贝至/backup/web01_172.16.1.7_2018-09-02
find /etc/*.conf|xargs tar -zcvf /backup/`hostname`_`hostname -I|awk '{print $2}'`_`date +%F`/backup.tar.gz
3.客户端最后将备份的数据进行推送至备份服务器
rsync -avz /backup/`hostname`_`hostname -I|awk '{print $2}'`_`date +%F`/backup.tar.gz rsync_backup@backup::backup
4.客户端每天凌晨1点定时执行该脚本
5.客户端服务器本地保留最近7天的数据, 避免浪费磁盘空间
bashRsync命令
三种工作模式
本地模式 了解
程访问模式 拷贝文件常用(scp)
ush: 推送 上传
本地的文件推送到指定服务器的某个路径下
sync -avz /etc/hosts root@10.0.0.8:/opt/
pull: 拉取 下载
rsync -avz root@10.0.0.8:/etc/passwd /tmp/
参数选项:
--exclude # 排除传输的文件
--bwlimit # 限速 1m 2m
--delete # 无差异同步
守护进程模式 # 企业最常用
bash#1.安装服务
yum -y install rsync
#2.配置服务
[root@backup /data]#cat /etc/rsyncd.conf
uid = zhang #启动的用户,可以自定义
gid = zhang #
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users =
#认证用户,可以自定义
secrets file = /etc/rsync.passwd #创建密码文件,可自定义文件名
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup #存放备份文件的路径
[data]
path=/data
#3.创建用户,先id以下看没有么,创建用户要跟配置服务里面的uid对应起来
#4.创建密码文件并修改权限,这是服务端,用户连接我们,需要来这个文件验证用户身份和密码
echo 'rsync_backup:1'>/etc/rsync.passwd
#5.创建目录并授权属主属组,属组属组给上面创建的用户
#6.启动服务
systemctl start rsyncd
systemctl enable rsyncd
#7.客户端推送测试
rsync -avz /etc/hosts rsync_backup@10.0.0.41::backup
bashNFS: 网络文件系统
作用: 数据共享 实现数据一致性
#1.安装nfs服务 10.0.0.31
[root@nfs ~]#yum -y install nfs-utils
#2.配置NFS服务 默认文件为空
[root@nfs ~]#cat /etc/exports
/nfs_data 172.16.1.0/24(rw,sync,all_squash) #将/nfs_data设为共享目录
rw:可读可写,nfs和启动身份没关
all_squash:匿名用户压缩,不管以什么身份创建,全部压缩成虚拟的用户身份,如果被压缩成www用户,那么/nfs_data的属主属组就会变成www
bash#3.创建必要的数据信息
[root@nfs /]#mkdir /nfs_data
[root@nfs /]#ll -d /nfs_data/
drwxr-xr-x 2 root root 6 Apr 2 15:49 /nfs_data/
#创建用户
[root@nfs ~]#groupadd -g200 zhang
[root@nfs ~]#useradd -u200 -g200 -M -s /sbin/nologin zhang
#修改属主属组为压缩后的用户
[root@nfs ~]#chown zhang.zhang /nfs_data
#4.启动nfs服务
[root@nfs /]#systemctl start nfs
[root@nfs /]#systemctl enable nfs
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
[root@nfs /]#netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:35563 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:35339 0.0.0.0:* LISTEN 4145/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 4148/rpcbind
tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 4157/rpc.mountd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1073/sshd: /usr/sbi
tcp6 0 0 :::2049 :::* LISTEN -
tcp6 0 0 :::111 :::* LISTEN 4148/rpcbind
tcp6 0 0 :::20048 :::* LISTEN 4157/rpc.mountd
tcp6 0 0 :::36849 :::* LISTEN 4145/rpc.statd
tcp6 0 0 :::22 :::* LISTEN 1073/sshd: /usr/sbi
tcp6 0 0 :::36343 :::* LISTEN -
udp 0 0 127.0.0.1:929 0.0.0.0:* 4145/rpc.statd
udp 0 0 0.0.0.0:20048 0.0.0.0:* 4157/rpc.mountd
udp 0 0 0.0.0.0:111 0.0.0.0:* 4148/rpcbind
udp 0 0 0.0.0.0:39118 0.0.0.0:* -
udp 0 0 127.0.0.1:323 0.0.0.0:* 746/chronyd
udp 0 0 0.0.0.0:43906 0.0.0.0:* 4145/rpc.statd
udp6 0 0 :::36782 :::* -
udp6 0 0 :::20048 :::* 4157/rpc.mountd
udp6 0 0 :::111 :::* 4148/rpcbind
udp6 0 0 :::48342 :::* 4145/rpc.statd
udp6 0 0 ::1:323 :::* 746/chronyd
#5.查看服务启动是否正常
#如果下列文件中有内容说明配置正确。如果为空说明配置文件格式错误。
#通过以下内容可以看到默认压缩成了anonuid=65534,anongid=65534的用户
[root@nfs /]#cat /var/lib/nfs/etab
/nfs_data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)
#6.根据65534找到被压缩的用户
[root@nfs /]#grep 65534 /etc/passwd
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
bash客户端挂载测试:
#1.安装命令nfs-utils # 只安装不运行 kylin系统默认已经安装了 主要使用showmount命令
#2.查看NFS服务器共享的是哪个目录
[root@web01 ~]#showmount -e 172.16.1.31
Export list for 172.16.1.31:
/nfs_data 172.16.1.0/24
#3.挂载
[root@web01 ~]#mount -t nfs 172.16.1.31:/nfs_data /mnt
#会看到已经挂载成功了
[root@web01 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 459M 0 459M 0% /dev
tmpfs 475M 0 475M 0% /dev/shm
tmpfs 475M 6.9M 468M 2% /run
tmpfs 475M 0 475M 0% /sys/fs/cgroup
/dev/mapper/klas-root 47G 3.8G 44G 8% /
/dev/sda1 1014M 169M 846M 17% /boot
tmpfs 95M 0 95M 0% /run/user/0
172.16.1.31:/nfs_data 47G 3.8G 44G 9% /mnt
#此时进入/mnt就是进入到31服务器的/nfs_data目录
[root@web01 ~]#cd /mnt
[root@web01 /mnt]#ll
total 0
#创建显示没有权限,因为相当于进入nfs服务器,而nfs服务是nobody用户
[root@web01 /mnt]#touch 1.txt
touch: cannot touch '1.txt': Permission denied
#因为/nfs_data/的权限为root用户
[root@nfs /]#ll -d /nfs_data/
drwxr-xr-x 2 root root 6 Apr 2 15:49 /nfs_data/
#修改属主属组后,创建成功,切换nfs服务器查看
[root@nfs /]#ll /nfs_data/
total 0
-rw-r--r-- 1 nobody nobody 0 Apr 2 16:18 1.txt
#4.写入到开机自动挂载
[root@web01 ~]#cat /etc/fstab
172.16.1.31:/nfs_data /mnt nfs defaults 0 0
#注意,因为/etc/exports指定的是172段,所以10段能显示但是挂载不上
[root@web01 ~]#showmount -e 10.0.0.31
Export list for 10.0.0.31:
/nfs_data 172.16.1.0/24
[root@web01 ~]#mount -t nfs 10.0.0.31:nfs_data /mnt
Created symlink /run/systemd/system/remote-fs.target.wants/rpc-statd.service → /usr/lib/systemd/system/rpc-statd.service.
mount.nfs: access denied by server while mounting 10.0.0.31:nfs_data
#测试nfs服务器ro权限
[root@nfs /]#vim /etc/exports
[root@nfs /]#systemctl restart nfs
[root@nfs /]#cat /etc/exports
/nfs_data 172.16.1.0/24(ro,sync,all_squash)
#在客户端测试,显示只读
[root@web01 ~]#cd /mnt
[root@web01 /mnt]#touch 2.txt
touch: cannot touch '2.txt': Read-only file system
bash#修改anonuid=666,anongid=666
[root@nfs /]#cat /etc/exports
/nfs_data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
[root@nfs /]#grep 666 /etc/passwd
#创建组及用户
[root@nfs /]#groupadd -g666 www
[root@nfs /]#useradd -u666 -g666 -M -s /sbin/nologin www
[root@nfs /]#id www
uid=666(www) gid=666(www) groups=666(www)
#重新启动服务
[root@nfs /]#systemctl restart nfs
#将属主属组nobody修改为www
[root@nfs /]#chown www.www /nfs_data/
#此时再次进行测试,因为web01没有www用户,所以显示的属主属组为666
[root@web01 /mnt]#touch {a..c}.txt
[root@web01 /mnt]#ll
total 0
-rw-r--r-- 1 nobody nobody 0 Apr 2 16:18 1.txt
-rw-r--r-- 1 666 666 0 Apr 2 16:54 a.txt
-rw-r--r-- 1 666 666 0 Apr 2 16:54 b.txt
-rw-r--r-- 1 666 666 0 Apr 2 16:54 c.txt
bash*准备3台虚拟机服务器,并且请按照要求搭建配置NFS服务。
NFS服务端(A)172.16.1.31
NFS客户端(B)172.16.1.7
NFS客户端(C)172.16.1.41
1.在NFS服务端(A)上共享/data/w(可写)及/data/r(只读)
2.在NFS客户端(B/C)上进行挂载*
B 挂载/data/w
C 挂载/data/r
#配置NFS服务/nfs_data为读写,/nfs_tmp为只读
[root@nfs /]#cat /etc/exports
/nfs_data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/nfs_tmp 172.16.1.0/24(ro,sync,all_squash,anonuid=666,anongid=666)
#重启nfs服务
[root@nfs /]#systemctl restart nfs
#修改属主属组为www
[root@nfs /]#chown www.www nfs_tmp
#查看NFS服务器共享的是哪个目录
[root@backup /]#showmount -e 172.16.1.31
Export list for 172.16.1.31:
/nfs_tmp 172.16.1.0/24
/nfs_data 172.16.1.0/24
#backup服务器挂载只读目录
[root@backup /]#mount -t nfs 172.16.1.31:nfs_tmp /mnt
#web01挂载读写目录
[root@web01 /]#mount -t nfs 172.16.1.31:/nfs_data
#测试成功
bash部署Rsync服务 10.0.0.41
1.yum -y install rsync
2.配置rsync服务
[root@backup /mnt]#cat /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup
[data]
path=/data
3.创建必要的数据
[root@backup /]#groupadd zhang
[root@backup /]#useradd -u101 -g101 -M -s /sbin/nologin rsync
4.创建密码文件
[root@backup /]#cat /etc/rsync.passwd
rsync_backup:1
修改文件权限
[root@backup /]#chmod 600 /etc/rsync.passwd
[root@backup /]#ll /etc/rsync.passwd
-rw------- 1 root root 20 Apr 1 19:54 /etc/rsync.passwd
创建目录,修改属主属组
[root@backup /]#mkdir data
4.启动服务
[root@backup ~]#systemctl start rsyncd
[root@backup ~]#systemctl enable rsyncd
bash略过
bash略过
bash监控谁,就在谁下面挂载
1.安装服务
[root@nfs /]#yum -y install lsyncd
2.配置服务
[root@nfs /]#cat /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd/lsyncd.log", #日志文件
statusFile ="/var/log/lsyncd/lsyncd.status", #日志文件
maxProcesses = 2, #最大进程数量
nodaemon = false, #以守护进程方式运行
}
sync {
default.rsync, #运行方式
source = "/nfs_data/", #监控哪个目录
target = "rsync_backup@10.0.0.41::nfs", #目标,使用rsync守护进程模式
delete = true, #--delete参数,无差异同步以当前的/data目录为准
delay = 1, #延时1秒
rsync = {
binary = "/usr/bin/rsync", #命令所在路径
password_file = "/etc/rsyncd.pwd", #密码文件路径,配置lsyncd使用rsync协议同步到远程服务器的rsyncd 服务时,两者的密码需要保持一致,相当于在nfs服务器调用守护进程模式
archive = true, #-a
compress = true, #-z
}
}
3.创建必要数据信息
#创建密码文件
[root@nfs /]#echo 1 > /etc/rsyncd.pwd
[root@nfs /]#chmod 600 /etc/rsyncd.pwd
[root@nfs /]#cat /etc/rsyncd.pwd
1
4.启动服务
[root@nfs /]#systemctl start lsyncd.service
5.测试,在nfs目录创建文件
[root@nfs /nfs_data]#ll
total 0
-rw-r--r-- 1 nobody nobody 0 Apr 2 17:40 1.txt
-rw-r--r-- 1 www www 0 Apr 2 16:54 a.txt
-rw-r--r-- 1 www www 0 Apr 2 16:54 b.txt
-rw-r--r-- 1 www www 0 Apr 2 16:54 c.txt
在backup服务器查看
[root@backup /cdm]#ll
total 0
-rw-r--r-- 1 rsync rsync 0 Apr 2 17:40 1.txt
-rw-r--r-- 1 rsync rsync 0 Apr 2 16:54 a.txt
-rw-r--r-- 1 rsync rsync 0 Apr 2 16:54 b.txt
-rw-r--r-- 1 rsync rsync 0 Apr 2 16:54 c.txt
#从web01新建zhang.txt文件
[root@web01 /mnt]#touch zhang.txt
#从backup服务器可以看到
[root@backup /cdm]#ll
total 0
-rw-r--r-- 1 rsync rsync 0 Apr 2 17:48 zhang.txt
作业
bash1.模拟NFS服务器挂掉
1、#关闭nfs服务器ens36网卡
[root@nfs /nfs_data]#ifdown ens36
WARN : [ifdown] You are using 'ifdown' script provided by 'network-scripts', which are now deprecated.
WARN : [ifdown] 'network-scripts' will be removed from distribution in near future.
WARN : [ifdown] It is advised to switch to 'NetworkManager' instead - it provides 'ifup/ifdown' scripts as well.
Device 'ens36' successfully disconnected.
2、#此时df -h界面宕住,因为挂载着共享目录的服务器断网了
#先查看挂载信息,可以看到是挂载到/mnt下了
[root@web01 /mnt]#cat /proc/mounts
172.16.1.31:/nfs_data /mnt
#强制卸载
[root@web01 /]#umount -f /mnt
[root@web01 /]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 459M 0 459M 0% /dev
tmpfs 475M 0 475M 0% /dev/shm
tmpfs 475M 6.9M 468M 2% /run
tmpfs 475M 0 475M 0% /sys/fs/cgroup
/dev/mapper/klas-root 47G 3.8G 44G 8% /
/dev/sda1 1014M 169M 846M 17% /boot
tmpfs 95M 0 95M 0% /run/user/0
3.backup服务器上部署NFS
[root@backup ~]#yum -y install nfs-utils
[root@backup /data]#cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=200,anongid=200)
[root@backup /data]#systemctl start nfs
[root@backup /data]#systemctl status nfs
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
Active: active (exited) since Thu 2025-04-03 17:17:02 CST; 5s ago
Process: 5091 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Process: 5092 ExecStart=/usr/sbin/rpc.nfsd (code=exited, status=0/SUCCESS)
Main PID: 5092 (code=exited, status=0/SUCCESS)
4.web服务器挂载backup的data目录
[root@web01 ~]#showmount -e 172.16.1.41
Export list for 172.16.1.41:
/data 172.16.1.0/24
[root@web01 ~]#mount -t nfs 172.16.1.41:/data /mnt
[root@web01 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 459M 0 459M 0% /dev
tmpfs 475M 0 475M 0% /dev/shm
tmpfs 475M 6.9M 468M 2% /run
tmpfs 475M 0 475M 0% /sys/fs/cgroup
/dev/mapper/klas-root 47G 3.8G 44G 8% /
/dev/sda1 1014M 169M 846M 17% /boot
tmpfs 95M 0 95M 0% /run/user/0
172.16.1.41:/data 47G 3.8G 44G 9% /mnt
bashbackup配置了rsync服务,nfs配置了lsyncd服务、nfs服务 nfs服务器的/nfs_data目录作为共享目录挂载到了web01服务器的/mnt目录上面,所以进入web01服务器的mnt目录就是进入了nfs服务器的/nfs_data目录,而且nfs服务器配置文件里面配置了监控目录(监控nfs服务器下的/nfs_data),如果/nfs_data有数据变化,那么会使用rsync的守护进程同步到backu的data下
bash#windows使用命令SSH远程连接Linu
[C:\~]$ ssh 10.0.0.41 #默认连接的22端口,手动输入用户名/密码
[C:\~]$ ssh root@10.0.0.41 #指定用户
#修改默认端口,改为Port 2222
[root@backup ~]#vim /etc/ssh/sshd_config
[root@backup ~]#grep Port /etc/ssh/sshd_config
Port 2222
#GatewayPorts no
GatewayPorts no
[root@web01 ~]#systemctl restart sshd
#查看是否修改成功,看一下监听端口是否是2222
[root@web01 ~]#netstat -tnulp
tcp 0 0 0.0.0.0:2222 0.0.0.0:* LISTEN 5263/sshd: /usr/sbi
#指定端口去连接
[C:\~]$ ssh root@10.0.0.41 2222
Connecting to 10.0.0.41:2222...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
#Linux系统连接Linux系统
[root@web01 ~]#ssh 10.0.0.41 #默认以当前登录的root作为远程连接的账号,默认使用22端口
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Thu Apr 3 14:41:56 2025 from 10.0.0.1
[root@backup ~]#ssh root@10.0.0.41 # 指定用户名称
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Thu Apr 3 14:42:26 2025 from 10.0.0.7
[root@web01 ~]#ssh -p2222 root@10.0.0.41 # 指定端口连接
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Thu Apr 3 14:46:53 2025 from 10.0.0.41
#注意: 不管是windows连接还是linux连接 主机都可以是IP 或者是域名 或者是主机名。
[root@backup ~]#cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.41 backup
[root@web01 ~]#ssh backup
Authorized users only. All activities may be monitored and reported.
root@backup's password:
Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Thu Apr 3 14:54:23 2025 from 10.0.0.1
bash语法结构:
ssh 服务器IP|域名|主机名 '可执行命令'
#案例1.在nfs服务器的root下创建nfs.txt
[root@web01 ~]#ssh 10.0.0.31 'touch nfs.txt'
Authorized users only. All activities may be monitored and reported.
root@10.0.0.31's password:
#案例2.在web服务器上执行,远程查看nfs服务器的ip地址
[root@web01 ~]#ssh 10.0.0.31 'hostname -I'
Authorized users only. All activities may be monitored and reported.
root@10.0.0.31's password:
10.0.0.31 172.16.1.31
#案例3.查看nfs的进程
[root@web01 ~]#ssh 10.0.0.31 'netstat -tnulp|grep sshd'
Authorized users only. All activities may be monitored and reported.
root@10.0.0.31's password:
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1073/sshd: /usr/sbi
tcp6 0 0 :::22 :::* LISTEN 1073/sshd: /usr/sbi
#案例4.远程执行目标主机的脚本
[root@web01 ~]#ssh 10.0.0.31 'sh /root/1.sh'
Authorized users only. All activities may be monitored and reported.
root@10.0.0.31's password:
#案例5.杀死目标主机的某个进程
[root@web01 ~]#ssh 10.0.0.31 'kill -9 7026'
bashssh远程连接 需要用户名密码 scp远程拷贝 需要用户名密码 ssh远程命令 需要用户名密码 SSH远程服务,可以用用户名和密码的验证方式也可以使用秘 钥的验证方式。 手机解锁方式: 密码 指纹 人脸
bash需求: 实现从web01远程连接NFS实现免秘钥
第一步:
[root@web01 ~]#ssh-keygen #一路回车
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:jrh97Uvw5Bn69ZGrl2WQL2u9YsM4WkEps/u787A1DsI root@web01
The key's randomart image is:
+---[RSA 3072]----+
| |
| . |
| o o . |
| = o |
| S.o. o |
| . o.*.o. ..+|
| . . oE=++ooB |
| o .o==***o.|
| . .. o=*O*=..|
+----[SHA256]-----+
[root@web01 ~]#ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? #注意已经存在了不要重新生成了。
第二步: 将生成的公钥传送到目标主机 nfs
[root@web01 ~]#ssh-copy-id 10.0.0.31
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Authorized users only. All activities may be monitored and reported.
root@10.0.0.31's password: #需要31的密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '10.0.0.31'"
and check to make sure that only the key(s) you wanted were added.
第三步:检查nfs端是否接收成功
[root@nfs ~]#ll .ssh/
total 8
-rw------- 1 root root 564 Apr 3 15:47 authorized_keys
-rw-r--r-- 1 root root 170 Apr 3 15:41 known_hosts
#相当于把web01服务器的.ssh/id_rsa.pub的文件内容复制到了.ssh/authorized_keys里面
[root@web01 ~]#cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDU08dGfI5rdo8Wvt9qdBSq1rXYeaRdHTSoAdxb8lhZR+v8/h0ACCYAhJnD5fpRj7+oWCba2hJE4+K+O8cyF+GdWMysOfMi/GmUt59452zdoMBCx9KFv8fiVA4NkJUOvaSIIvsULdaj0J8dEMXLvOIasDP+L+s67xrBPfA4Af3Ms2VDo2ottTtb+oW0gfJa+HGe9cpimURhb7QxQdRycxPGuZpqT8iyQRPXACr/L3lFGB2I02qtzWDOOxZgYO/WH8/EMd6YR9V8cXx17o6votZUWuaKg6hCgxtqPAV9FYH02DWKPQBGmKHW1Fh6LURf4DztWHbyPFZvaCSwb7bNTJl3TgeMJRHyYAC12nnWyc8GZeI/maQ2T/cmr4V4957mpmXbKayDfMkv7LojmyFT/dhzmGwaOxyLwx8PO74t13OvkXO1Q+U0zQLKA4m0G3GCU8okktCJqABpHk2hAKrSV13LFd5HVf5n7vZ8CHNyqiDiGVz2IjfGCE0cbNsTU5SSmKc= root@web01
[root@nfs ~]#cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDU08dGfI5rdo8Wvt9qdBSq1rXYeaRdHTSoAdxb8lhZR+v8/h0ACCYAhJnD5fpRj7+oWCba2hJE4+K+O8cyF+GdWMysOfMi/GmUt59452zdoMBCx9KFv8fiVA4NkJUOvaSIIvsULdaj0J8dEMXLvOIasDP+L+s67xrBPfA4Af3Ms2VDo2ottTtb+oW0gfJa+HGe9cpimURhb7QxQdRycxPGuZpqT8iyQRPXACr/L3lFGB2I02qtzWDOOxZgYO/WH8/EMd6YR9V8cXx17o6votZUWuaKg6hCgxtqPAV9FYH02DWKPQBGmKHW1Fh6LURf4DztWHbyPFZvaCSwb7bNTJl3TgeMJRHyYAC12nnWyc8GZeI/maQ2T/cmr4V4957mpmXbKayDfMkv7LojmyFT/dhzmGwaOxyLwx8PO74t13OvkXO1Q+U0zQLKA4m0G3GCU8okktCJqABpHk2hAKrSV13LFd5HVf5n7vZ8CHNyqiDiGVz2IjfGCE0cbNsTU5SSmKc= root@web01
第三步:远程连接测试
[root@web01 ~]#ssh 10.0.0.31
Authorized users only. All activities may be monitored and reported.
Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Wed Apr 2 19:59:31 2025 from 10.0.0.1
[root@web01 ~]#ssh 10.0.0.31 'sh /root/1.sh'
Authorized users only. All activities may be monitored and reported.
[root@web01 ~]#
bash服务器没有公网,找一台有公网IP的服务器作为跳板机远程连接
bash#关闭31服务器网卡
[root@nfs /data]#ifdown ens33
#通过7服务器远程31服务器另一个网卡进行连接,因为配置了免密登录,所以可以直接跳转
[root@web01 /]#ssh 172.16.1.31
Authorized users only. All activities may be monitored and reported.
Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Mon Apr 7 15:48:20 2025 from 10.0.0.7
[root@nfs ~]#
bash只要看到]#就会执行ssh 172.16.1.31
bash#清理7服务器登录文件缓存,脚本就卡在此界面,需要新配置脚本内容
[root@web01 ~]#> .ssh/known_hosts
!(C:\Users\z1340\AppData\Roaming\Typora\typora-user-images\image-20250407160116667.png)
bash点连接,就可以进行正常登录了
bash需求:
1.跳板机禁用密码登录
[root@web01 /]#awk 'NR==66' /etc/ssh/sshd_config
PasswordAuthentication no
#重启生效
[root@web01 ~]#systemctl restart sshd
bashxshell生成密钥 2.windows通过秘钥方式连接跳板机
bash#在7服务器编辑配置文件,把公钥复制进去
[root@web01 /]#vim .ssh/authorized_keys
bash[root@web01 /]#cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAruWIsnD2RklE5k0tsFzSdTZVKR6VvHUZdDywwwhD4JAERil6ZDX3WwBEY6iJzRyKrvl2h6Sjo+DaheSRD9fczTSmhdIfQgsTsMrbSo/JFQ6A0/cAljfRtF9MADMU/tsrN/6V8p6HAAvPexrpzxf0B0dT9+hKsdmFTXgODl35bjiRhXapNeugCAP3WIDSm6W1nl4c5QRfHGPj/X3LEXg9nu8kKx9XTHw2esZUOfto0VJ+GjwcnrUR8HLGoFYqOA1+wijco0/ikN9DHSpxilQYfbe65ROtU7WyI+BKFc0/iIVyLgrqUWyCERyIXLdfgElm4vX3znoRV8C9V5It+KEjIw== rsa 2048-040725
bash配置过程中,Xshell显示“所选的用户密钥未在远程主机上注册”经过排查,原来是root目录的属主属组改成了rsync用户,才导致问题产生
#测试完进行修改
[root@web01 ~/.ssh]#awk 'NR==66' /etc/ssh/sshd_config
PasswordAuthentication yes
#测试禁用root
[root@web01 ~/.ssh]#awk 'NR==39' /etc/ssh/sshd_config
PermitRootLogin no
[root@web01 ~/.ssh]#systemctl restart sshd
bash#使用普通用户登录
[C:\~]$ ssh oldboy@10.0.0.7
Connecting to 10.0.0.7:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Authorized users only. All activities may be monitored and reported.
WARNING! The remote SSH server rejected X11 forwarding request.
Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Tue Apr 1 15:56:15 2025
[oldboy@web01 ~]#
#切换root用户
[oldboy@web01 ~]#su - root
Password:
Last login: Mon Apr 7 17:26:09 CST 2025 from 10.0.0.1 on pts/1
Last failed login: Mon Apr 7 17:26:34 CST 2025 from 10.0.0.1 on ssh:notty
There was 1 failed login attempt since the last successful login.
[root@web01 ~]#
bash全量拷贝
语法结构:
scp 源文件 目标主机:目标位置
scp -r 目录 目标主机:目标位置
#案例1.将web01的hosts拷贝到nfs的root家目录
[root@web01 ~]#scp /etc/hosts 10.0.0.31:/root
The authenticity of host '10.0.0.31 (10.0.0.31)' can't be established.
ECDSA key fingerprint is SHA256:Fe+StTKc+1JoYwNC0wQDHkY4QAuKr+cB3wGKah/fzkM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.31' (ECDSA) to the list of known hosts.
Authorized users only. All activities may be monitored and reported.
root@10.0.0.31's password:
hosts [root@web01 ~]#scp /etc/hosts 10.0.0.31:/root
The authenticity of host '10.0.0.31 (10.0.0.31)' can't be established.
ECDSA key fingerprint is SHA256:Fe+StTKc+1JoYwNC0wQDHkY4QAuKr+cB3wGKah/fzkM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.31' (ECDSA) to the list of known hosts.
Authorized users only. All activities may be monitored and reported.
root@10.0.0.31's password:
hosts 100% 188 154.2KB/s 00:00
#案例2.将oldboy目录拷贝nfs家目录
[root@web01 ~]#scp -r oldboy 10.0.0.41:/root
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
1.txt 100% 0 0.0KB/s 00:00
2.txt 100% 0 0.0KB/s 00:00
3.txt 100% 0 0.0KB/s 00:00
4.txt 100% 0 0.0KB/s 00:00
5.txt 100% 0 0.0KB/s 00:00
#案例3.将nfs的上的passwd拷贝到当前的位置
[root@web01 ~]#scp -r 10.0.0.41:/etc/passwd .
Authorized users only. All activities may be monitored and reported.
root@10.0.0.41's password:
passwd 100% 1970 1.2MB/s 00:00
[root@web01 ~]#ll
bash案例4.指定用户 注意权限问题
[root@web01 ~]#scp /etc/hostsname oldboy@10.0.0.31:/tmp
案例5.指定端口
[root@nfs ~]#scp -P2222 /etc/hosts 10.0.0.7:/root/
bashNginx非常轻量 功能模块少(源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装) 代码模块化 (易读,便于二次开发,对于开发人员非常友好) 互联网公司都选择Nginx 1.Nginx技术成熟,具备的功能是企业最常使用而且最需要的 2.适合当前主流架构趋势, 微服务、云架构、中间层 3.统一技术栈, 降低维护成本*,* 降低技术更新成本。 Nginx采用Epool网络模型,Apache采用Select模型 Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。 Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。 处理大量的连接的读写,Apache所采用的select网络I/O模型非常低效。用一个比喻来解释Apache采用的select模型和Nginx采用的epoll模型进行之间的区别: 假设你在大学读书,住的宿舍楼有很多间房间,你的朋友要来找你。select版宿管大妈就会带着你的朋友挨个房间去找,直到找到你为止。而epoll版宿管大妈会先记下每位同学的房间号,你的朋友来时,只需告诉你的朋友你住在哪个房间即可,不用亲自带着你的朋友满大楼找人。如果来了10000个人,都要找自己住这栋楼的同学时,select版和epoll版宿管大妈,谁的效率更高,不言自明。同理,在高并发服务器中,轮询I/O是最耗时间的操作之 一,select和epoll的性能谁的性能更高,同样十分明了。 Nginx和Apache区别 Nginx 轻量级,采用 C 进行编写,同样的 web 服务,会占用更少的内存及资源 抗并发,nginx 以 epoll and kqueue 作为开发模型,处理请求是异步非阻塞的,负载能力比 apache 高很多,而 apache 则是阻塞型的。在高并发下 nginx 能保持低资源低消耗高性能 ,而 apache 在 PHP 处理慢或者前端压力很大的情况下,很容易出现进程数飙升,从而拒绝服务的现象。 nginx 处理静态文件好,静态处理性能比 apache 高三倍以上 nginx 的设计高度模块化,编写模块相对简单 nginx 配置简洁,正则配置让很多事情变得简单,而且改完配置能使用 -t 测试配置有没有问题,apache 配置复杂,重启的时候发现配置出错了,会很崩溃 nginx 作为负载均衡服务器,支持 7 层负载均衡 Nginx安装部署 nginx 本身就是一个反向代理服务器,而且可以作为非常优秀的邮件代理服务器启动特别容易, 并且几乎可以做到 7*24 不间断运行,即使运行数个月也不需要重新启动,还能够不间断服务的情况下进行软件版本的升级社区活跃,各种高性能模块出品迅速 Apache apache 的 rewrite 比 nginx 强大,在 rewrite 频繁的情况下,用 apache apache 发展到现在,模块超多,基本想到的都可以找到 apache 更为成熟,少 bug ,nginx 的 bug 相对较多 apache 超稳定 apache 对PHP支持比较简单,nginx 需要配合其他后端用 apache 在处理动态请求有优势,nginx 在这方面是鸡肋,一般动态请求要 apache 去做,nginx 适合静态和反向。
bash三种安装方式:
#第一种: 默认的仓库安装 版本较低、配置不易读(不建议)
[root@web01 ~]#yum -y install nginx
[root@web01 ~]#nginx -v
nginx version: nginx/1.21.5
[root@web01 ~]#yum -y remove nginx
#第二种: 通过官方的仓库安装(建议使用)
#安装完nginx仓库,下载仓库会根据名字先去nginx仓库去安装,nginx没有会去默认仓库
[root@web01 ~]#vim /etc/yum.repos.d/nginx.repo
1.配置官方仓库
[nginx-stable] # 仓库的名称
name=nginx stable repo # 描述--重要
baseurl=http://nginx.org/packages/centos/7/$basearch/ # 仓库的URL连接 仓库的地址--重要
gpgcheck=1 # 检查下载软件的完整性 1为检查 0为不检查
enabled=1 # 启动此仓库--重要
gpgkey=https://nginx.org/keys/nginx_signing.key # key的链接地址
[root@web01 ~]#nginx -v
nginx version: nginx/1.26.1
[root@web01 ~]#rpm -qa nginx
nginx-1.26.1-2.el7.ngx.x86_64
#第三种: 通过编译安装
bashNginx启动方式
命令行的方式启动: /usr/sbin/nginx
/usr/sbin/nginx -s stop
/usr/sbin/nginx -s reload
/usr/sbin/nginx -s stop && sleep 1 && /usr/sbin/nginx
#这种方式启动不支持查看当前nginx状态 只能用netstat -tnulp查看是否启动
systemctl方式启动:
systemctl start nginx
restart # 重启
reload # 重新加载
status # 查看服务状态
enable # 开机自启动
disable # 开机禁止启动
bash[root@web01 ~]#cat /etc/nginx/nginx.conf
#核心区块
user nginx; # nginx服务的启动用户
worker_processes auto; # nginx启动的进程数量 启动数量和内核一致。通过lscpu查看cpu信息,On-line CPU(s)list:0表示1个
error_log /var/log/nginx/error.log notice; # 错误日志的位置
pid /var/run/nginx.pid; # 存放nginx主程序PID的文件
#事件区块
events {
worker_connections 1024; # 每个进程的最大连接数量
}
#http区块
http {
include /etc/nginx/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 /var/log/nginx/access.log main; # nginx访问日志,根据上面日志格式生成
sendfile on; # 文件高效传输
#tcp_nopush on;
keepalive_timeout 65; # 长连接的失效时间 65s
#gzip on; # 是否压缩
include /etc/nginx/conf.d/*.conf; #将conf.d所有的*.conf引用进来
}
include概念:
bash#解释:pid /var/run/nginx.pid; # 运行程序存放PID的文件
[root@web01 ~]#cat /var/run/nginx.pid
6526
[root@web01 ~]#ps aunx|grep nginx
0 6526 0.0 0.0 22504 844 ? Ss 18:02 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
990 6527 0.0 0.3 33776 3740 ? S 18:02 0:00 nginx: worker process
0 6530 0.0 0.0 213140 880 pts/0 R+ 18:02 0:00 grep --color=auto nginx
#解释:资源支持的类型有哪些,比如1.txt,可以把里面的内容显示到浏览器页面上
[root@web01 /etc/nginx]#cat mime.types
types {
text/plain txt;
[root@web01 /etc/nginx]#ll /usr/share/nginx/html
total 8
-rw-r--r-- 1 root root 497 May 30 2024 50x.html
-rw-r--r-- 1 root root 615 May 30 2024 index.html
[root@web01 /etc/nginx]#echo "13406161615"> /usr/share/nginx/html/1.txt


bashserver区块配置
[root@web01 /etc/nginx/conf.d]#cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name www.baidu.com;
location / {
root /code/;
index index.html index.htm;
}
}
#当用户访问 http://www.baidu.com 时:Nginx 检查到域名 www.baidu.com和端口80,匹配此配置。进入 location /块,将请求映射到服务
#器的 /code 目录。若请求路径是目录,返回 /code/index.html 或 index.htm;若是文件,直接返回对应文件。
#root不是用户,而是指定用户访问代码的位置是 /code
#检查nginx配置文件是否正确
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重启nginx生效
[root@web01 ~]#systemctl restart nginx
[root@web01 ~]#systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@web
#配置默认首页资源
[root@web01 ~]#mkdir /code
[root@web01 ~]#echo "0123456">/code/index.html
[root@web01 ~]#cat /code/index.html
0123456
bash200 # 表示网站OK
301 # 永久跳转
302 # 临时跳转
403 # 表示访问的资源不存在,但是目录存在。
404 # 表示访问的资源不存在,目录不存在。
HTTP常见状态码
一、常见的状态码
200(成功) - 服务器成功返回网页
201(已创建) - 请求成功并且服务器创建了新的资源。
304(未修改) - 缓存
301 - 永久跳转
302 - 临时跳转
400(错误请求) - 服务器不理解请求的语法。
403 - 资源不存在
404(未找到) - 请求的网页不存在
500(服务器内部错误) - 服务器遇到错误,无法完成请求。
502 - 网关超时
503(服务不可用)- 服务器目前无法使用(由于超载或停机维护)。通常,这只是暂时状态。
二、HTTP状态码100-500详细介绍
1XX(临时响应信息提示 )这些状态代码表示临时的响应。客户端在收到常规响应之前,应准备接收一个或多个1xx。
100(继续)- 请求者应当继续提出请求。服务器返回此代码表示已收到请求的第一部分,正在等待其余部分。
101(切换协议) - 服务器将遵从客户的请求转换到另外一种协议
#2XX(成功)
表示成功处理了请求的状态代码。
200(成功) - 服务器已成功处理了请求。
201(已创建) - 请求成功并且服务器创建了新的资源。
202(已接受) - 服务器已接受请求,但尚未处理。
203(非授权信息) - 服务器已成功处理了请求,但返回的信息可能来自另一来源。
204(无内容) - 服务器成功处理了请求,但没有返回任何内容。
205(重置内容) - 服务器成功处理了请求,但没有返回任何内容。与 204响应不同,此响应要求请求者重置文档视图(例如,清除表单内容以输入新内容)。
206(部分内容) - 服务器成功处理了部分 GET 请求。
#3XX - 重定向
表示要完成请求,需要进一步操作。客户端浏览器必须采取更多操作来实现请求。例如,浏览器可能不得不请求服务器上的不同的页面,或通过代理服务器重复该请求。 建议在每次请求中使用重定向不要超过 5次。 通常,这些状态代码用来重定向。
300(多种选择)- 针对请求,服务器可执行多种操作。服务器可根据请求者 (user agent) 选择一项操作,或提供操作列表供请求者选择。
301(永久移动)- 请求的网页已永久移动到新位置。服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自动将请求者转到新位置。
302(临时移动)- 服务器目前从不同位置的网页响应请求,但请求者应继续使用原有位置来进行以后的请求。
303(查看其他位置)- 请求者应当对不同的位置使用单独的 GET 请求来检索响应时,服务器返回此代码。
304(未修改)- 自从上次请求后,请求的网页未修改过。服务器返回此响应时,不会返回网页内容。
305(使用代理)- 请求者只能使用代理访问请求的网页。如果服务器返回此响应,还表示请求者应使用代理。
307(临时重定向)- 服务器目前从不同位置的网页响应请求,但请求者应继续使用原有位置来进行以后的请求。
4XX(请求错误)
这些状态码表示请求可能出错,客户端似乎有问题。例如,客户端请求不存在的页面,客户端未提供有效的身份验证信息。 妨碍了服务器的处理。
400(错误请求)- 服务器不理解请求的语法。
401(未授权)- 请求要求身份验证。对于登录后请求的网页,服务器可能返回此响应。
403(禁止)- 服务器拒绝请求。如果您在 Googlebot尝试抓取您网站上的有效网页时看到此状态码(您可以在Google网站管理员工具诊断下的网络抓取页面上看到此信息),可能是您的服务器或主机拒绝了 Googlebot 访问。
404(未找到)- 服务器找不到请求的网页。例如,对于服务器上不存在的网页经常会返回此代码。
405(方法禁用)- 禁用请求中指定的方法。
406(不接受)- 无法使用请求的内容特性响应请求的网页。
407(需要代理授权)- 此状态码与 401(未授权)类似,但指定请求者应当授权使用代理。如果服务器返回此响应,还表示请求者应当使用代理。
408(请求超时)- 服务器等候请求时发生超时。
409(冲突)- 服务器在完成请求时发生冲突。服务器必须在响应中包含有关冲突的信息。服务器在响应与前一个请求相冲突的 PUT请求时可能会返回此代码,以及两个请求的差异列表。
410(已删除)- 如果请求的资源已永久删除,服务器就会返回此响应。该代码与404(未找到)代码类似,但在资源以前存在而现在不存在的情况下,有时会用来替代 404 代码。如果资源已永久移动,您应使用 301指定资源的新位置。
411(需要有效长度)- 服务器不接受不含有效内容长度标头字段的请求。
412(未满足前提条件)- 服务器未满足请求者在请求中设置的其中一个前提条件。
413(请求实体过大)- 服务器无法处理请求,因为请求实体过大,超出服务器的处理能力。
414(请求的 URI 过长)- 请求的 URI(通常为网址)过长,服务器无法处理。
415(不支持的媒体类型)- 请求的格式不受请求页面的支持。
416(请求范围不符合要求)- 如果页面无法提供请求的范围,则服务器会返回此状态码。
417(未满足期望值)- 服务器未满足”期望”请求标头字段的要求。
5XX(服务器错误)
这些状态码表示服务器在处理请求时发生内部错误。这些错误可能是服务器本身的错误,而不是请求出错。
500 (服务器内部错误)- 服务器遇到错误,无法完成请求。
500.12- 应用程序正忙于在 Web 服务器上重新启动。
500.13- Web 服务器太忙。
500.15- 不允许直接请求 Global.asa。
500.16- UNC 授权凭据不正确。这个错误代码为 IIS6.0 所专用。
500.18- URL 授权存储不能打开。这个错误代码为 IIS6.0 所专用。
500.100- 内部 ASP 错误。
501 (尚未实施)- 服务器不具备完成请求的功能。例如,服务器无法识别请求方法时可能会返回此代码。
502 (错误网关)- 服务器作为网关或代理,从上游服务器收到无效响应。
503 (服务不可用)- 服务器目前无法使用(由于超载或停机维护)。通常,这只是暂时状态。
504 (网关超时)- 服务器作为网关或代理,但是没有及时从上游服务器收到请求。
505 (HTTP 版本不受支持)- 服务器不支持请求中所用的 HTTP 协议版本
bash第一步: 安装nginx服务
第二步: 配置nginx server
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
index index.html index.htm;
}
}
第三步: 创建代码目录👈
[root@web01 /etc/nginx/conf.d]#mkdir -p /code/xbw
第四步: 上传游戏代码(公司业务 首页 开发给我们的)
[root@web01 /code/xbw]#rz -E
[root@web01 /code/xbw]#unzip 1.zip
[root@web01 /code/xbw]#ll
total 7768
-rw-r--r-- 1 root root 7902976 Apr 7 11:36 1.zip
-rw-r--r-- 1 root root 28032 May 24 2021 bgm.mp3
drwxr-xr-x 2 root root 23 May 24 2021 css
drwxr-xr-x 2 root root 23 May 24 2021 images
-rw-r--r-- 1 root root 8956 May 24 2021 index.html
drwxr-xr-x 2 root root 213 May 24 2021 js
drwxr-xr-x 2 root root 4096 May 24 2021 roms
-rw-r--r-- 1 root root 811 May 24 2021 shuoming.html
第五步: nginx重新加载生效
[root@web01 /code/xbw]#nginx -t
[root@web01 /code/xbw]#systemctl reload nginx
访问测试:
第一种通过IP地址访问
浏览器-->10.0.0.7
第二种通过域名方式访问
windows hosts文件的位置:
C:\Windows\System32\drivers\etc
修改hosts后测试有没有配置成功

bash成功通过浏览器访问域名测试
浏览器-->xbw.oldboy.com
如果访问不到,注意清理浏览器缓存,或者换个浏览器访问测试。
打开浏览器按 ctrl+shift+n 进入无痕模式访问xbw.oldboy.com
bashNginx部署多业务: 配置多个server
www.oldboy.com-->网站首页
blog.oldboy.com-->博客
images.oldboy.com-->图片
# 三种方式配置:
bash1.在单块网卡上配置两个IP地址(了解)
[root@web01 /code/xbw]#ip addr add 10.0.0.6/24 dev ens33
[root@web01 /code/xbw]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:02:88:28 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 10.0.0.6/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe02:8828/64 scope link
valid_lft forever preferred_lft forever
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:02:88:32 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.7/24 brd 172.16.1.255 scope global noprefixroute ens36
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe02:8832/64 scope link
valid_lft forever preferred_lft forever
2.配置基于IP的多业务
访问10.0.0.7--->访问小霸王游戏
[root@web01 /code/xbw]#cat /etc/nginx/conf.d/xbw.conf
server {
listen 10.0.0.7;
server_name _;
location / {
root /code/xbw;
index index.html index.htm;
}
}

bash访问10.0.0.6--->访问捕鱼游戏
#创建捕鱼目录
[root@web01 /code/xbw]#mkdir /code/by
#捕鱼代码上传到目录
[root@web01 /code/by]#rz -E
rz waiting to receive.
#解压
[root@web01 /code/by]#unzip by
[root@web01 /code/by]#ll
total 4528
-rw-r--r-- 1 root root 4579084 Apr 7 14:52 by.zip
-rw-r--r-- 1 root root 47887 Dec 21 2011 fishjoy.game.js
drwxr-xr-x 2 root root 4096 Mar 25 2015 images
-rw-r--r-- 1 root root 2614 Mar 25 2015 index.html
drwxr-xr-x 2 root root 43 Mar 25 2015 js
drwxr-xr-x 3 root root 126 Mar 25 2015 src
[root@web01 /code/by]#cp /etc/nginx/conf.d/xbw.conf /etc/nginx/conf.d/by.conf
#编辑配置文件
[root@web01 /code/by]#vim /etc/nginx/conf.d/by.conf
[root@web01 /code/by]#cat /etc/nginx/conf.d/by.conf
server {
listen 10.0.0.6;
server_name _;
location / {
root /code/by;
index index.html index.htm;
}
}
#检查代码文件
[root@web01 /code/by]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重启服务
[root@web01 /code/by]#systemctl restart nginx
#查看端口
[root@web01 /code/by]#netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 10.0.0.7:80 0.0.0.0:* LISTEN 7050/nginx: master
tcp 0 0 10.0.0.6:80 0.0.0.0:* LISTEN 7050/nginx: master
#ok

bash基于多端口的配置:(企业中用的多 掌握的配置)
80端口用户访问的页面,88 89 可以定义其他的端口作为后台管理接口。
访问10.0.0.7:88-->对应的小霸王
[root@web01 /code/by]#cat /etc/nginx/conf.d/xbw.conf
server {
listen 88;
server_name _;
location / {
root /code/xbw;
index index.html index.htm;
}
}
访问10.0.0.7:99-->对应的捕鱼
[root@web01 /code/by]#cat /etc/nginx/conf.d/by.conf
server {
listen 99;
server_name _;
location / {
root /code/by;
index index.html index.htm;
}
}
#检查配置并重启
[root@web01 /code/by]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /code/by]#systemctl restart nginx
#网页测试,默认80端口,如果不是80,要加上端口号访问


bash[root@web01 /code/by]#netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 5932/sshd: /usr/sbi
tcp 0 0 0.0.0.0:88 0.0.0.0:* LISTEN 7123/nginx: master
tcp 0 0 0.0.0.0:99 0.0.0.0:* LISTEN 7123/nginx: master
tcp6 0 0 :::22 :::* LISTEN 5932/sshd: /usr/sbi
bashwww.baidu.com----->首页搜索引擎
news.baidu.com--->新闻页面
tieba.baidu.com-->贴吧业务
#xbw.oldboy.com--->小霸王
[root@web01 /code/by]#cat /etc/nginx/conf.d/xbw.conf
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
index index.html index.htm;
}
}
#by.oldboy.com---->捕鱼业务
[root@web01 /code/by]#cat /etc/nginx/conf.d/by.conf
server {
listen 80;
server_name by.oldboy.com;
location / {
root /code/by;
index index.html index.htm;
}
}
#检查配置文件并且重启生效
[root@web01 /code/by]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /code/by]#systemctl restart nginx
#修改windows的hosts解析
10.0.0.7 xbw.oldboy.com by.oldboy.com
#ping测试

bash浏览器测试


bash#js.oldboy.com--->植物大战僵尸
#创建目录
[root@web01 /code/js]#mkdir -p /code/js
#下载到目录
[root@web01 /code/js]#rz -E
#解压缩
[root@web01 /code/js]#unzip zombies-master.zip
#修改配置文件
[root@web01 /code/js]#cat /etc/nginx/conf.d/js.conf
server {
listen 80;
server_name sj.oldboy.com;
location / {
root /code/js;
index index.html index.htm;
}
}
#检查并重启
[root@web01 /code/js]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /code/js]#systemctl restart nginx
#修改host文件
10.0.0.7 xbw.oldboy.com by.oldboy.com js.oldboy.com
#ping测试

bash#浏览器测试
bashNginx日志格式: 默认/etc/nginx/nginx.conf记载,输出到/var/log/nginx/access.log error.log
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
$remote_addr # 记录访问我的客户端的IP地址,也就是VMnet8的地址,
# 如果访问云服务器的ip地址是8.148.236.36,我客户端ip地址是192.16.8段,局域网出不去,通过NAT网络转换成公网ip地址才可以访问
# 记录的就是出网ip地址,如何查看公网ip,百度输入ip138.com,linux输入curl cip.cc查看
$remote_user #远程用户,现在是空的,因为没登陆过
$time_local #本地时间
$request #请求的内容URL,GET是请求方法,访问的是 /
#$request
#完整地址,可直接访问资源,包含完整协议(http)、域名(www.jd.com)和资源路径(/xx/bj.html),
URL:http://www.jd.com/xx/bj.html
#仅表示资源的路径标识符,服务器上具体的资源
URI:/xx/bj.html
请求方法: 用户对当前的页面干啥
GET浏览器 查看 获取
POST 上传内容,创建新的页面
DELETE 删除一个页面
#access.log信息
10.0.0.1 - - [08/Apr/2025:16:01:27 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "-"
#error.log信息
2025/04/08 09:09:41 [error] 7708#7708: *16 open() "/code/xbw/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: xbw.oldboy.com, request: "GET /favicon.ico HTTP/1.1", host: "xbw.oldboy.com", referrer: "http://xbw.oldboy.com/1.txt"

bashroot /code/by:指定用户访问代码目录为 /code/by
用户访问by.oldboy.com/,那么访问的就是/code/by目录,这个/号就等于/code/by,因为前面location匹配到这个/号了。
如果用户访问的是by.oldboy.com/test,那么用户访问的是/code/by/test
#测试,显示没有这个目录

bash查看日志发现/code/xbw/test/1.txt报错,所以如果用户访问的是by.oldboy.com/test,那么用户访问的是/code/by/test
[root@web01 /code/xbw]#cat /var/log/nginx/error.log
2025/04/08 16:37:29 [error] 7708#7708: *26 open() "/code/xbw/test/1.txt" failed (2: No such file or directory), client: 10.0.0.1, server: xbw.oldboy.com, request: "GET /test/1.txt HTTP/1.1", host: "xbw.oldboy.com"
#创建目录
[root@web01 /code/xbw]#mkdir /code/xbw/test
[root@web01 /code/xbw]#echo 123 >/code/xbw/test/1.txt
# 测试

bash[root@web01 /code/xbw]#cat /var/log/nginx/access.log
10.0.0.1 - - [08/Apr/2025:16:57:39 +0800] "GET /test/1.txt HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "-"
$status #HTTP状态码304
$body_bytes_sent # 页面内容大小0
"$http_referer" # 在哪个页面过来的无
"$http_user_agent" # UA客户端的信息,Mozilla...Edg/135.0.0.0
"$http_x_forwarded_for" # 如果前端有代理的情况下记录真实的IP地址,比如前面有负载均衡(10.0.0.5),客户端的ip是10.0.0.1,所以记录的是真实的客户端ip(10.0.0.1)
IP # 独立的IP地址 公网IP地址
UV # 独立访客 表示一台设备
PV # 点击一次页面,相当一个PV,打开一个页面相当于一个PV,刷新一次相当于1个PV
#面试题,你们公司有多少IP,多少PV,多少UV
日IP约:8000
日UV:约1万
日PV:约10万
假设公司有一座大厦,大厦有100人,每个人有一台电脑和一部手机,上网都是通过nat转换出口,每个人点击网站2次, 请问对应的pv,uv,ip分别是多少?
IP=1个
UV: 100X2=200
PV: 200X2=400PV
bash#Nginx路径理解,很重要!!!!
[root@web01 /code/xbw]#cat /etc/nginx/conf.d/xbw.conf
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
index index.html index.htm;
}
location /download { #/download实际路径是/code/download,在局部定义路径,不影响全局,首页还是正常访问
root /code;
index index.html;
}
}
#如果是下面这种情况,全局指定默认/路径
server {
listen 80;
server_name xbw.oldboy.com;
root /code/xbw; #定义全局,下面所有location的跟都是访问到这里
location / { #访问 /就是在/code/xbw返回index.html内容
index index.html index.htm;
}
location /download { #访问/download就是在/code/xbw/download里面去找index.html
index index.html;
}
}
#以下是日志,/code/xbw/download/index.html" is not found
2025/04/08 19:27:43 [error] 10313#10313: *1 "/code/xbw/download/index.html" is not found (2: No such file or directory), client: 10.0.0.1, server: xbw.oldboy.com, request: "GET /download/ HTTP/1.1", host: "xbw.oldboy.com"
#alias直接指定路径
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
index index.html index.htm;
}
location /download {
alias /code; #不管location的定义,直接去找/code路径,如果有用户访问的是xbw.oldboy.com/download --->/code/index.html
index index.html;
}
}
2025/04/08 19:39:04 [error] 10375#10375: *1 directory index of "/code/" is forbidden, client: 10.0.0.1, server: xbw.oldboy.com, request: "GET /download/ HTTP/1.1", host: "xbw.oldboy.com"
bash[root@web01 /code/xbw]#cat /etc/nginx/conf.d/xbw.conf
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
index index.html index.htm;
}
location /download {
root /code;
index index.html;
autoindex on; #--开启目录索引,前提是要把目录下面的lndex.html删掉
}
}
[root@web01 /code/download]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /code/xbw]#systemctl restart nginx
[root@web01 /code/download]#mkdir {01..10}.test
#传文件测试看效果,访问http://xbw.oldboy.com/download/就是访问/code/download目录

bash#因为路径匹配原则,如果访问http://xbw.oldboy.com/code/download/就报错了,看错误日志显示/code/xbw/code/download/index.html" is not found (2: No such file or directory),因为/code/download路径在配置文件里没有,所以会优先匹配/,所以会从/code/xbw/code/download开始匹配
bash中文显示乱码
[root@web01 /code/download]#cat /etc/nginx/conf.d/xbw.conf
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
index index.html index.htm;
}
location /download {
root /code;
charset utf-8,gbk; #--修改字符集,局部生效,如果写到server里面,是xbw生效,如果写到http里面,所有server生效
index index.html;
}
}
#测试
bashserver {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
index index.html index.htm;
}
location /download {
root /code; # 服务器中的位置/code/download
charset utf-8,gbk;
autoindex_localtime on; # 显示本地时间
autoindex_exact_size off; # 文件大小mb优化
index index.html;
}
}

bash#需求 只允许10.0.0.1访问xbw.oldboy.com 其他都拒绝
[root@web01 /code/download]#cat /etc/nginx/conf.d/xbw.conf
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
allow 10.0.0.1; #只允许10.0.0.1访问,如果想允许多个ip段,可以# allow 10.0.0.1;
deny all; #拒绝所有 # allow 172.0.0.2;
index index.html index.htm; # allow 192.0.0.1;
}
location /download {
root /code;
index index.html;
}
}
#41服务器做host解析
[root@backup ~]#cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.7 xbw.oldboy.com
#别的ip访问失败
[root@backup ~/.ssh]#curl xbw.oldboy.com
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.26.1</center>
</body>
</html>
#10.0.0.1访问成功

bash#去掉allow deny all; 正常访问
[root@backup ~/.ssh]#curl xbw.oldboy.com
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="format-detection" content="telephone=no">
<meta name="format-detection" content="email=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="full-screen" content="yes">
<meta name="browsermode" content="application">
<meta name="x5-orientation" content="landscape">
<meta name="x5-fullscreen" content="true">
<meta name="x5-page-mode" content="app">
<link rel="shortcut icon" href="images/index.jpg" type="image/x-icon">
<title>FC怀旧游戏机/免费提供</title>
bash1、先安装httpd-tools,该包中携带了htpasswd命令,默认已经安装
2、生成密码文件(因为需要需要加密的才行) #用户名 密码
[root@web01 ~]#htpasswd -b -c /etc/nginx/auth_conf oldboy oldboy
Adding password for user oldboy
# -b 允许在命令行直接输入密码 免交互
# -c 创建一个文件 auth_conf
# 查看密码文件
[root@web01 ~]#cat /etc/nginx/auth_conf
oldboy:$apr1$yAk6H0EF$CtVwYC2JOQYiauEqK8IEy0
3、配置模块
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
index index.html index.htm;
}
location /download {
root /code;
charset utf-8,gbk;
autoindex_localtime on;
autoindex_exact_size off;
auth_basic "hehe"; # 认证描述模块 自定义内容必须存在
auth_basic_user_file auth_conf; # 指定密码文件的位置。这里不用写绝对路径,因为include把这个server引用到 了/etc/nginx/nginx.conf文件里面的http区块
index index.html;
autoindex on;
}
}
#测试

bash七种状态
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
index index.html index.htm;
}
location /nginx_status{ #用户访问xbw.oldboy.com/nginx_status 返回七种状态,nginx_status可以自定义
stub_status; #返回这个模块名
}
location /download {
root /code;
index index.html;
}
}
#状态模块解读
Active connections: 2
server accepts handled requests
2 2 1
Reading: 0 Writing: 1 Waiting: 1
Active connections 2 # 当前活跃的TCP连接数,已经建立三次握手的,多少个已经和我建立连接的
accepts 2 # nginx启动以来,已接收的总TCP连接数量
handled 2 # 成功处理的TCP连接数量(通常等于 accepts,若不等可能因资源不足或配置问题)
requests 1 # 当前http请求数,基于TCP三次握手之上,请求数就是服务器给你提供的页面上有多少资源,比如访问 xbw.oldboy.com/1.txt那就是1个请求(资源)
Reading 0 # 当前读取请求头数量,记载着文档类型,html、txt或者别的,请求的语言,短链接长连接,域名是什么..等等(解析请求阶段)
Writing 1 # 当前响应的请求头数量,响应长连接,长度多大,类型,时间,服务是什么,nginx等等(处理响应阶段)
Waiting 1 # 等待的请求数,开启了keepalive,响应完之后等待看有没有新的响应
排查问题,比如昨天Active connections连接数是2万个,今天已经超过3万了,需要排查是做活动了还是有黑客攻击,接受了accepts 2万的请求, handled才处理了1万,也就是说处理失败了1万个,需要看看页面不存在了等待
#长短连接
http1.0:短连接,一次连接一次请求,比如16个资源,三次握手之后,先请求1个,响应1个,完成后断开,再三次握手再请求第2个,消耗时间
http1.1:长连接,一次连接,多次请求,三次握手之后,请求-响应-请求-响应,串行的,全部响应完之后再断开
http2.0:多次连接多次请求,并行连接,建立连接后多次请求,服务器多次进行响应
# 注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s没有活动则断开连接
#测试

bash#limit_conn_zone 模块名称
#$remote_addr 客户端IP地址
#zone 开辟内存空名称conn_zone 大小为10m
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
limit_conn_zone $remote_addr zone=conn_zone:10m;
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
limit_conn conn_zone 1; #1秒只能有一个连接
index index.html index.htm;
}
location /nginx_status{
stub_status;
}
location /download {
root /code;
index index.html;
}
}
bash[root@web01 /etc/nginx/conf.d]#cat xbw.conf
#limit_conn_zone $remote_addr zone=conn_zone:10m;
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=5r/s;
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
#limit_conn conn_zone 1;
limit_req zone=req_zone burst=5; # 每秒最多5个请求延迟处理5个,剩下的拒绝
index index.html index.htm;
}
location /download {
root /code;
index index.html;
}
}
#测试,处理不完
bash#修改为
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
#limit_conn_zone $remote_addr zone=conn_zone:10m;
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=20r/s;
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
#limit_conn conn_zone 1;
limit_req zone=req_zone burst=15; # 每秒最多20个请求延迟处理15个,剩下的拒绝
index index.html index.htm;
}
location /download {
root /code;
index index.html;
}
}
bash自定义返回错误状态码:
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
#limit_conn_zone $remote_addr zone=conn_zone:10m;
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
#limit_conn conn_zone 1;
limit_req zone=req_zone burst=1; # 请求跟延迟处理都改为1
limit_req_status 478; # 被限制的请求默认返回478状态码。
index index.html index.htm;
}
location /download {
root /code;
index index.html;
}
}
bash#可以将错误代码重定向到html页面
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
#limit_conn_zone $remote_addr zone=conn_zone:10m;
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
server {
listen 80;
server_name xbw.oldboy.com;
location / {
root /code/xbw;
#limit_conn conn_zone 1;
limit_req zone=req_zone burst=1;
limit_req_status 478; # 错误状态码
error_page 478 /err.html; # 遇到错误478进行页面重定向到err.html,路径/code/xbw/err.html
index index.html index.htm;
}
location /download {
root /code;
index index.html;
}
}
#创建/code/xbw/err.html错误文件
[root@web01 /etc/nginx/conf.d]#cat /code/xbw/err.html
www.4399.com
bash面试题: 说一下location的匹配规则。
匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 4
/ 通用匹配,任何请求都会匹配到 5
server {
listen 80;
server_name test.oldboy.com;
default_type text/html;
location = / {
return 200 "configuration A";
}
location / {
return 200 "configuration B";
}
location /documents/ {
return 200 "configuration C";
}
location ^~ /images/ {
return 200 "configuration D";
}
location ~* \.(gif|jpg|jpeg)$ {
return 200 "configuration E";
}
}
bash1.安装goaccess软件
[root@web01 ~]#yum -y install goaccess
2.配置
[root@web01 ~]#vim /etc/goaccess/goaccess.conf
#找到一下三行将注释去掉
time-format %H:%M:%S
date-format %d/%b/%Y
log-format %h %^[%d:%t %^] "%r" %s %b "%R""%u"
3.配置Nginx
[root@web01 /etc/nginx/conf.d]#cp by.conf go.conf
[root@web01 /etc/nginx/conf.d]#cat go.conf
server {
listen 80;
server_name go.oldboy.com;
location / {
root /code/log;
index index.html index.htm;
}
}
4.检查语法重启生效
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
5.配置相关数据
[root@web01 /etc/nginx/conf.d]#mkdir /code/log
6.配置host文件
7.启动goaccess
[root@web01 /etc/nginx/conf.d]#goaccess -f /var/log/nginx/access.log -o /code/log/index.html -p /etc/goaccess/goaccess.conf --real-time-html &
[1] 1490
WebSocket server ready to accept new client connections

bashLNMP架构概述 LNMP是一套技术的组合,L=Linux、N=Nginx、M~=Mysql、P~=PHP 静态数据: 图片 音频 视频,不能交互的。例如静态HTML页面。 Nginx静态处理 静态+动态数据: 个人信息 注册 修改 登录 文字 -->数据库 LNMP架构 LNMT Linux Nginx MySQL PHP Linux Nginx MySQL Python Linux Nginx MySQL Tomcat

bash#1.使用官方仓库安装Nginx
[root@web01 ~]#cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
#2.安装Nginx
[root@web01 ~]#rpm -qa nginx
nginx-1.26.1-2.el7.ngx.x86_64
#3.启动Nginx
[root@web01 ~]#systemctl start nginx
[root@web01 ~]#systemctl enable nginx
bash1.安装
[root@web01 ~]#yum -y install php php-bcmath php-cli php-common php-devel php-embedded php-fpm php-gd php-intl php-mbstring php-mysqlnd php-opcache php-pdo php-process php-xml php-json
2.修改配置文件为监听端口的方式运行
[root@web01 ~]#grep '9000' /etc/php-fpm.d/www.conf -n
39:listen =127.0.0.1:9000;
3.启动PHP
[root@web01 ~]#systemctl start php-fpm
[root@web01 ~]#systemctl enable php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@web01 ~]#netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 2598/php-fpm: maste
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1546/nginx: master
tcp 0 0 0.0.0.0:7890 0.0.0.0:* LISTEN 1490/goaccess
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1068/sshd: /usr/sbi
tcp6 0 0 :::22 :::* LISTEN 1068/sshd: /usr/sbi
bash1.安装mariadb服务
[root@web01 ~]#yum -y install mariadb-server
2.启动mariadb服务
[root@web01 ~]#systemctl start mariadb
[root@web01 ~]#systemctl enable mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
3.配置默认的root密码
[root@web01 ~]#mysqladmin password 'lzy123.com'
登录测试
[root@web01 ~]#mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.39-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)
bash1.修改nginx配置
server {
listen 80;
server_name php.oldboy.com;
location / {
root /code;
index index.php index.html;
}
location ~\.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#客户端请求http://php.oldboy.com/foo.php,nginx匹配到location \.php$,会去/code目录下找,如果能找到,fastcgi_pass(Nginx 中用于处理动态内容)将php请求转发到fastcgi服务,一般是php-fpm服务,监听在本地127.0.0.1:9000,通过PHP-FPM脚本的关键参数(传参)以及加载默认的FastCGI参数配置文件,PHP-FPM执行/code/foo.php并返回结果,nginx将响应返回给客户端
[root@web02 /etc/nginx/conf.d]#cat /etc/nginx/fastcgi_params
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
#我们访问php.oldboy.com,默认会根据location匹配原则返回php.oldboy.com/index.php,也就是匹配下面location ~给用户,因为优先级高,所以也就是说有用户访问.php结尾的,由我这个locatio ~来处理,具体怎么处理呢,会查找/code目录,fastcgi_pass是将用户的请求转发给127.0.0.1:9000端口去解析,但是php不知道127.0.0.1:9000是哪个资源,所以由下面的变量来传参

bash2.重启配置
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
3.修改host文件
4.准备phpinfo配置文件,主要查看php本身的信息
[root@web01 /etc/nginx/conf.d]#cat /code/info.php
<?php
phpinfo();
?>
#注意,因为文件名是info.php,所以想访问的话必须加info.php,因为我们配置文件里默认的是index.php
#带info.php浏览器测试

bash#修改文件名测试
[root@web01 /code]#mv info.php index.php
[root@web01 /code]#ll
total 4
drwxr-xr-x 5 root root 109 Apr 9 09:31 by
drwxr-xr-x 12 root root 331 Apr 8 19:50 download
-rw-r--r-- 1 root root 25 Apr 9 15:06 index.php
#浏览器测试


bash#配置连接数据库的代码文件,用普通用户
[root@web01 /code]#cat mysql.php
<?php
$servername = "127.0.0.1";
$username = "lzy";
$password = "lzy123.com";
// 创建连接
$conn = mysqli_connect($servername,$username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " .mysqli_connect_error());
}
echo "小哥哥,php可以连接MySQL...";
?>
<img style='width:100%;height:100%;'src=7.jpg>
#把7.jpg文件放入到/code下
访问php.oldboy.com/mysql.php,测试连接

bash第一步: Nginx创建wordpress的server
[root@web01 /etc/nginx/conf.d]#cat wordpress.conf
server {
listen 80;
server_name wp.oldboy.com;
root /code/wp;
location / {
index index.php index.html;
}
location ~\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
第二步: 创建代码目录
[root@web01 /etc/nginx/conf.d]#mkdir /code/wp
[root@web01 /etc/nginx/conf.d]#cd /code/wp
第三步: 下载代码
[root@web01 /code/wp]#wget https://cn.wordpress.org/wordpress-5.0.3-zh_CN.tar.gz
解压代码
[root@web01 /code/wp]#tar xf wordpress-5.0.3-zh_CN.tar.gz -C /code/wp/
将代码移动到/code/wp目录
第四步: hosts解析
10.0.0.7 wp.oldboy.com
浏览器访问
bash创建一个库名称为wordpress
[root@web01 /code]#mysql -uroot -plzy123.com -e "create database wordpress;"
[root@web01 /code]#mysql -uroot -plzy123.com -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| wordpress |
+--------------------+

bash提交后会报错,现在以nginx的启动用户(nginx)往里写入,但是动态页面解析是由PHP服务完成的,PHP服务用户是apche,所以要统一nginx和php的启动用户
[root@web01 /code]#vim /etc/php-fpm.d/www.conf
[root@web01 /code]#grep nginx /etc/php-fpm.d/www.conf -n
24:user = nginx
26:group = nginx
修改后重启生效
[root@web01 /code]#systemctl restart php-fpm
[root@web01 /code]#ps axu|grep php
root 4033 0.1 3.0 225292 29700 ? Ss 16:04 0:00 php-fpm: master process (/etc/php-fpm.conf)
nginx 4035 0.0 1.2 235844 12304 ? S 16:04 0:00 php-fpm: pool www
nginx 4036 0.0 1.2 235844 12304 ? S 16:04 0:00 php-fpm: pool www
nginx 4037 0.0 1.2 235844 12304 ? S 16:04 0:00 php-fpm: pool www
nginx 4038 0.0 1.2 235844 12304 ? S 16:04 0:00 php-fpm: pool www
nginx 4039 0.0 1.2 235844 12304 ? S 16:04 0:00 php-fpm: pool www
修改完成后将/code/wp属主属组改为Nginx用户
[root@web01 /code]#chown -R nginx.nginx /code/wp/
回到页面重新提交数据信息(点击提交 -->重新输入数据库信息到下面的步骤点击提交)



bash1.Nginx配置知乎server
[root@web01 /etc/nginx/conf.d]#cat zh.conf
server {
listen 80;
server_name zh.oldboy.com;
root /code/zh;
location / {
index index.php index.html;
}
location ~\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: [warn] conflicting server name "wp.oldboy.com" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
2.创建代码目录
[root@web01 /code]#mkdir /code/zh
[root@web01 /code]#cd /code/zh
3.下载或者上传代码
[root@web01 /code/zh]#rz -E
[root@web01 /code/zh]#unzip WeCenter_V3.6.2.zip
修改代码目录属主属组
[root@web01 /code/zh]#chown -R nginx.nginx /code/zh
4.hosts解析
10.0.0.7 wp.oldboy.com zh.oldboy.com
浏览器测试
#注意nginx和php的权限要一样的
bash创建zh数据库
[root@web01 /code]#mysql -uroot -plzy123.com -e "create database zh;"
[root@web01 /code]#mysql -uroot -plzy123.com -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| wordpress |
| zh |
+--------------------+



bash克隆200模板机
1.部署mysql服务器 10.0.0.51 172.16.1.51
[root@oldboy ~]#yum -y install mariadb-server
启动数据库
[root@db01 ~]#systemctl start mariadb-server
2.将老的数据库中的数据导出
[root@web01 ~]#mysqldump -uroot -plzy123.com -A > all.sql
[root@web01 ~]#ll
total 3364
-rw-r--r-- 1 root root 3442605 Apr 9 16:39 all.sql
3.将导出的数据库导入到10.0.0.51新的数据库
[root@web01 ~]#vim all.sql
#scp传到51库
[root@web01 ~]#scp all.sql 10.0.0.51:/root
The authenticity of host '10.0.0.51 (10.0.0.51)' can't be established.
ECDSA key fingerprint is SHA256:Fe+StTKc+1JoYwNC0wQDHkY4QAuKr+cB3wGKah/fzkM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.51' (ECDSA) to the list of known hosts.
Authorized users only. All activities may be monitored and reported.
root@10.0.0.51's password:
all.sql
[root@db01 ~]#mysql -uroot <all.sql
重启生效
[root@db01 ~]#systemctl restart mariadb
检查导入是否成功
[root@db01 ~]#mysql -uroot -plzy123.com -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| wordpress |
| zh |
+--------------------+
4.测试web01能不能使用root连接10.0.0.51的数据库
#默认数据库禁止root远程连接
[root@web01 ~]#mysql -h 10.0.0.51 -uroot -plzy123.com
ERROR 1130 (HY000): Host '10.0.0.7' is not allowed to connect to this MariaDB server
#自己手动创建可以远程连接的普通用户 lzy 密码是lzy123.com
[root@db01 ~]#mysql -uroot -plzy123.com
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.39-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
#创建用户
MariaDB [(none)]> grant all on *.* to lzy@'%' identified by 'lzy123.com';
Query OK, 0 rows affected (0.000 sec)
#完成后再web01尝试远程登录
[root@web01 ~]#mysql -h 10.0.0.51 -ulzy -plzy123.com
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.3.39-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
# 登录成功后再修改业务的配置文件
5.修改代码配置文件指向新的数据库
#关闭web01数据库
[root@web01 ~]#systemctl stop mariadb.service
[root@web01 ~]#systemctl disable mariadb.service
#修改wp配置文件
[root@web01 ~]#cd /code/wp/
[root@web01 /code/wp]#vim wp-config.php
[root@web01 /code/wp]#grep DB wp-config.php
define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'lzy' );
define( 'DB_PASSWORD', 'lzy123.com' );
define( 'DB_HOST', '172.16.1.51' );
define( 'DB_CHARSET', 'utf8mb4' );
define( 'DB_COLLATE', '' );
define( 'AUTH_SALT', '=4;VPKH!L<n/qp]G>iUVoEf[W4_?wJxv&9OrODBXbf]qT3{@*kH#9ij!;v@Ztt{`' );
#测试,恢复了

bash----------修改zh代码指向新的数据库
#递归过滤文件的内容
[root@web01 /code/zh]#grep -r 'lzy123.com' ./*
./system/config/database.php: 'password' => 'lzy123.com',
[root@web01 /code/zh]#cat system/config/database.php
<?php
$config['charset'] = 'utf8mb4';
$config['prefix'] = 'aws_';
$config['driver'] = 'MySQLi';
$config['master'] = array (
'charset' => 'utf8mb4',
'host' => '172.16.1.51',
'username' => 'lzy',
'password' => 'lzy123.com',
'dbname' => 'zh',
'port' => '3306',
);
$config['slave'] = false;
$config['port'] = '3306';
#测试知乎

bash#1.配置文件server
[root@web01 /code/phpshe]#cat /etc/nginx/conf.d/phpshe.conf
server {
listen 80;
server_name phpshe.oldboy.com;
root /code/phpshe;
location / {
index index.php index.html;
}
location ~\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#2.创建代码目录
[root@web01 /code]#mkdir /code/phpshe
[root@web01 /code]#cd /code/phpshe
#3.下载或者上传代码
[root@web01 /code/zh]#rz -E
[root@web01 /code/phpshe]#unrar x phpshe1.8.rar
#4.修改代码目录属主属组
[root@web01 /code/phpshe]#chown -R nginx.nginx /code/phpshe/
#5.配置host
10.0.0.7 phpshe.oldboy.com
#6.重启服务
[root@web01 /code/phpshe]#systemctl restart nginx
#7创建she数据库
[root@web01 /code]#mysql -uroot -plzy123.com -e "create database she;"
[root@web01 /code]#mysql -uroot -plzy123.com -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| she |
| wordpress |
| zh |
+--------------------+

bash当流量高,访问量过大的时候,单台web服务器扛不住,这时候就要加一台,将流量进行分摊
1.克隆一台WEB02服务器
修改33网卡IP、36网卡IP,修改主机名称
2.安装nginx
#先将web01服务器的nginx源拷贝到本机
[root@web02 ~]#scp 10.0.0.7:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:Fe+StTKc+1JoYwNC0wQDHkY4QAuKr+cB3wGKah/fzkM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
Authorized users only. All activities may be monitored and reported.
root@10.0.0.7's password:
nginx.repo
#执行安装
[root@web02 ~]#yum -y install nginx
[root@web02 ~]#nginx -v
nginx version: nginx/1.26.1
3.安装php
[root@web02 ~]#yum -y install php php-bcmath php-cli php-common php-devel php-embedded php-fpm php-gd php-intl php-mbstring php-mysqlnd php-opcache php-pdo php-process php-xml php-json
4.同步配置文件
#将10.0.0.7的配置文件同步到web02
[root@web02 ~]#rsync -avz --delete 10.0.0.7:/etc/nginx /etc/
[root@web02 ~]#scp 10.0.0.7:/etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf
Authorized users only. All activities may be monitored and reported.
root@10.0.0.7's password:
receiving incremental file list
deleting nginx/conf.d/default.conf
nginx/
nginx/auth_conf
nginx/conf.d/
nginx/conf.d/by.conf
nginx/conf.d/go.conf
nginx/conf.d/jd.conf
nginx/conf.d/js.conf
nginx/conf.d/php-fpm.conf
nginx/conf.d/phpshe.conf
nginx/conf.d/wordpress.conf
nginx/conf.d/xbw.conf
nginx/conf.d/zh.conf
nginx/default.d/
sent 240 bytes received 2,579 bytes 626.44 bytes/sec
total size is 47,136 speedup is 16.72
5.检查配置文件
[root@web02 ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 ~]#php-fpm -t
[10-Apr-2025 16:13:30] NOTICE: configuration file /etc/php-fpm.conf test is successful
6.启动Nginx和php服务
[root@web02 ~]#systemctl start nginx
[root@web02 ~]#systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@web02 ~]#systemctl start php-fpm
[root@web02 ~]#systemctl enable php-fpm
[root@web02 ~]#netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 2568/php-fpm: maste
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2406/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1069/sshd: /usr/sbi
tcp6 0 0 :::22 :::* LISTEN 1069/sshd: /usr/sbi
udp 0 0 127.0.0.1:323 0.0.0.0:* 757/chronyd
udp6 0 0 ::1:323 :::* 757/chronyd
7.将web01的code代码目录打包拷贝到web02
[root@web02 ~]#scp -r 10.0.0.7:/code /
8.修改权限
[root@web02 /code]#chown -R nginx.nginx /code/zh
[root@web02 /code]#chown -R nginx.nginx /code/wp
#测试host解析到10.0.0.8

bash#测试。在10.0.0.8上传图片,解析到10.0.0.7进行访问

bash#在10.0.0.7访问,因为在8服务器上传,图片是保存在8的服务器导致7访问不到图片,通过下面的请求 URL:http://wp.oldboy.com/wp-content/uploads/2025/04/7.jpg,可以看到

bash1.部署NFS服务
[root@nfs ~]#yum -y install nfs-utils
2.配置NFS服务
[root@nfs ~]#cat /etc/exports
/data/wp 172.16.1.0/24(rw,sync,all_squash,anonuid=200,anongid=200)
3.创建必要数据
[root@nfs ~]#mkdir -p /data/wp
[root@nfs ~]#id zhang
uid=200(zhang) gid=200(zhang) groups=200(zhang)
[root@nfs ~]#chown zhang.zhang /data/wp
[root@nfs ~]#ll -d /data/wp/
drwxr-xr-x 2 zhang zhang 6 Apr 10 17:13 /data/wp/
4.启动NFS服务
[root@nfs ~]#systemctl start nfs
[root@nfs ~]#systemctl enable nfs
5.推送图片到NFS共享目录,因为直接挂载会覆盖原图片导致之前的图片无法显示
[root@web02 ~]#scp -r /code/wp/wp-content/uploads/* 10.0.0.31:/data/wp/
The authenticity of host '10.0.0.31 (10.0.0.31)' can't be established.
ECDSA key fingerprint is SHA256:Fe+StTKc+1JoYwNC0wQDHkY4QAuKr+cB3wGKah/fzkM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.31' (ECDSA) to the list of known hosts.
Authorized users only. All activities may be monitored and reported.
root@10.0.0.31's password:
7.jpg 100% 405KB 8.7MB/s 00:00
7-300x200.jpg 100% 17KB 1.0MB/s 00:00
7-150x150.jpg 100% 7626 567.1KB/s 00:00
7-768x513.jpg 100% 102KB 4.5MB/s 00:00
0c0e7c61-807f-410a-b871-3041a3e67185.png 100% 20KB 11.0MB/s 00:00
0c0e7c61-807f-410a-b871-3041a3e67185-300x183.png 100% 17KB 207.9KB/s 00:00
0c0e7c61-807f-410a-b871-3041a3e67185-1024x625.png 100% 115KB 20.1MB/s 00:00
0c0e7c61-807f-410a-b871-3041a3e67185-150x150.png 100% 5563 3.2MB/s 00:00
0c0e7c61-807f-410a-b871-3041a3e67185-768x469.png
#授权,因为从web02同步了一遍数据,导致目录的权限是root而非www用户
[root@nfs ~]#chown -R www.www /data/wp/
6.WEB01和WEB02挂载到NFS
[root@web01 ~]#showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/wp 172.16.1.0/24
[root@web01 ~]#mount -t nfs 172.16.1.31:/data/wp /code/wp/wp-content/uploads
[root@web01 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 459M 0 459M 0% /dev
tmpfs 475M 0 475M 0% /dev/shm
tmpfs 475M 13M 462M 3% /run
tmpfs 475M 0 475M 0% /sys/fs/cgroup
/dev/mapper/klas-root 47G 4.6G 43G 10% /
/dev/sda1 1014M 169M 846M 17% /boot
tmpfs 95M 0 95M 0% /run/user/0
172.16.1.31:/data/wp 47G 3.8G 44G 8% /code/wp/wp-content/uploads
[root@web02 ~]#mount -t nfs 172.16.1.31:/data/wp /code/wp/wp-content/uploads
[root@web02 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 459M 0 459M 0% /dev
tmpfs 475M 0 475M 0% /dev/shm
tmpfs 475M 6.9M 468M 2% /run
tmpfs 475M 0 475M 0% /sys/fs/cgroup
/dev/mapper/klas-root 47G 4.3G 43G 9% /
/dev/sda1 1014M 169M 846M 17% /boot
tmpfs 95M 0 95M 0% /run/user/0
172.16.1.31:/data/wp 47G 3.8G 44G 8% /code/wp/wp-content/uploads
#测试web01是否能正常查看
bash#上传图片报错
查看日志: 解决权限问题
[root@web02 ~]#ll -d /var/cache/nginx/
drwxr-xr-x 7 nginx nginx 98 Apr 10 08:58 /var/cache/nginx/
#上传大小限制: Nginx默认是1M
Nginx-->http区块配置
[root@web01 /]#vim /etc/nginx/nginx.conf
[root@web01 /]#systemctl restart nginx
...
access_log /var/log/nginx/access.log
main;
client_max_body_size 10m; # 默认1MB,
限制最大请求体大小
...
#PHP上传限制默认2M,修改为200M
[root@web01 /etc/nginx]#vim /etc/php.ini
[root@web01 /etc/nginx]#systemctl reload php-fpm.service
[root@web02 /etc/nginx]#sed -n '825p' /etc/php.ini
upload_max_filesize = 200M
#不修改默认配置,报错,因为文件太小了
bash#注意修改服务器配置文件参数的时候要看网页后台读取的哪台服务器,如果是10.0.0.7,那就修改10.0.0.7
[root@web02 ~]#cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
client_max_body_size 10m; # 默认1MB,限制最大请求体大小,负载均衡服务器也得设置
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
[root@web01 /]#systemctl restart nginx
#PHP上传限制默认2M,修改为200M
[root@web01 /etc/nginx]#vim /etc/php.ini
[root@web01 /etc/nginx]#systemctl reload php-fpm.service
[root@web02 /etc/nginx]#sed -n '825p' /etc/php.ini
upload_max_filesize = 200M #默认2M
[root@web02 ~]#systemctl restart php-fpm.service

bash反向代理是客户端与后端服务器之间的中介,隐藏真实服务器,处理请求转发、安全防护、缓存加速等 正向代理:由路由器代理着我们客户端去访问浏览器服务端
bash反向代理:代理着服务器去访问我们的客户端网站
bash#匹配规则,如果server反向代理没有写头部信息,那么会返回ip,拿着ip去/etc/nginx/conf.d目录下所有的server去匹配这个ip,如果没找到就返回第一个server,如果写了头部信息,比如wp.oldboy.com,那么也会拿着去匹配所有server
1.安装nginx
#先拷贝nginx源再安装nginx
[root@oldboy ~]#scp 10.0.0.7:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/
[root@lb01 ~]#yum -y install nginx
2.配置nginx为反向代理
server {
listen 80;
server_name wp.oldboy.com;
location / { #如果你访问我这台的wp.oldboy.com,那么会让你去访问172.16.1.7服务器的
proxy_pass http://172.16.1.7;
}
}
[root@oldboy /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
3.启动nginx
[root@oldboy /etc/nginx/conf.d]#systemctl start nginx
[root@oldboy /etc/nginx/conf.d]#systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@oldboy /etc/nginx/conf.d]#netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2078/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1070/sshd: /usr/sbi
tcp6 0 0 :::80 :::* LISTEN 2078/nginx: master
tcp6 0 0 :::22 :::* LISTEN 1070/sshd: /usr/sbi
udp 0 0 127.0.0.1:323 0.0.0.0:* 751/chronyd
udp6 0 0 ::1:323
4.访问测试
hosts解析到10.0.0.5
bash#在配置反向代理的时候有个坑,在配置lb01反向代理的时候,没有添加头部信息,访问页面报404错误,查看lb01服务器错误日志没有报错,访问日志出现404,但是没有具体说明原因,host解析成10.0.0.7就能通,解析成10.0.0.5就不通,
#原因是配置反向代理的server中,因为没有添加头部信息,所以会默认找/etc/nginx/conf.d目录下的第一个配置文件,比如/etc/nginx/conf.d/by,那就会访问by,by找不到就报错,因为我在配置lb01服务器的lb.conf文件server中发现域名是wp.oldboy.com,所以把web01服务器/etc/nginx/conf.d的目录删的只剩下wp目录了,通过查看web01的访问日志发现( "/code/jd/index.php" is not found (2: No such file or directory), client: 10.0.0.5, server: jd.oldboy.com)找到的是jd.oldboy.com,但是jd目录被删了,所以需要重启下服务,就可以访问成功了
bash#通过抓包实现,需要把网段server改成同一网段的ip,要不抓不到
第一次,1访问5,5也就是反向代理服务器,51134端口访问80端口,这时候htttp1.1长连接,头部信息wp.oldboy.com
bash第二次,56353端口访问80端口,此时http协议变成1.0短链接了,并且host头部丢了,按照10.0.0.7访问后端了,所以按照IP地址去访问web01,默认返回第一个
bash#添加头部信息, proxy_set_header Host $http_host;
server {
listen 80;
server_name wp.oldboy.com;
location / {
proxy_pass http://10.0.0.7;
proxy_set_header Host $http_host; #--配置头部信息
proxy_http_version 1.1; #--配置http1.1协议
}
}
#再看知乎抓包信息,已经携带头部访问了

bash#让/var/log/nginx/access.log最后一个位置记录客户端访问的真实ip地址(前端有代理的情况下)
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
[root@lb01 /etc/nginx/conf.d]#cat lb.conf
server {
listen 80;
server_name zh.oldboy.com;
location / {
proxy_pass http://10.0.0.7;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#通过日志查看,10.0.0.1是我们vmnet客户端的ip
10.0.0.5 - - [11/Apr/2025:15:03:11 +0800] "GET /?/crond/run/1744354991 HTTP/1.1" 200 5 "http://zh.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
#反向代理配置说明,负载均衡基于反向代理来做
server {
listen 80;
server_name zh.oldboy.com;
location / {
proxy_pass http://10.0.0.7;
proxy_set_header Host $http_host; #配置头部信息
proxy_http_version 1.1; #配置http1.1协议
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #记录客户端访问的真实ip地址
proxy_connect_timeout 30; #连接超时
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on; #开启内存缓冲区
proxy_buffer_size 32k; #头部缓冲区大小
proxy_buffers 4 128k; #主题缓冲区大小
}
}
#后面业务多要把配置写到文件里,引用include文件,不用单独写到server里面
#放在nginx目录
[root@lb01 /etc/nginx/conf.d]#vim ../proxy_params
[root@lb01 /etc/nginx/conf.d]#cat proxy_params
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
bash为什么要使用负载均衡
当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾
往往我们接触的最多的是SLB(Server Load Balance)负载均衡,实现最多的也是SLB、那么SLB它的调度节点和服务节点通常是在一个地域里面。那么它在这个小的逻辑地域里面决定了他对部分服务的实时性、响应性是非常好的。所以说当海量用户请求过来以后,它同样是请求调度节
点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后在转发给调度节点,调度节点最后响应给用户节点。这样也能实现一个均衡的作用,那么Nginx则是一个典型的SLB
#负载均衡请求后端的时候,是由负载均衡的随机端口和后端的端口建立连接,后端是什么端口跟用户没关系,只跟负载均衡有关
bash#处理的请求是平均的
[root@lb01 /etc/nginx/conf.d]#cat lb.conf
upstream webs { # upstream可以定义地址池webs
server 172.16.1.7; # 端口默认80
server 172.16.1.8;
}
server {
listen 80;
server_name wp.oldboy.com;
location / {
proxy_pass http://webs; # 从上面地址池找, http://表示七层协议
include proxy_params; # 携带的参数,头部,优化等等
}
}
#测试7的日志,4条

bash#测试8的日志,4条

bash#配置两个WEB,实现刷新一下显示wen01,再刷新一下显示web02
1、配置web01静态页面
[root@web01 /code]#cd /etc/nginx/conf.d/
[root@web01 /etc/nginx/conf.d]#cat test.conf
server {
listen 80;
server_name test.oldboy.com;
root /code/test;
index index.html;
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
2、创建代码目录
[root@web01 /etc/nginx/conf.d]#mkdir /code/test
[root@web01 /etc/nginx/conf.d]#echo "web01">/code/test/index.html
[root@web01 /etc/nginx/conf.d]#ll /code/test/
total 4
-rw-r--r-- 1 root root 6 Apr 11 16:16 index.html
3、配置host
10.0.0.7 test.oldboy.com
测试

bash配置web02静态页面
[root@web02 /etc/nginx/conf.d]#cat test.conf
server {
listen 80;
server_name test.oldboy.com;
root /code/test;
index index.html;
}
[root@web02 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 /etc/nginx/conf.d]#systemctl restart nginx
3、配置host
测试
bash#配置负载均衡LB01 10.0.0.5
[root@lb01 /etc/nginx/conf.d]#cat lb.conf
upstream webs {
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name test.oldboy.com;
location / {
proxy_pass http://webs;
include proxy_params; #代理参数不能丢,请求头,长连接,携带ip地址
}
}
#重启生效
[root@lb01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx/conf.d]#systemctl restart nginx
#将hosts解析到LB01,修改host文件
#测试,访问结果 WEB01和WEB02以轮循的方式来回切换,每次只有1个请求

配置wordpress介入负载均衡
bash[root@lb01 /etc/nginx/conf.d]#vim lb.conf
[root@lb01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx/conf.d]#cat lb.conf
upstream wp {
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name wp.oldboy.com;
location / {
proxy_pass http://wp;
include proxy_params;
}
}
[root@lb01 /etc/nginx/conf.d]#systemctl restart nginx
#修改host解析
#查看是否平均分摊
#web01测试
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET / HTTP/1.1" 200 69887 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-includes/css/admin-bar.min.css?ver=6.7.2 HTTP/1.1" 200 20182 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-content/themes/twentytwentyfive/style.css?ver=1.0 HTTP/1.1" 200 2503 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-includes/js/dist/script-modules/interactivity/index.min.js?ver=907ea3b2f317a78b7b9b HTTP/1.1" 200 36946 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-includes/js/hoverintent-js.min.js?ver=2.2.1 HTTP/1.1" 200 1718 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-content/themes/twentytwentyfive/assets/fonts/manrope/Manrope-VariableFont_wght.woff2 HTTP/1.1" 200 53600 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-content/uploads/2025/04/0c0e7c61-807f-410a-b871-3041a3e67185-1024x625.png HTTP/1.1" 200 117564 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
#web02测试
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-includes/css/dashicons.min.css?ver=6.7.2 HTTP/1.1" 200 59016 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-includes/blocks/navigation/style.min.css?ver=6.7.2 HTTP/1.1" 200 16384 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-includes/js/dist/script-modules/block-library/navigation/view.min.js?ver=8ff192874fc8910a284c HTTP/1.1" 200 3300 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-content/uploads/2025/04/0dc09954-8e0a-4db7-b4ae-20a8ac1b2e72.png HTTP/1.1" 200 42508 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-includes/js/admin-bar.min.js?ver=6.7.2 HTTP/1.1" 200 3487 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-content/uploads/2025/04/7.jpg HTTP/1.1" 200 415176 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
172.16.1.5 - - [11/Apr/2025:16:42:21 +0800] "GET /wp-includes/js/wp-emoji-release.min.js?ver=6.7.2 HTTP/1.1" 200 18726 "http://wp.oldboy.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" "10.0.0.1"
1.测试一台服务器nginx挂掉
[root@web01 /etc/nginx/conf.d]#systemctl stop nginx
[root@web01 /etc/nginx/conf.d]#systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Drop-In: /usr/lib/systemd/system/nginx.service.d
#测试,无影响,浏览器正常访问,后端走的8服务器,如果后端Nginx挂掉,负载均衡会自动的让下个服务器处理用户的请求.

bash2.测试一台php挂掉
[root@web01 /etc/nginx/conf.d]#systemctl stop php-fpm.service
[root@web01 /etc/nginx/conf.d]#systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
#刚开始报错502,一刷新又恢复正常,因为一台服务器是好的,以轮询的方式请求。如果后端PHP或者数据库挂掉,不会自动请求下一个服务器而是返回报错信息。

bash当后端出现问题,使用next模块请求下个服务器
[root@lb01 /etc/nginx/conf.d]#cat lb.conf
upstream wp {
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name wp.oldboy.com;
location / {
proxy_pass http://wp;
include proxy_params;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504; #当出现500,502,503,504错误,自动访 问下一个服务器,比如7有问题,去访问8
}
}
[root@lb01 /etc/nginx/conf.d]#systemctl restart nginx
#测试正常,不会出现502
bash说一下负载均衡有哪几种调度算法,说一下rr循环和ip_hash的区别。
调度算法 概述
rr轮询 按时间顺序逐一分配到不同的后端服务器(默认)
weight 加权轮询,weight值越大,分配到的访问几率越高
ip_hash 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn 最少链接数,那个机器链接数少就分发
说一下rr循环和ip_hash的区别。
rr循环是默认的调度算法,也是常用的。平均分摊流量到后端服务器
ip_hash以IP地址hash到后端的固定服务,会造成负载不均衡,但是可以解决会话保持的问题。
#rr轮询为默认的负载均衡调度算法。
使用场景:
后端web服务器配置相同的情况下。
bash使用场景:
后端web服务器配置不同的情况下。配置高的多处理,配置低的少处理。
[root@lb01 /etc/nginx/conf.d]#cat lb.conf
upstream webs {
server 172.16.1.7 weight=5; #每次会多处理5个请求
server 172.16.1.8;
}
server {
listen 80;
server_name test.oldboy.com;
location / {
proxy_pass http://webs;
include proxy_params;
}
}
bash使用场景:
IP地址唯一,解决会话保持的场景。给ip配置固定服务器,比如116.63.0.11,访问web01,以后就只在web01访问,因为一个IP不一定是一个人
[root@lb01 /etc/nginx/conf.d]#cat lb.conf
upstream webs {
server 172.16.1.7;
server 172.16.1.8;
ip_hash; #配置
}
server {
listen 80;
server_name test.oldboy.com;
location / {
proxy_pass http://webs;
include proxy_params;
}
}
bash状态 概述
down 当前的server暂时不参与负载均衡 #类似与前面加注释
backup 预留的备份服务器 #预留备份,类似备胎,使用较少。当其他所有web服务器挂掉,backup服务器顶上。
#健康状态检查模块
max_fails 允许请求失败的次数
fail_timeout 经过max_fails失败后, 服务暂停时间
max_conns 限制最大的接收连接数
[root@lb01 /etc/nginx/conf.d]#cat lb.conf
upstream webs {
server 172.16.1.7;
server 172.16.1.8 down;
}
server {
listen 80;
server_name test.oldboy.com;
location / {
proxy_pass http://webs;
include proxy_params;
}
}
[root@lb01 /etc/nginx/conf.d]#cat lb.conf
upstream webs {
server 172.16.1.7;
server 172.16.1.8 backup;
}
server {
listen 80;
server_name test.oldboy.com;
location / {
proxy_pass http://webs;
include proxy_params;
}
}
bash#把从第三方下载的模块以打补丁的方式添加到配置文件里,然后编译,跟源码包融为一体
需求: 在当前的Nginx中增加一个新的模块
1.安装依赖包
[root@lb01 ~]#yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
2.下载nginx源码包以及nginx_upstream_check模块第三方模块
#首先先查看nginx版本
[root@lb01 ~]#nginx -v
nginx version: nginx/1.26.1
#下载nginx包,把源路径的1.14.2该成1.26.1
[root@lb01 ~]#wget http://nginx.org/download/nginx-1.26.1.tar.gz
下载第三方的模块
nginx默认的配置方式:
[root@lb01 /etc/nginx/conf.d]#nginx -V
nginx version: nginx/1.26.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017 (running with OpenSSL 1.0.2o-fips 27 Mar 2018)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --add-module=/root/nginx_upstream_check_module-master --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
#解压新的模块
[root@lb01 ~]#unzip nginx_upstream_check_module-master.zip
#解压nginx
[root@lb01 ~]#tar -xf nginx-1.26.1.tar.gz
3.将模块打补丁到默认的nginx配置中
#选择跟自己nginx版本差不多的
[root@lb01 ~/nginx-1.26.1]#patch -p1 <../nginx_upstream_check_module-master/
CHANGES check_1.9.2+.patch
check_1.11.1+.patch check.patch
check_1.11.5+.patch config
check_1.12.1+.patch doc/
check_1.14.0+.patch nginx-sticky-module.patch
check_1.16.1+.patch nginx-tests/
check_1.20.1+.patch ngx_http_upstream_check_module.c
check_1.2.1.patch ngx_http_upstream_check_module.h
check_1.2.2+.patch ngx_http_upstream_jvm_route_module.patch
check_1.2.6+.patch README
check_1.5.12+.patch test/
check_1.7.2+.patch upstream_fair.patch
check_1.7.5+.patch util/
[root@lb01 ~/nginx-1.26.1]#patch -p1 <../nginx_upstream_check_module-master/check_1.20.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h
4.重新配置nginx模块,增加了这个 --add-module=/root/nginx_upstream_check_module-master
[root@lb01 ~/nginx-1.26.1]#./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --add-module=/root/nginx_upstream_check_module-master --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
5.编译代码
[root@lb01 ~/nginx-1.26.1]#make
6.安装部署
[root@lb01 ~/nginx-1.26.1]#make install
检查Nginx编译是否成功。看是否有--add-module=/root/nginx_upstream_check_module-master
[root@lb01 ~/nginx-1.26.1]#nginx -V
nginx version: nginx/1.26.1
built by gcc 7.3.0 (GCC)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --add-module=/root/nginx_upstream_check_module-master --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
使用新增加的模块功能
[root@lb01 /etc/nginx/conf.d]#vim lb.conf
[root@lb01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx/conf.d]#cat lb.conf
upstream wp {
server 172.16.1.7 max_fails=2 fail_timeout=10s;
server 172.16.1.8 max_fails=2 fail_timeout=10s;
check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
}
#interval 检测间隔时间,单位为毫秒
#rise 表示请求2次正常,标记此后端的状态为up
#表示请求3次失败,标记此后端的状态为down
#type 类型为tcp
#timeout 超时时间,单位为毫秒
server {
listen 80;
server_name wp.oldboy.com;
location / {
proxy_pass http://wp;
include proxy_params;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
}
location /upstream_check { # 用户访问upstream_check 则返回check_status页面
check_status; # 新增加模块后才可以用此模块。,如果模块里面没有nginx -t就找不到这个模块
}
}
[root@lb01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx/conf.d]#systemctl restart nginx
访问测试
wp.oldboy.com/upstream_check
作业
bash周末: SSH免秘钥 SSH远程连接 SCP远程拷贝 NFS nfs服务 lsync推送/data目录 /data/wp /data/zh WEB01 nginx+php nfs-utils(挂载) wordpress zh WEB02 nginx+php nfs-utils(挂载) wordpress zh DB01 mariadb(独立部署) LB01 nginx 负载均衡 wp zh BACKUP rsync服务 会话保持
bashNginx会话保持是一种在负载均衡场景中,确保同一客户端的多次请求被分发到同一台后端服务器的机制。这种机制主要用于维护用户会话状态
(如登录信息、购物车内容等),避免因请求被分配到不同服务器而导致会话数据丢失。
#1.基于IP哈希(ip_hash)
原理:根据客户端的IP地址计算哈希值,将同一IP的请求固定分发到某台后端服务器。
特点:简单易用,无需客户端支持。
#2. 基于Cookie(sticky模块)缺点:如果客户端使用动态IP或处于同一局域网(NAT)下(如公司网络),多个用户可能被哈希到同一服务器,导致负载不均。
原理:通过插入或识别Cookie,将同一会话的请求绑定到特定后端服务器
特点:更精准,适合动态IP或移动端用户。
缺点:需客户端支持Cookie,且需额外安装模块(非Nginx原生功能)。
解决会话保持:保持多态业务服务器访问同一个共享存储设备服务器就行
1、lsync
2、nfs,但是这个也是与磁盘交互,会有性能方面的影响
3、redis
#测试知乎两个浏览器同一账户密码,不同的cookie,重新登录会产生不同的cookie值

bash#通过可视化管理数据库业务
1.nginx配置
[root@web01 /etc/nginx/conf.d]#cat admin.conf
server {
listen 80;
server_name admin.oldboy.com;
root /code/admin;
location / {
index index.php index.html;
}
location ~\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
2.创建代码目录 并且下载代码
[root@web01 /etc/nginx/conf.d]#mkdir /code/admin
[root@web01 /code/admin]#ll
total 14744
-rw-r--r-- 1 root root 15096155 Apr 14 17:04 phpMyAdmin-5.2.1-all-languages.zip
[root@web01 /code/admin]#unzip phpMyAdmin-5.2.1-all-languages.zip
[root@web01 /code/admin]#mv phpMyAdmin-5.2.1-all-languages/* .
3.修改代码连接数据库的IP地址,默认是localhost
#先备份文件,复制示例文件为正式的配置文件
[root@web01 /code/admin]#cp config.sample.inc.php config.inc.php
#修改hosts为数据库地址172.16.1.51
[root@web01 /code/admin]#grep 172.16.1.51 config.inc.php
$cfg['Servers'][$i]['host'] = '172.16.1.51';
4.修改本地host文件
#浏览器测试,报错,因为权限不够,需要用启动php的用户的身份有写入权限,也就是nginx
bash5.授权本地存放session会话目录的权限为nginx
[root@web01 /code/admin]#chown nginx.nginx /var/lib/php/session/
bashphpmyadmin不管怎么刷新一直是登录状态, 因为后端把会话状态给保存下来了
[root@web01 /code/admin]#ll /var/lib/php/session
total 4
-rw------- 1 nginx nginx 3452 Apr 14 19:48 sess_umavr9vaap79chrtmblunpdq8r
#删除会话文件,跳出了当前登录界面了
[root@web01 /var/lib/php/session]#mv sess_umavr9vaap79chrtmblunpdq8r /tmp
[root@web01 /var/lib/php/session]#ll
total 0
[root@web01
bash#下面报错信息, 变量 $cfg['TempDir'] (/code/admin/tmp/)无法访问。phpMyAdmin无法缓存模板文件,所以会运行缓慢,需要给权限
[root@web01 /code/admin]#chown -R nginx.nginx /code/admin/
解决
bash因为session值默认放在本地,如果在web01跟web02分别部署phpmyadmin,前面加上负载均衡的平均分摊请求(比如20个请求,web01有10个,web02也有10个),那么session如果放在web01那web02就找不到session值,在网页登录的一瞬间,session就会变
bash#拷贝nginx的配置文件
[root@web01 /var/lib/php/session]#scp /etc/nginx/conf.d/admin.conf 10.0.0.8:/etc/nginx/conf.d/
Authorized users only. All activities may be monitored and reported.
root@10.0.0.8s password:
admin.conf
#拷贝phpmyadmin的代码到web02
[root@web01 /var/lib/php/session]#scp -r /code/admin 10.0.0.8:/code/
[root@web02 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 /etc/nginx/conf.d]##systemctl restart nginx
#session目录属主属组
[root@web02 /etc/nginx/conf.d]#chown nginx.nginx /var/lib/php/session/
[root@web02 /etc/nginx/conf.d]#ll -d /var/lib/php/session/
drwxrwx--- 2 nginx nginx 6 Mar 31 16:54 /var/lib/php/session/
#hosts解析到10.0.0.8 测试访问
登陆测试
bash#配置LB
[root@lb01 /etc/nginx/conf.d]#cat admin.conf
upstream ad{
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name admin.oldboy.com;
location / {
proxy_pass http://ad;
include proxy_params;
}
}
[root@lb01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx/conf.d]#systemctl restart nginx
#浏览器测试


bash#要解决以上问题,可以挂载nfs进行实时同步,也可以lsync进行监控同步,最好的方式写到redis
1.安装服务
[root@db01 ~]#yum -y install redis
[root@db01 ~]#redis-cli
#设置redis密码两种方式,开始是没有配置认证信息的
#第一种
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
#比如设置认证为123,如果返回OK正面修改成功
127.0.0.1:6379> config set requirepass 123
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> exit
[root@db01 ~]#redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
#此时就得需要输入认证操作redis了
127.0.0.1:6379> auth 123
OK
127.0.0.1:6379> keys *
#第二种
[root@db01 ~]#sed -n '1044p' /etc/redis.conf
requirepass 123
#远程访问,前提是装了redis情况下
[root@web01 ~]#redis-cli -h 172.16.1.51 -a '123'
2.配置服务,修改监听地址,因为默认redis只能本机访问。
[root@db01 ~]#grep 172.16.1.51 /etc/redis.conf -n
87:bind 127.0.0.1 172.16.1.51
3.启动服务
[root@db01 ~]#systemctl start redis
[root@db01 ~]#netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1078/sshd: /usr/sbi
tcp 0 0 172.16.1.51:6379 0.0.0.0:* LISTEN 61296/redis-server
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 61296/redis-server
tcp6 0 0 :::22 :::* LISTEN 1078/sshd: /usr/sbi
tcp6 0 0 :::3306 :::* LISTEN 56231/mysqld
udp 0 0 127.0.0.1:323 0.0.0.0:* 744/chronyd
udp6 0 0 ::1:323 :::* 744/chronyd
#修改PHP会话保持的信息
默认php会将会话存储到本地磁盘/var/lib/php/session/目录修改php将会话写入到搭建好的redis服务器中。
1.修改php.ini
[root@web01 /var/lib/php/session]#vim /etc/php.ini
1222:session.save_handler = redis #由file改为redis
1255:session.save_path = "tcp://172.16.1.51:6379?auth=123" #由tmp改成tcp://172.16.1.51:6379,123是密码
2.修改/etc/php-fpm.d/www.conf配置文件,注释倒数第3-4行
[root@web01 /var/lib/php/session]#tail -4 /etc/php-fpm.d/www.conf
;php_value[session.save_handler] = files #注释
;php_value[session.save_path] = /var/lib/php/session #注释
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
;php_value[opcache.file_cache] = /var/lib/php/opcache
3.安装php连接redis的模块(编译)
PHP安装redis插件
第一步: 上传redis源码包
[root@web01 ~]#ll
drwxr-xr-x 6 root root 4096 Mar 25 02:30 phpredis-6.2.0
-rw-r--r-- 1 root root 435613 Apr 14 17:50 phpredis-6.2.0.zip
第二步: 解压代码
#[root@web01 ~]#unzip phpredis-6.2.0
第三步: 配置
[root@web01 ~]#cd phpredis-6.2.0/
[root@web01 ~/phpredis-6.2.0]#phpize
Configuring for:
PHP Api Version: 20200930
Zend Module Api No: 20200930
Zend Extension Api No: 420200930
[root@web01 ~/phpredis-6.2.0]#./configure
第四步: 编译安装
[root@web01 ~/phpredis-6.2.0]#make
[root@web01 ~/phpredis-6.2.0]#make install
Installing shared extensions: /usr/lib64/php/modules/
#测试是否编译成功
[root@web01 ~/phpredis-6.2.0]#php-fpm -t
[14-Apr-2025 21:29:38] NOTICE: configuration file /etc/php-fpm.conf test is successful
第五步: 开启redis插件功能,配置文件增加以下一行内容
[root@web01 ~/redis-4.0.1]#grep redis.so /etc/php.ini -n
1356:extension=redis.so
第六步: 重启服务
[root@web01 ~/redis-4.0.1]#systemctl restart php-fpm.service
#将WEB01的配置文件同步到WEB02
[root@web01 ~/phpredis-6.2.0]#scp /etc/php.ini 10.0.0.8:/etc/
[root@web01 ~/phpredis-6.2.0]#scp /etc/php-fpm.d/www.conf 10.0.0.8:/etc/php-fpm.d/
1.上传代码文件
[root@web02 ~]#ll
-rw-r--r-- 1 root root 435613 Apr 14 17:50 phpredis-6.2.0.zip
2.解压
[root@web02 ~]#unzip phpredis-6.2.0.zip
3.进入目录编译
[root@web01 ~]#cd phpredis-6.2.0/
[root@web02 ~/phpredis-6.2.0]#phpize
[root@web02 ~/phpredis-6.2.0]#./configure
[root@web02 ~/phpredis-6.2.0]#make
[root@web02 ~/phpredis-6.2.0]#make install
#测试是否编译成功
[root@web02 ~/phpredis-6.2.0]#php-fpm -t
[14-Apr-2025 21:47:18] NOTICE: configuration file /etc/php-fpm.conf test is successful
[root@web02 ~/phpredis-6.2.0]#grep redis.so /etc/php.ini -n
1356:extension=redis.so
[root@web02 ~/phpredis-6.2.0]#systemctl restart php-fpm
hosts文件解析到lb01
#测试

bash四层负载均衡是基于传输层协议包来封装的(如:TCP,ip),那我们前面所指的七层负载是指的应用层,他的组装在四层的基础之上,无论是四层还是 七层都是指的是OSI网络模型 作用: 解决高并发,在四层传输层进行转发。中小型互联网公司使用较少。做代理使用的较多比如数据库集群。 面试题: LVS的几种转发模式 1.DR模式 2.NAT模式 3.TUN隧道模式 4.FULLNAT模式
bash用户10.0.0.1的随机端口去请求10.0.0.4的80端口,那么四层负载就会做目标地址的修改,也就是只要有用户去请求10.0.0.4的80端口, 那么目标地址就被修改成了负载均衡10.0.0.5的80端口或者改成了负载均衡10.0.0.6的80端口,直接让10.0.0.1与10.0.0.5或者6建立连接, 之后再有负载均衡平均分摊到web服务器,但是有个问题是用户是跟四层负载的mac地址已经建立联系,如果直接与负载均衡服务器建立连接那么mac地址 会不一样导致连接失败的问题,所以出现了LVS,支持四层转发,高并发场景使用 Nginx支持的四层负载假四层,它是以NAT的方式转发,也就是代理的性质。跟上面不一样,上面相当于客户直接与房东建立联系,而nginx是客户 找中介,中介找房东
bash四层负载均衡总结: 1、四层负载均衡仅能转发TCP/IP协议、UDP协议、通常用来转发端口,如tcp/22、udp/53; 2、四层负载均衡可以用来解决七层负载均衡端口限制问题;(七层负载均衡最大使用65535个端口号); 3、四层负载均衡可以解决七层负载均衡高可用问题;(多台后端七层均衡能同时使用); 4、四层的转发效率比七层的高的多,但仅支持tcp/ip协议,不支持http和https协议; 5、通常大并发场景会选择使用七层负载前面增加四层负载均衡;
bash#通过访问负载均衡的5555端口,实际是后端的web01的22端口在提供服务;
使用场景:后端没有公网ip地址,在机房里面,跟前边学的跳板机一样,既有内网又有公网,先连接公网,通过公网再连接内网
1、克隆新的虚拟机
2、安装nginx
[root@oldboy:~]#scp 10.0.0.5:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/
[root@oldboy:~]#yum -y install nginx
3.配置四层
先删除默认的七层配置文件,要不四层起不来
[root@lb /etc/nginx/conf.d]#rm -rf *
[root@lb /etc/nginx]#cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
#配置四层,写在http区块上面,要不访问不到
stream {
upstream web01 {
server 172.16.1.7:22; #3.web01对应的172.16.1.7:22端口
}
server {
listen 5555; #1.有用户连接我的5555端口
proxy_pass web01; #2.那么就让她请求web01
}
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
[root@lb /etc/nginx]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb /etc/nginx]#systemctl start nginx
#客户端远程测试,如果连接10.0.0.4:5555那么应该访问172.16.1.7:22
bash#通过访问负载均衡的6666端口,实际是后端的mysql的3306端口在提供服务。
stream {
upstream web01 {
server 172.16.1.7:22;
}
server {
listen 5555;
proxy_pass web01;
}
upstream db01 {
server 172.16.1.51:3306; #3.db01对应的172.16.1.51:3306端口
}
server {
listen 6666; #1.有用户连接我的6666端口
proxy_pass db01; #2.那么就让她请求db01
}
}
[root@lb /etc/nginx]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb /etc/nginx]#systemctl start nginx
#测试访问
bash#负载wp.oldboy.com zh.oldboy.com admin.oldboy.com
...
stream {
upstream webs {
server 172.16.1.5:80; #3.直接将ip地址映射成5或者6
server 172.16.1.6:80;
}
server {
listen 80; #1.有用户连接我的80端口
proxy_pass webs; #2.那么就让她请求webs
}
}
...
[root@lb /etc/nginx]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb /etc/nginx]#systemctl restart nginx
#修改host文件
10.0.0.4 admin.oldboy.com wp.oldboy.com zh.oldboy.com
#测试myphpadmin没问题,是通过4服务器的四层负载转发给5或者6的负载均衡,然后再访问web01或者web02
bash#通过测试,都可以正常访问,拿wp.oldboy.com域名解释,通过host文件解析成10.0.0.4服务器,然后10.0.0.4分发给10.0.0.5和10.0.0.6两台负载均衡服务器,不是平均分发,通过以下日志可以看到两台负载均衡服务器均有日志产生,说明被转发到了这两台服务器,并且两台服务器又把请求分摊到了web01跟web02服务器

| 对比维度 | 四层负载均衡(L4) | 七层负载均衡(L7) |
|---|---|---|
| 工作层级 | OSI 传输层(第4层) | OSI 应用层(第7层) |
| 协议支持 | TCP、UDP | HTTP、HTTPS、FTP、TELNET、DNS 等应用层协议 |
| 转发依据 | IP地址 + 端口号,基于算法(轮询、最小连接数等) | 应用层内容(URL路径、HTTP头、Cookie、请求内容等) |
| 性能 | 高吞吐量,低延迟(仅处理网络层数据) | 相对较低(需解析应用层协议,计算开销大) |
| 功能特性 | - 简单连接分发 - 支持端口映射 - 无内容感知 | - URL路由 - SSL终止 - 内容缓存 - 会话保持(Sticky Session) - 应用层防护 |
| 健康检查 | 检查端口是否存活 | 检查应用状态(如HTTP 200响应、数据库查询成功) |
| 安全性 | 无法防御应用层攻击 | 支持应用层防护(如防DDoS、WAF、SQL注入检测) |
| 典型应用场景 | - 数据库集群 - 实时通信(如游戏、VoIP) - 高并发TCP/UDP服务 | - Web服务器负载均衡 - 微服务路由(按API路径分发) - 需要内容优化的场景 |
| 代表工具/产品 | LVS(Linux Virtual Server)、F5 BIG-IP(硬件)、AWS NLB | Nginx、HAProxy、Apache Traffic Server、AWS ALB、Cloudflare |
| 示例场景 | 客户端请求 10.0.0.1:80,均衡器直接转发至后端服务器的IP和端口,不解析HTTP内容。 | 客户端请求 /api 路径时,均衡器解析HTTP头,将请求路由至API服务器;/static 转至CDN。 |
bash#通过表格可直观看出,L4适合高性能、低延迟的基础流量分发,而L7适合复杂业务逻辑和精细化控制的场景。实际应用中常结合两者(如L4做全局流量入口,L7做内部服务路由)。
| 维度 | 静态资源 | 动态资源 |
|---|---|---|
| 本质 | 预先存在的物理文件 | 实时生成的内容 |
| 生成时机 | 部署前(开发者手动或构建工具生成) | 用户请求时(服务器运行时处理) |
| 典型示例 | .html, .css, .js, 图片, 视频, 字体文件 | PHP渲染的页面、API接口、数据库查询结果 |
| 服务器行为 | 直接读取文件并返回 | 执行代码(如Java/Python/PHP)、查询数据库等 |
bash#静态资源与动态资源的区别
静态资源
静态资源是指非Web服务器运行处理而生成的文件,指预先存在于服务器上的文件,如:.html, .css, .js, 图片(.jpg, .png)、字体文件(.woff)、视频(.mp4)等。
关键特征:文件内容在服务器上已生成并存储,无需服务器运行时处理,可直接返回给客户端。
动态资源
指由服务器实时生成的内容,如:
PHP/Node.js/Python 动态渲染的页面
数据库查询结果(如用户订单列表)
实时计算的API响应(如天气预报)
关键特征:内容在请求时由服务器程序动态生成。
静态资源是“直接传输的文件”,动态资源是“实时生成的结果”。
1. 静态资源请求流程
用户请求 → Nginx → 直接读取磁盘文件 → 返回浏览器
2. 动态资源请求流程
用户请求 → Nginx → 反向代理到Tomcat/Node.js → 执行业务逻辑 → 查询数据库 → 生成HTML/JSON → 返回浏览器
bashTomcat主要运行java的代码的WEB服务
Nginx主要运行HTML的WEB服务 静态数据 结合其他应用才可以运行动态数据。
WEB02部署:
1.下载Tomcat 类似windows的绿色软件
[root@web02 ~]#wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.40/bin/apache-tomcat-10.1.40.tar.gz
2.安装运行环境
[root@web02 ~]#yum -y install java
3.启动Tomcat
#解压
[root@web02 ~]#tar xf apache-tomcat-10.1.40.tar.gz -C /usr/local/
#运行Tomcat
[root@web02 ~]#/usr/local/apache-tomcat-10.1.40/bin/startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat-10.1.40
Using CATALINA_HOME: /usr/local/apache-tomcat-10.1.40
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-10.1.40/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-10.1.40/bin/bootstrap.jar:/usr/local/apache-tomcat-10.1.40/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
#检查端口8080
[root@web02 ~]#netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 7975/php-fpm: maste
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2482/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1068/sshd: /usr/sbi
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 12495/java
tcp6 0 0 :::8080 :::* LISTEN 12495/java
浏览器访问: 10.0.0.8:8080
bash#单台实现静态资源拆分
问题1.用户访问8080才可以访问到Tomcat?
问题2.nginx代理配置实现静态资源拆分
[root@web02 /etc/nginx/conf.d]#cat tom.conf
server {
listen 80;
server_name tomcat.oldboy.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
[root@web02 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 /etc/nginx/conf.d]#systemctl restart nginx
#测试访问

bash#把小老虎图片给拆分出去,http://tomcat.oldboy.com/tomcat.svg
[root@web02 /etc/nginx/conf.d]#cat tom.conf
server {
listen 80;
server_name tomcat.oldboy.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
location ~\.(png|svg|jpg|jpeg|mp4)$ { #根据匹配原则,会从/code/image路径找png|svg|jpg|jpeg|mp4图,如果没找到
root /code/image; #单独找图片就返回404,
}
}
[root@web02 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 /etc/nginx/conf.d]#systemctl restart nginx
#测试看图
bash将tomcat下的图片复制一份到/code/image
[root@web02 /etc/nginx/conf.d]#mkdir /code/image
[root@web02 /etc/nginx/conf.d]#cp /usr/local/apache-tomcat-10.1.40/webapps/ROOT/* /code/image/
#经过替换后还是不能看,看错误日志显示/code/image/tomcat.svg" failed (13: Permission denied),没有权限,发现属主属组都是root,需要改成nginx的启动用户,也就是nginx才能看,修改完权限可以看到了
[root@web02 /code/image]#ll
total 164
-rw-r----- 1 root root 27235 Apr 15 15:33 asf-logo-wide.svg
-rw-r----- 1 root root 713 Apr 15 15:33 bg-button.png
-rw-r----- 1 root root 1918 Apr 15 15:33 bg-middle.png
-rw-r----- 1 root root 1401 Apr 15 15:33 bg-nav.png
[root@web02 /code/image]#chown -R nginx.nginx /code/image/
bash#多台实现动态分离
系统 作用 服务 地址
kylin 负载均衡 nginx proxy 10.0.0.5
kylin 静态资源 nginx static 10.0.0.7 静态页面
kylin 动态资源 tomcat server 10.0.0.8 Tomcat
bash[root@web01 /etc/nginx/conf.d]#cat default.conf
server {
listen 80;
server_name s.oldboy.com;
location / {
root /code/s;
index index.html index.htm;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
[root@web01 /etc/nginx/conf.d]#mkdir /code/s
#host解析
#浏览器测试,因为存在目录,但是目录里面没有东西,所以403错误
bash#带路径访问,成功
bashWEB02部署Tomcat
1.下载tomcat
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.40/bin/apache-tomcat-10.1.40.tar.gz
2.解压
tar xf apache-tomcat-10.1.40.tar.gz -C /usr/local/
3.安装java
yum -y install java
4.准备动态资源代码
[root@web02 /code/image]#cd /usr/local/apache-tomcat-10.1.40/webapps/ROOT/
#删除目录下的文件
[root@web02 /usr/local/apache-tomcat-10.1.40/webapps/ROOT]#rm -rf *
#把随机数代码放进去,默认读index.jsp
[root@web02 /usr/local/apache-tomcat-10.1.40/webapps/ROOT]#vim index.jsp
[root@web02 /usr/local/apache-tomcat-10.1.40/webapps/ROOT]#cat index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>oldboy JSP Page</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>oldboy随机数:<h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>
5.启动tomcat
#先停止tomcat
[root@web02 /usr/local/apache-tomcat-10.1.40/webapps/ROOT]#/usr/local/apache-tomcat-10.1.40/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/apache-tomcat-10.1.40
Using CATALINA_HOME: /usr/local/apache-tomcat-10.1.40
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-10.1.40/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-10.1.40/bin/bootstrap.jar:/usr/local/apache-tomcat-10.1.40/bin/tomcat-juli.jar
Using CATALINA_OPTS:
#再启动tomcat
[root@web02 /usr/local/apache-tomcat-10.1.40/webapps/ROOT]#/usr/local/apache-tomcat-10.1.40/bin/startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat-10.1.40
Using CATALINA_HOME: /usr/local/apache-tomcat-10.1.40
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-10.1.40/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-10.1.40/bin/bootstrap.jar:/usr/local/apache-tomcat-10.1.40/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
#出现随机数页面,刷新一次变一次
bashLB01 10.0.0.5配置
[root@lb01 /etc/nginx/conf.d]#cat s.conf
upstream static {
server 172.16.1.7:80;
}
upstream java {
server 172.16.1.8:8080;
}
server {
listen 80;
server_name s.oldboy.com;
location ~* \.(jpg|png|gif)$ {
proxy_pass http://static;
proxy_set_header Host $http_host;
}
location ~ \.jsp {
proxy_pass http://java;
proxy_set_header Host $http_host;
}
}
[root@lb01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx/conf.d]#systemctl restart nginx
浏览器直接访问s.oldboy.com 会显示nginx默认站点目录
Nginx默认的代码目录:/usr/share/nginx/html/index.html
bash通过负载均衡访问静态页面
bash通过负载均衡访问动态页面
bash整合静态和动态资源为一个页面:
[root@lb01 /etc/nginx/conf.d]#cat s.conf
upstream static {
server 172.16.1.7:80;
}
upstream java {
server 172.16.1.8:8080;
}
server {
listen 80;
server_name s.oldboy.com;
root /code; #指定静态代码目录
index index.html; #默认首页
location ~* \.(jpg|png|gif)$ {
proxy_pass http://static;
proxy_set_header Host $http_host;
}
location ~ \.jsp {
proxy_pass http://java;
proxy_set_header Host $http_host;
}
}
[root@lb01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx/conf.d]#systemctl restart nginx
#编辑首页配置:
[root@lb01 /etc/nginx/conf.d]#cat /code/index.html
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试ajax和跨域访问</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://s.oldboy.com/index.jsp",
success: function(data){
$("#get_data").html(data)
},
error: function() {
alert("哎呦喂,失败了,回去检查你服务去~");
}
});
});
</script>
<body>
<h1>测试动静分离</h1>
<img src="http://s.oldboy.com/1.jpg">
<div id="get_data"></div>
</body>
</html>
#访问首页测试
bash停止静态不影响静态资源
web01 sytemctl stop nginx
#访问页面测试s.oldboy.com
bash停止动态不影响静态资源
/usr/local/apache-tomcat-10.1.40/bin/shutdown.sh
#访问页面测试s.oldboy.com
bashNginx通过负载均衡实现手机与PC调度至不通的后端节点应用案例
系统版本 主机角色 外网IP 内网IP 提供端口
Kylin 负载均衡 10.0.0.5 172.16.1.5 80
Kylin 提供Android页面 172.16.1.7 9090
Kylin 提供Iphone页面 172.16.1.7 9091
Kylin 提供pc页面 172.16.1.7 9092
1.配置Nginx 通过多端口实现不同的业务
[root@web01 /etc/nginx/conf.d]#cat m.conf
charset utf-8,gbg;
server {
listen 9090;
server_name _;
location / {
root /code/9090;
index index.html;
}
}
server {
listen 9091;
server_name _;
location / {
root /code/9091;
index index.html;
}
}
server {
listen 9092;
server_name _;
location / {
root /code/9092;
index index.html;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
#创建静态页面
[root@web01 /etc/nginx/conf.d]#mkdir /code/{9090,9091,9092}
[root@web01 /etc/nginx/conf.d]#ll -d /code/{9090,9091,9092}
drwxr-xr-x 2 root root 6 Apr 16 14:42 /code/9090
drwxr-xr-x 2 root root 6 Apr 16 14:42 /code/9091
drwxr-xr-x 2 root root 6 Apr 16 14:42 /code/9092
[root@web01 /etc/nginx/conf.d]#echo "您访问的是Android">/code/9090/index.html
[root@web01 /etc/nginx/conf.d]#echo "您访问的是Iphone">/code/9091/index.html
[root@web01 /etc/nginx/conf.d]#echo "您访问的是pc">/code/9092/index.html
直接访问测试:
10.0.0.7:9090
10.0.0.7:9091
10.0.0.7:9092
bash#负载均衡服务器部署
负载均衡通过客户端的UA来判断让用户转发到哪个后端业务。
[root@lb01 /etc/nginx/conf.d]#cat m.conf
upstream android {
server 172.16.1.7:9090;
}
upstream iphone {
server 172.16.1.7:9091;
}
upstream pc {
server 172.16.1.7:9092;
}
server {
listen 80;
server_name m.oldboy.com;
charset 'utf-8';
location / {
#如果客户端来源是Android则跳转到Android的资源;
if ($http_user_agent ~* "Android") {
proxy_pass http://android;
}
#如果客户端来源是Iphone则跳转到Iphone的资源;
if ($http_user_agent ~* "Iphone") {
proxy_pass http://iphone;
}
#如果客户端是IE浏览器则返回403错误;
if ($http_user_agent ~* "MSIE") {
return 403;
}
#默认跳转pc资源;
proxy_pass http://pc;
}
}
[root@lb01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx/conf.d]#systemctl restart nginx
#host解析
#测试电脑访问
bash#卡换三星手机访问通过日志可以看到Android

bash#换iphone访问,通过日志可以看到iphone

bash#测试其他设备
bash访问1.html 跳转到2.html
[root@web01 /etc/nginx/conf.d]#cat re.conf
server {
listen 80;
server_name re.oldboy.com;
location / {
root /code/re;
rewrite /1.html /2.html;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
[root@web01 /etc/nginx/conf.d]#mkdir /code/re
[root@web01 /etc/nginx/conf.d]#echo "欢迎访问1.html">/code/re/1.html
[root@web01 /etc/nginx/conf.d]#echo "欢迎访问2.html">/code/re/2.html
#修改host,测试
bash访问1.html最终访问的是3.html
[root@web01 /etc/nginx/conf.d]#cat re.conf
server {
listen 80;
server_name re.oldboy.com;
root /code/re;
location / {
rewrite /1.html /2.html;
}
location /2.html {
rewrite /2.html /3.html;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
bash##########完整的跳转配置
[root@web01 /etc/nginx/conf.d]#cat re.conf
server {
listen 80;
server_name re.oldboy.com;
root /code/re/;
location / {
rewrite /1.html /2.html; #从1.html跳转到2.html
rewrite /2.html /3.html; #从2.html跳转到3.html
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html { #匹配到/3.html
rewrite /3.html /b.html; #从3.html跳转到b.html
}
}
[root@web01 /code/re]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /code/re]#systemctl restart nginx
#测试
bash#使用break控制跳转规则
[root@web01 /code/re]#cat /etc/nginx/conf.d/re.conf
server {
listen 80;
server_name re.oldboy.com;
root /code/re;
location / {
rewrite /1.html /2.html break; #从1.html跳转到2.html卡住,访问1.html 则显示2.html的内容。
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
bash#使用last控制跳转规则,访问1.html则匹配2.html结束,然后重新对web发起请求 re.oldboy.com/2.html
[root@web01 /etc/nginx/conf.d]#cat re.conf
server {
listen 80;
server_name re.oldboy.com;
root /code/re/;
location / {
rewrite /1.html /2.html last; #1.html跳转到2.html卡住,并且重新发起请求/2.html
rewrite /2.html /3.html;
}
location /2.html { #匹配到/2.html
rewrite /2.html /a.html; #从2.html跳转到a.html
}
location /3.html {
rewrite /3.html /b.html;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
bash#临时重定向302-临时跳转
[root@web01 /etc/nginx/conf.d]#cat r.conf
server {
listen 80;
server_name r.oldboy.com;
root /code;
location /test{ #1.如果有用户访问r.oldboy.com/test
rewrite ^(.*)$ http://www.baidu.com redirect; #2.那么就让他去访问百度,这是临时跳转,每次都要访问源站
#rewrite ^(.*)$ http://www.oldboy.com permanent;
#return 301 http://www.oldboy.com;
#return 302 http://www.oldboy.com;
}
}
访问测试:
10.0.0.7 r.oldboy.com
bash#这种临时跳转(302)如果把nginx停止,那么就访问不了
#如果永久跳转(301)如果把nginx停止,那么还有缓存可以支持正常访问跳转后的域名
bash#永久重定向302-永久跳转
[root@web01 /etc/nginx/conf.d]#cat r.conf
server {
listen 80;
server_name r.oldboy.com;
root /code;
location /test{
#rewrite ^(.*)$ http://www.baidu.com redirect;
rewrite ^(.*)$ http://www.baidu.com permanent;
#return 301 http://www.oldboy.com;
#return 302 http://www.oldboy.com;
}
}
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
访问测试:
先访问一次r.oldboy.com/test
然后停止nginx在访问测试,在浏览器缓存了r.oldboy.com/test对应的新的网址,清除缓存后连接失败
| 类型 | 状态码 | 核心区别 |
|---|---|---|
| 临时重定向 | 302 | 告诉客户端(如浏览器)本次请求的资源暂时需要跳转,后续请求仍应访问原URL。 |
| 永久重定向 | 301 | 告诉客户端原URL已永久失效,所有后续请求应直接访问新URL。 |
bash#匹配abc开头跳转到2.html
[root@web01 /etc/nginx/conf.d]#cat re.conf
server {
listen 80;
server_name re.oldboy.com;
root /code/re/;
location /abc { #如果用户访问re.oldboy.com/abc,那么就让他访问http://re.oldboy.com/ccc/bbb/2.html
rewrite (.*) /ccc/bbb/2.html redirect; #(.*)是表示所有,abc开头后面是什么无所谓,比如abcjjjj
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
[root@web01 /etc/nginx/conf.d]#mkdir -p /code/re/ccc/bbb
[root@web01 /etc/nginx/conf.d]#echo 2.html..... > /code/re/ccc/bbb/2.html
bash#匹配abc结尾的跳转到2.html
[root@web01 /etc/nginx/conf.d]#cat re.conf
server {
listen 80;
server_name re.oldboy.com;
root /code/re/;
location ~ abc$ {
rewrite (.*) /ccc/bbb/2.html redirect;
#return 302 /ccc/bbb/2.html;
}
}
[root@web01 /etc/nginx/conf.d]#cat re.conf
server {
listen 80;
server_name re.oldboy.com;
root /code/re/;
location ~* abc$ { #加*不区分大小写
rewrite (.*) /ccc/bbb/2.html redirect;
#return 302 /ccc/bbb/2.html;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
bash#案例2.用户访问/2025/data/1.html实际上真实访问的是/2014/data/1.html
[root@web01 /etc/nginx/conf.d]#cat re.conf
server {
listen 80;
server_name re.oldboy.com;
root /code/re/;
location /2025 {
rewrite ^/2025/(.*)$ /2014/$1 redirect;
}
}
# 当用户访问re.oldboy.com/2025,则让用户访问re.oldboy.com/2014/$1,这个$1相当于后向引用,意思是当访问的2025目录后面有内容的
# 时候会引用到(.*)里面,也会被$1引用
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
[root@web01 /etc/nginx/conf.d]#mkdir /code/re/2014/data -p
[root@web01 /etc/nginx/conf.d]#echo 11111... > /code/re/2014/data/1.html
bash#案例3.访问re.oldboy.com/course-11-22-33 实际访问的/code/re/course/11/22/33/course_33.html
server {
listen 80;
server_name re.oldboy.com;
root /code/re/;
location / {
#灵活配法
rewrite ^/course-(.*)-(.*)-(.*)$ /course/$1/$2/$3/course_$3.html redirect;
#固定配法
#rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart
Too few arguments.
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
[root@web01 /etc/nginx/conf.d]#
[root@web01 /etc/nginx/conf.d]#mkdir /code/re/course/11/22/33 -p
[root@web01 /etc/nginx/conf.d]#echo test... > /code/re/course/11/22/33/course_33.html
#浏览器访问http://re.oldboy.com/course/11/22/33/
bash#案例4.错误页面优雅显示
遇到错误页引流到自己的网站首页。
[root@web01 /etc/nginx/conf.d]#cat re.conf
server {
listen 80;
server_name re.oldboy.com;
root /code/re/;
error_page 403 404 500 501 502 @error_test; # 如果出现403 404 ..保存到@error_test
location @error_test { # 通过location再匹配到@error_test
rewrite ^(.*)$ http://zh.oldboy.com break; # 跳转到oldboyedu.com,break不再往下走
}
}
#访问http://re.oldboy.com/asdasd,跳转到下面
bash#测试错误页跳转到图片,把3.png放到/code/re目录
server {
listen 80;
server_name re.oldboy.com;
root /code/re/;
error_page 403 404 500 501 502 @error_test;
location @error_test {
rewrite ^(.*)$ /3.png break;
}
}
测试
bash#公司实际案例5.访问re.oldboy.com ,自己公司的IP访问跳转到re.oldboy.com/&showoffline=1
[root@web01 /etc/nginx/conf.d]#cat re.conf
server {
listen 80;
server_name re.oldboy.com;
# $args为Nginx内置变量请求行的参数
set $args "&showoffline=1"; #设置变量$args为&showoffline=1,把这一堆赋值给args
location / {
root /code;
index index.html;
}
if ($remote_addr = 10.0.0.1 ){ #如果客户端ip是公司自己的ip才会rewrite跳转
rewrite (.*) http://re.oldboy.com$1; # (.*)会默认在请求行的时候加上前面的内置变量args,$1可以引用
}
} # rewrite 用户访问test.oldboy.com 跳转到http://test.oldboy.com/$args
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
#案例6.网站跳转维护页面
[root@web01 /etc/nginx/conf.d]#cat re.conf
server {
listen 80;
server_name re.oldboy.com;
root /code;
charset utf-8,gbk;
index index.html;
location / {
set $ip 0; # 设置变量为0
if ($remote_addr = "10.0.0.1"){
set $ip 1; # 如果来源IP为0.1则设置为1
}
if ($ip = 0){ # 判断如果变量为0 则跳转维护页面
rewrite ^(.*)$ /wh.html break;
}
}
}
[root@web01 /etc/nginx/conf.d]#vim /code/index.html
[root@web01 /etc/nginx/conf.d]#cat /code/index.html
game....................
[root@web01 /etc/nginx/conf.d]#echo wh........ > /code/wh.html
客户端为WEB02:10.0.0.8
WEB02想要访问re.oldboy.com请问怎么办?
[root@web02 ~]#cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.7 re.oldboy.com
[root@web02 ~]#curl re.oldboy.com
game....................
客户端为10.0.0.1
通过浏览器测试
bash10.0.0.8访问测试
[root@web02 ~]#curl re.oldboy.com
wh........
bash#重点小结
1.通过客户端UA转发到不同的代码目录
2.rewirte跳转
面试题: 301和302的区别
301 永久跳转,只有第一次访问源站,第一次后每次直接访问新的网站。
302 临时跳转,每次先访问源站,然后在访问新的网站。
last 企业需求 卡住并且重新发起请求
break企业需求 卡住
3.资源跳转
location 后面匹配规则
=
^~
~
~*
/
通过域名访问网站,如果匹配不到默认返回第一个域名对应的内容。default
HTTP-->HTTPS
bash为什么需要https,因为http不安全,当我们使用http网站时,会遭到劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时信息泄露。
bash#https加密流程
首先服务器购买并部署证书,然后客户端通过浏览器去请求域名的时候,服务器端接收到请求并返回证书,证书里包含公钥,浏览器会去CA机构验证这
个证书的是否合法有效,如果不合法会给个警告提示,你访问的网站不安全,如果合法有效,浏览器这边把会生成一个随机数,比如用R来表示,通
过服务端返回的公钥对R进行加密,加密之后传给服务端。服务端用自己的私钥解自己的公钥,解开后获取到客户端传过来的R,然后用R再进行加密,
将加密后的内容传给自己的客户端,最后浏览器用自己的随机数进行解密。
bash1.配置源站
[root@web01 /etc/nginx/conf.d]#cat jc.conf
server {
listen 80;
server_name jc.oldboy.com;
location / {
root /code/jc;
index index.html;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
2.配置静态页面
[root@web01 /etc/nginx/conf.d]#mkdir /code/jc
[root@web01 /etc/nginx/conf.d]#cat /code/jc/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我是title</title>
</head>
<body>
<article>
<header>
<h1>我是妹妹</h1>
<p>创建时间:<time pubdate="pubdate">2025/4/20</time></p>
</header>
<p>
<b>Aticle</b>第一次用h5写文章,好他*的紧张...
</p>
<footer>
<p><small>版权所有!</small></p>
</footer>
</article>
</body>
</html>
3.host解析
#浏览器测试
bash#配置WEB02劫持WEB01
1.配置nginx
[root@web02 /etc/nginx/conf.d]#cat jc.conf
server {
listen 80;
server_name jc.oldboy.com;
location / {
proxy_pass http://10.0.0.7;
proxy_set_header Host $http_host;
}
}
[root@web02 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 /etc/nginx/conf.d]#systemctl restart nginx
2.修改host
#访问测试,没有问题
bash#WEB02篡改首页内容
[root@web02 /etc/nginx/conf.d]#cat jc.conf
server {
listen 80;
server_name jc.oldboy.com;
location / {
proxy_pass http://10.0.0.7;
proxy_set_header Host $http_host;
sub_filter '<h1>我是妹妹' '<h1>我是哥哥'; #修改内容
sub_filter '<small>版权所有' ' <small>开源'; #修改内容
}
}
[root@web02 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 /etc/nginx/conf.d]#systemctl restart nginx
#测试,篡改成功
bashhttps不支持续费,证书到期重新申请并进行替换 https不支持三级预警解析,如test.my.oldboy.com https显示绿色,说明整个网站的url都是https的 https显示黄色,说明网站代码中包含http的不安全链接 https显示红色,那么证书是假的或者证书过期
bash#nginx必须要有ssl模块
[root@web01 /code/jc]#nginx -V
--with-stream_ssl_module
1.生成证书(我们自己充当CA机构给自己颁发证书)
#创建存放证书的目录
[root@web01 /code/jc]#mkdir -p /etc/nginx/ssl_key
[root@web01 /code/jc]#cd /etc/nginx/ssl_key/
#使用openssl生成证书 过程中输入密码 1234
[root@web01 /etc/nginx/ssl_key]#openssl genrsa -idea -out server.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
........................+++++
.+++++
e is 65537 (0x010001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key
#生成自签证书,同时去掉私钥的密码 信息都可以自定义。
openssl req -days 36500 -x509 \
> -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
Generating a RSA private key
.......................+++++
....................................................................................+++++
writing new private key to 'server.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:BJ
Locality Name (eg, city) []:CHANGPING
Organization Name (eg, company) [Internet Widgits Pty Ltd]:OLDBOY
Organizational Unit Name (eg, section) []:oldboygirl
Common Name (e.g. server FQDN or YOUR name) []:linunc
Email Address []:123@.com
#配置证书实现单台web https加密
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 443 ssl; #监听443端口并启用SSL加密
server_name xbw.oldboy.com;
ssl_certificate ssl_key/server.crt; #证书文件路径
ssl_certificate_key ssl_key/server.key; #私钥文件路径
location / {
root /code/xbw;
index index.html index.htm;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#此时,输入域名测试,因为小霸王的listen成了443,访问80匹配不到默认是访问第一个
bash#xbw.oldboy.com:443访问成功
bash#加入跳转,访问默认的80端口自动跳转到443
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 443 ssl;
server_name xbw.oldboy.com;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
root /code/xbw;
index index.html index.htm;
}
}
server {
listen 80;
server_name xbw.oldboy.com; #如果有人访问xbw.oldboy.com默认80端口
return 302 https://$server_name$request_uri;#那么就临时跳转,$server_name=xbw.oldboy.com这个域名
} #$request_uri存放域名后面的半截,意思就是域名后面携带着uri。这里也要有uri
1:HTTP 请求(80端口)
用户访问 http://xbw.oldboy.com,Nginx 监听 80 端口的 server 块匹配该请求。
执行 return 302 重定向,将请求跳转至 https://xbw.oldboy.com,保留原始请求的 URI。
2:HTTPS 重定向(443端口)
浏览器自动向 https://xbw.oldboy.com 发起请求,使用 443 端口。
第一个 server 块处理 HTTPS 请求,加载 SSL 证书(server.crt)和私钥(server.key)进行加密通信。
3:静态资源处理
匹配 location /,设置根目录为 /code/xbw。
查找并返回 /code/xbw 下的 index.html 或 index.htm 文件。
#测试访问xbw.oldboy.com,不带端口可以访问
客户端访问流程: 1. 浏览器输入 http://xbw.oldboy.com ↓ 2. 80端口服务器捕获请求 ↓ 3. 返回302重定向到 https://xbw.oldboy.com ↓ 4. 浏览器发起 https://xbw.oldboy.com 请求 ↓ 5. 443端口服务器处理SSL握手 ↓ 6. 返回/code/xbw目录下的静态文件
bash1.购买云服务器 1核1G 包年 包月 按天付费 2.注册一个域名 3.需要备案 提交身份证正反面 拍照上传 接听电话 1-1.5个月,备案成功后域名才可以使用。 4.域名解析到自己购买的服务器公网IP地址 第一步: 申请一个证书

bash第二步下载证书到web01


bash[root@web01 /etc/nginx/ssl_key]#cat ../conf.d/xbw.conf
server {
listen 443 ssl;
server_name www.weixiang.info; #申请的域名
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
root /code/xbw;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.weixiang.info;
return 302 https://$server_name$request_uri;
}
[root@web01 /etc/nginx/ssl_key]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/ssl_key]#systemctl restart nginx
##先下载到windows再上传证书解压
[root@web01 /etc/nginx/ssl_key]#unzip 18172849_www.weixiang.info_nginx.zip
[root@web01 /etc/nginx/ssl_key]#ll
total 24
-rw-r--r-- 1 root root 4098 Apr 17 17:55 18172849_www.weixiang.info_nginx.zip
-rw-r--r-- 1 root root 1403 Apr 17 15:39 server.crt
-rw------- 1 root root 1704 Apr 17 15:38 server.key
-rw-r--r-- 1 root root 0 Apr 17 15:38 -sha256
-rw-r--r-- 1 root root 1675 Apr 17 17:54 www.weixiang.info.key
-rw-r--r-- 1 root root 3834 Apr 17 17:54 www.weixiang.info.pem
#覆盖
[root@web01 /etc/nginx/ssl_key]#mv www.weixiang.info.key server.key
mv: overwrite 'server.key'? y
[root@web01 /etc/nginx/ssl_key]#mv www.weixiang.info.pem server.crt
mv: overwrite 'server.crt'? y
[root@web01 /etc/nginx/ssl_key]#ll
total 16
-rw-r--r-- 1 root root 4098 Apr 17 17:55 18172849_www.weixiang.info_nginx.zip
-rw-r--r-- 1 root root 3834 Apr 17 17:54 server.crt
-rw-r--r-- 1 root root 1675 Apr 17 17:54 server.key
-rw-r--r-- 1 root root 0 Apr 17 15:38 -sha256
bash#配置静态页面的https
1.web01配置
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 80;
server_name www.weixiang.info;
root /code;
index index.html;
}
[root@web01 /etc/nginx/conf.d]#nginx -t
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
bash#WEB02配置静态页面
[root@web02 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 80;
server_name www.weixiang.info;
root /code;
index index.html;
}
[root@web02 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 /etc/nginx/conf.d]#systemctl restart nginx
[root@web02 /etc/nginx/conf.d]#echo web02... > /code/index.html
bash#负载均衡配置代理到后端
[root@lb01 /etc/nginx/conf.d]#cat xbw.conf
upstream xbw {
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name www.weixiang.info;
location / {
proxy_pass http://xbw;
include proxy_params;
}
}
[root@lb01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx/conf.d]#systemctl restart nginx
访问测试
10.0.0.5 xbw.oldboy.com
测试结果 以轮循的方式转发给后端的两台WEB
WEB01..
WEB02..
bash#配置HTTPS证书,从浏览器到负载均衡配置,负载均衡到后端不需要配置
1.将证书从WEB01拷贝到LB01
[root@web01 /etc/nginx]#scp -r ssl_key 10.0.0.5:/etc/nginx/
Authorized users only. All activities may be monitored and reported.
root@10.0.0.5s password:
server.key 100% 1675 592.4KB/s 00:00
-sha256 100% 0 0.0KB/s 00:00
server.crt 100% 3834 1.2MB/s 00:00
18172849_www.weixiang.info_nginx.zip
[root@lb01 /etc/nginx/conf.d]#cat xbw.conf
upstream xbw {
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 443 ssl;
server_name www.weixiang.info;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
proxy_pass http://xbw;
include proxy_params;
}
}
server {
listen 80;
server_name www.weixiang.info;
return 302 https://$server_name$request_uri;
}
[root@lb01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#浏览器访问测试,由不安全变为安全
bash#没办法用证书,因为之前已经写到了数据库,只能用假证
[root@lb01 /etc/nginx/conf.d]#cat wp.conf
upstream wp {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 443 ssl;
server_name wp.oldboy.com;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
proxy_pass http://wp;
include proxy_params;
}
}
server {
listen 80;
server_name wp.oldboy.com;
return 302 https://$server_name$request_uri;
}
[root@lb01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx/conf.d]#systemctl restart nginx
#出现不安全,因为证书虽然是真的,但是域名跟证书不匹配
bash#在测试过程中出现图片卡不出来的情况,出现这种情况先单台测试,解析到7试试,发现没问题
#WEB01和WEB02的wordpress中开始PHP对https的支持
[root@web01 /etc/nginx/conf.d]#cat wordpress.conf
server {
listen 80;
server_name wp.oldboy.com;
root /code/wp;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param HTTPS on; # 负载均衡开启了HTTPS 必须有此配置,如果负载均衡关闭了HTTPS ,则必须删除此配置。
}
}
#测试,成功
bash服务器的角度!
1.https的加密流程
2.如何查看证书什么时间过期
[root@lb01 ~]#openssl x509 -in /etc/nginx/ssl_key/server.crt -noout -dates
notBefore=Apr 17 00:00:00 2025 GMT
notAfter=Jul 16 23:59:59 2025 GMT
1.部署单台服务器实现HTTPS
2.部署集群实现HTTPS wordpress zh部署上证书
扩展:
1.购买一台ECS 1核1G 2核1G内存
2.注册一个域名-->备案 自己写博客。
bash#Keepalived概念
一般是指两台机器启动着完全相同的业务系统,当有一台宕机了,另外一台服务器就能快速的接管,对于访问的用户是无感知的
keepalived软件是基于VRRP协议实现的,VRRP虚拟路由冗余协议,主要用于解决单点故障问题
#抢占式模式
主节点(MASTER)故障恢复后,会主动抢占备节点(BACKUP)的VIP(虚拟ip),恢复为主节点。主节点优先级需高于备节点(如主节点优先级100,备节点优先级90)。、
#非抢占式模式
主备节点均配置为 BACKUP 状态,并添加 nopreempt 参数,优先级仍生效:高优先级节点初始成为主节点,但故障恢复后不会主动抢占VIP
#语法
使用 keepalived -t 进行语法检查
keepalived -t -f /etc/keepalived/keepalived.conf
-t:测试模式,检查配置文件的语法并退出(不实际运行服务)。
-f:指定配置文件路径(默认路径是 /etc/keepalived/keepalived.conf,可省略
#脑裂
两台路由器都收不到对方消息,都会配置虚拟的ip和mac,需要自动或者手动杀死一台
#脑裂产生原因
服务器网线松动等网络故障
服务器硬件故障发生损坏现象而崩溃
主备都开启firewalld防火墙,导致接受不到广播
#语法
使用 keepalived -t 进行语法检查
keepalived -t -f /etc/keepalived/keepalived.conf
-t:测试模式,检查配置文件的语法并退出(不实际运行服务)。
-f:指定配置文件路径(默认路径是 /etc/keepalived/keepalived.conf,可省略)
#脑裂的危害
主备节点同时持有VIP,导致服务冲突或数据损坏(如共享存储的数据库读写冲突)
客户端无法正确路由请求,导致业务不可用
bash#扩展一台LB02-10.0.0.6 负载均衡的备用服务器
克隆服务器
修改IP地址
xshell远程连接
安装Nginx
[root@lb02 ~]#yum -y install nginx
将LB01的数据同步到LB02
[root@lb02 ~]#rsync -avz --delete 10.0.0.5:/etc/nginx/ /etc/nginx/
[root@lb02 ~]#scp 10.0.0.5:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/
windows修改hosts文件解析到lb02,测试
bashLB01部署
1.下载keepalived
[root@lb01 /etc/nginx/conf.d]#yum -y install keepalived
2.配置keepalived
[root@lb01 ~]#cat /etc/keepalived/keepalived.conf
global_defs { #全局配置
router_id lb01 #标识身份-名称
}
vrrp_instance VI_1 {
state MASTER #标识角色状态 主节点
interface ens33 #网卡绑定接口
virtual_router_id 50 #虚拟路由id
priority 150 #优先级150
advert_int 1 #检测间隔时间
authentication { #认证
auth_type PASS #认证方式
auth_pass 1111 #认证密码
}
virtual_ipaddress {
10.0.0.3 #虚拟的vip地址
}
}
3.启动keepalived
[root@lb01 /etc/nginx/conf.d]#systemctl start keepalived.service
[root@lb01 /etc/nginx/conf.d]#systemctl enable keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
LB02部署
1.下载keepalived
[root@lb01 /etc/nginx/conf.d]#yum -y install keepalived
2.配置keepalived
[root@lb02 ~]#cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP #标识角色状态 备用节点
interface ens33
virtual_router_id 50
priority 100 #优先级150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
3.启动keepalived
[root@lb02 ~]#systemctl start keepalived
[root@lb02 ~]#systemctl enable keepalived
#测试,10.0.0.3在lb01
[root@lb01 ~]#ip a|grep 10.0.0.3
inet 10.0.0.3/32 scope global ens33
[root@lb02 ~]#ip a|grep 10.0.0.3
无
#host解析成10.0.0.3也能访问到
bash#通过抓包工具测试,可以看到lb01的ip地址10.0.0.5一直在通过广播ip224.0.0.18向外广播,频率为一秒一次,虚拟的ip地址为10.0.0.3,状态为good
bash#把lb01的keepalived停掉,vip会自动飘移到lb02服务器,中间会丢弃一个包,对用户来说无感知
[root@lb01 ~]#systemctl stop keepalived.service
#停掉后vip飘逸到leb02
[root@lb02 ~]#ip a|grep 10.0.0.3
inet 10.0.0.3/32 scope global ens33
bash此时再把lb01的keepalived的服务启动,lb01会把vip自动抢占过来
#两台服务开启
C:\Users\z1340>arp -a
Internet 地址 物理地址 类型
10.0.0.3 00-0c-29-df-98-1e 动态
10.0.0.4 00-0c-29-ee-18-18 动态
10.0.0.5 00-0c-29-df-98-1e 动态
10.0.0.6 00-0c-29-b5-b3-a0 动态
#把lb01的服务停掉
Internet 地址 物理地址 类型
10.0.0.3 00-0c-29-b5-b3-a0 动态
10.0.0.4 00-0c-29-ee-18-18 动态
10.0.0.5 00-0c-29-df-98-1e 动态
10.0.0.6 00-0c-29-b5-b3-a0 动态
bash#修改lb01为非抢占模式
[root@lb01 ~]#cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state backup
interface ens33
virtual_router_id 50
priority 150
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
重启生效
[root@lb01 ~]#systemctl restart keepalived.service
#修改lb02为非抢占模式
[root@lb02 ~]#cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 50
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
重启生效
[root@lb01 ~]#systemctl restart keepalived.service
#测试,服务都开启的情况下,vip在lb01
[root@lb01 ~]#ip a|grep 10.0.0.3
inet 10.0.0.3/32 scope global ens33
[root@lb02 ~]#ip a|grep 10.0.0.3
#测试,停掉lb01的keepalived服务,vip在lb02
[root@lb01 ~]#systemctl stop keepalived.service
[root@lb01 ~]#ip a|grep 10.0.0.3
[root@lb02 ~]#ip a|grep 10.0.0.3
inet 10.0.0.3/32 scope global ens33
#测试,开启lb01的keepalived服务,vip还在lb02
[root@lb01 ~]#ip a|grep 10.0.0.3
[root@lb02 ~]#ip a|grep 10.0.0.3
inet 10.0.0.3/32 scope global ens33
bash面试题:出现脑裂的原因
1、服务器网线松动等网络故障
2、服务器硬件故障发生损坏现象而崩溃
3、主备都开启firewalld防火墙
如果开启了防火墙,默认拒绝80和443的访问,只允许了SSH远程连接服务22端口
放行80和443端口
[root@lb01 ~]#firewall-cmd --permanent --add-port=80/tcp
success
[root@lb01 ~]#firewall-cmd --permanent --add-port=443/tcp
success
[root@lb01 ~]#firewall-cmd --reload
success
#开启防火墙后虚拟ip同时在lb01与lb02出现,这就是脑裂
[root@lb01 ~]#systemctl start firewalld
[root@lb01 ~]#ip a |grep 10.0.0.3
inet 10.0.0.3/32 scope global ens33
[root@lb02 ~]#systemctl start firewalld
[root@lb02 ~]#ip a|grep 10.0.0.3
inet 10.0.0.3/32 scope global ens33
#抓包测试
bash解决脑裂:
手动停止其中一台keepalived
LB02写出现脑裂则自动杀死当前的keepalived服务(必须为抢占式)
1.LB02生成密钥对和LB01做免密钥的配置
[root@lb02 ~]#ssh-keygen
[root@lb02 ~]#ssh-copy-id 10.0.0.5
2.脚本
[root@lb02 ~]#cat test.sh
#1.探测本机是否存在10.0.0.3 lb02变量的值要么1 要么是0 存在为1 不存在为0
lb02=`ip a|grep 10.0.0.3|wc -l`
#2.探测lb01是否存在10.0.0.3
lb01=`ssh 10.0.0.5 "ip a|grep 10.0.0.3"|wc -l`
#3.判断两台如果同时存在10.0.0.3则关闭本机的keepalived
[ $lb01 -eq 1 -a $lb02 -eq 1 ] && systemctl stop keepalived
3.开启lb02的防火墙测试脚本是否执行成功
[root@lb02 ~]#systemctl start firewalld
[root@lb02 ~]#ip a|grep 10.0.0.3
inet 10.0.0.3/32 scope global ens33
[root@lb02 ~]#sh test.sh
Authorized users only. All activities may be monitored and reported.
#执行成功
[root@lb02 ~]#ip a|grep 10.0.0.3
bash如果lb01的nginx挂掉,不会自动将vip漂移到备用服务器。必须keepalived挂掉,vip才会漂移
所以我们要写一个脚本检测lb01的nginx服务是否存在,如果nginx停止工作,那么要将keepalived服务停止,让VIP地址漂移到LB02备用服务器持续工作。
1.写脚本探测nginx
[root@lb01 ~]#cat chek.sh
#!/bin/bash
#通过进程取出nginx的存活状态 如果为0说明nginx挂掉,如果不为0说明在运行
ng=`ps axu|grep nginx|grep -v grep|wc -l`
#判断如果ng为0,先尝试重启nginx
if [ $ng -eq 0 ];then
# 重启nginx服务
systemctl restart nginx &>/dev/null
ng=`ps axu|grep nginx|grep -v grep|wc -l`
[ $ng -eq 0 ] && systemctl stop keepalived
fi
#增加执行x权限
[root@lb01 ~]#chmod +x chek.sh
[root@lb01 ~]#ll chek.sh
-rwxr-xr-x 1 root root 362 Apr 18 11:43 chek.sh
2.将脚本将keepalived结合
[root@lb01 ~]#cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb01
}
vrrp_script check_web {
script "/root/chek.sh"
interval 3
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 50
priority 150
#nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
track_script {
check_web
}
}
[root@lb01 ~]#systemctl restart keepalived
3.定时
bash从OSI模型考虑优化方向
硬件 代理 (cpu) 静态(磁盘) 动态(cpu、内存)
网络 带宽、丢包、延迟
系统 文件描述符
应用 服务与服务保持长连接 http1.1
服务 静态资源服务优化
#安装ab压力测试工具
[root@web01 ~]#yum -y install httpd-tools
#创建静态文件
[root@web01 /code/s]#cat /etc/nginx/conf.d/xbw.conf
server {
listen 80;
server_name s.oldboy.com;
location / {
root /code/s;
index index.html index.htm;
}
}
[root@web01 /code/s]#echo '大家好' > index.html
[root@web01 /code/s]#cat index.html
大家好
#解析到linux host文件
[root@web01 /code/s]#tail -1 /etc/hosts
10.0.0.7 s.oldboy.com
[root@web01 /code/s]#ping s.oldboy.com
PING s.oldboy.com (10.0.0.7) 56(84) bytes of data.
64 bytes from s.oldboy.com (10.0.0.7): icmp_seq=1 ttl=64 time=0.194 ms
64 bytes from s.oldboy.com (10.0.0.7): icmp_seq=2 ttl=64 time=0.111 ms
#压测工具使用方式
ab -n20000 -c 200 http://s.oldboy.com/index.html
-n 要执行的请求数
-c 请求的并发数
-k 是否开启长连接
#压力测试
[root@web01 /code/s]#ab -n20000 -c 200 http://s.oldboy.com/index.html
This is ApacheBench, Version 2.3 <$Revision: 1874286 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking s.oldboy.com (be patient)
Completed 2000 requests
Completed 4000 requests
Completed 6000 requests
Completed 8000 requests
Completed 10000 requests
Completed 12000 requests
Completed 14000 requests
Completed 16000 requests
Completed 18000 requests
Completed 20000 requests
Finished 20000 requests
Server Software: nginx/1.26.1 # 服务端服务版本号
Server Hostname: s.oldboy.com # 域名
Server Port: 80 # 端口
Document Path: /index.html # 具体的资源
Document Length: 10 bytes # 资源的大小
Concurrency Level: 200 # 并发请求200
Time taken for tests: 9.551 seconds # 处理完成的时间
Complete requests: 20000 # 总请求数2万
Failed requests: 0 # 失败的请求
Total transferred: 5180000 bytes # 总传输大小
HTML transferred: 200000 bytes # HTML大小
Requests per second: 2094.10 [#/sec] (mean) # 吞吐率 请求数 和资源大小有关,QPS:2094.10 请求/秒
Time per request: 95.506 [ms] (mean) #用户平均等待时间95毫秒
Time per request: 0.478 [ms] (mean, across all concurrent requests) #平均处理时间
Transfer rate: 529.66 [Kbytes/sec] received # 数据大小,网络吞吐
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 36 61.3 30 1072
Processing: 17 60 12.8 58 263
Waiting: 1 51 11.3 51 251
Total: 31 95 63.3 89 1135
Percentage of the requests served within a certain time (ms)
50% 89
66% 98
75% 105
80% 110
90% 117
95% 124
98% 130
99% 140
100% 1135 (longest request)
[root@web01 /code/s]#netstat -an|grep 80|head
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 10.0.0.7:80 10.0.0.7:46606 TIME_WAIT
tcp 0 0 10.0.0.7:80 10.0.0.7:58420 TIME_WAIT
tcp 0 0 10.0.0.7:80 10.0.0.7:50480 TIME_WAIT
tcp 0 0 10.0.0.7:80 10.0.0.7:57700 TIME_WAIT
tcp 0 0 10.0.0.7:80 10.0.0.7:44252 TIME_WAIT
tcp 0 0 10.0.0.7:80 10.0.0.7:50034 TIME_WAIT
tcp 0 0 10.0.0.7:80 10.0.0.7:51310 TIME_WAIT
tcp 0 0 10.0.0.7:80 10.0.0.7:34806 TIME_WAIT
tcp 0 0 10.0.0.7:80 10.0.0.7:54876 TIME_WAIT
#测试动态页面
[root@db01 ~]#ab -n20000 -c 200 http://wp.oldboy.com/
This is ApacheBench, Version 2.3 <$Revision: 1874286 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking wp.oldboy.com (be patient)...
..done
Server Software:
Server Hostname: wp.oldboy.com
Server Port: 80
Document Path: /
Document Length: 114 bytes
Concurrency Level: 10
Time taken for tests: 30.088 seconds
Complete requests: 50
Failed requests: 0
Total transferred: 10800 bytes
HTML transferred: 5700 bytes
Requests per second: 1.66 [#/sec] (mean)
Time per request: 6017.626 [ms] (mean)
Time per request: 601.763 [ms] (mean, across all concurrent requests)
Transfer rate: 0.35 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 72 78 3.7 78 93
Processing: 198 488 814.3 204 5214
Waiting: 198 487 814.4 204 5214
Total: 274 566 814.8 282 5292
Percentage of the requests served within a certain time (ms)
50% 282
66% 286
75% 294
80% 302
90% 1302
95% 1314
98% 5292
99% 5292
100% 5292 (longest request)
#需要大家理解的值
QPS每秒的查询(数据库) 吞吐率(资源相关)
QPS:
QPS= 总查询次数/总时间(秒)
示例:
1.若 MySQL 1 秒内处理了 500 次查询 → QPS = 500。
高 QPS 意味着数据库处理速度快,但需结合查询复杂度分析(如 QPS 高但存在慢查询仍会拖垮系统)
吞吐率:吞吐率表示系统单位时间内处理的数据量,通常与资源(网络、磁盘、CPU)的传输或处理能力相关,单位可能是网络吞吐:MB/s(如 Nginx 传输静态文件的速度)、磁盘吞吐:IOPS(每秒读写操作次数)或 MB/s、业务吞吐:请求数/秒(如 Web 服务器的 Requests/s)。
计算方式:吞吐率= 总处理数据量/总时间(秒)
示例:
1.若 Web 服务器每秒传输 10MB 静态文件 → 吞吐率 = 10MB/s。
2.若应用每秒处理 200 个 HTTP 请求 → 吞吐率 = 200 请求/秒。
bash#默认改成65535
[root@web01 ~]#cat /etc/security/limits.conf |tail -1
* - nofile 65535
bash#四次挥手后,主动方不是立刻进入closed状态,而是进入一个TIME_WAIT(时间等待),会占用端口,那么这个端口是没办法被新的进程所连接,当出现大量的TIME_WAIT,新的端口就连接不进来,可以通过netstat -an|grep 80|head查看端口状态,假如65535个端口,被用户访问后,用户走了,端口就进入TIME_WAIT约2分钟时间,这中间,用户是访问不了其中的端口的。
#开启复用,如果端口状态处于TIME_WAIT,那么就允许新的端口连接,轻易不要加,开启前提是负载均衡端口不够用,并发高,客户端请求三次握手连接不上,看7种状态,用户连接的比较多,但是都是失败的,然后看一下后台是否又大量的TIME_WAIT导致的,如果是,那么开启。并且要禁用时间戳
#为什么禁用时间戳,如果开启后,那就会检查时间戳,如果对方发来的包的时间戳是乱跳的或者是滞后的,这样服务器就不会回复,把带了“倒退”的时间戳的包当作是“recycle的tw连接的重传数据,而不是新的请求”,于是丢掉不回包,就出现了开始说的syn不响应
[root@web01 ~]#vim /etc/sysctl.conf
[root@web01 ~]#tail -2 /etc/sysctl.conf
net.ipv4.tcp_tw_reuse = 1 #开启端口复用
net.ipv4.tcp_timestamps = 0 #禁用时间戳
#让配置文件生效
[root@web01 ~]#sysctl -p
kernel.sysrq = 0
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.tcp_syncookies = 1
kernel.dmesg_restrict = 1
net.ipv6.conf.all.accept_redirects = 0
net.ipv6.conf.default.accept_redirects = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_timestamps = 0
bashupstream http_backend {
server 127.0.0.1:8080;
keepalive 16; #每个work保持的连接池大小(即连接池容量)。
keepalive_timeout 30s; #连接空闲超时(需nginx>=1.15.3)
}
setver{
...
location /http/{
proxy_set_header Host $http_host;
proxy_http_version 1.1; #必须使用HTTP/1.1
fastcgi_keep_conn on; #开启长连接
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
...
#keepalive 16表示每个work保持的连接池大小(即连接池容量)。请求完成后,连接不会立即关闭,而是放入空闲池。新请求优先复用空闲连接,若池满则关闭最旧连接。减少nginx后端tcp握手开销,提升负载均衡性能
#连接池概念:为减少重复建立和销毁连接的开销而预先创建并维护一组连接的技术,nginx采用连接池技术来优化与后端服务器的通信性能,可以有效减少连接的创建和销毁成本,特别是在高并发的情况下,提升性能
bash浏览器缓存设置用于提高网站性能,尤其是新闻网站,图片一旦发布,改动比较小,希望用户访问一次后,图片在用户的浏览器长时间的缓存。浏览器有自己的缓存机制,它是基于http协议缓存机制来实现的,在http协议种有很多头部信息,那么实现浏览器的缓存就需要依赖特殊的头部信息来与服务器进行特殊的验证。

bash浏览器校验机制: 浏览器先请求自己的缓存,通过Etag看是否过期,如果有Etag,那么会拿着自己的的Etag向服务器发起请求,服务器端会响应一个IF-None-Match,看两个值是否一样,如果相等那说明没有修改过,直接返回给浏览器。 如果没有Etag,那么会查询一下last-Modified(最后修改时间),向服务器请求if-Modified-Since(文件的最后修改时间),看两个时间是否一样,如果是,返回304(没有修改)给浏览器。 (比如10.00访问1.png,那么浏览器会缓存这个时间,如果再向服务器请求1.png,那么会先查看这个资源的头部信息,如果这个时间也是10.00,那么返回304)

bash配置静态缓存场景
[root@web01 /etc/nginx/conf.d]#cat wordpress.conf
server {
listen 80;
server_name wp.oldboy.com;
root /code/wp;
location / {
index index.php index.html;
}
location ~ .*\.(jpg|gif|png)$ {
expires 7d; # 将静态图片资源在客户浏览器端缓存7天
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_keep_conn on;
#fastcgi_param HTTPS on;
}
}
root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
#可以看到缓存到了7天后


bash------------------- # 用户访问的静态资源不走缓存,每次都要请求源站。
location ~ .*\.(jpg|gif|png)$ {
add_header Cache-Control no-store; #不存储
add_header Pragma no-cache;
}
-------------------
#测试,履行者没有缓存信息
bashvim /etc/nginx/nginx.conf
...
sendfile on;
tcp_nodelay on; # 小文件业务建议开启,来一个包发一个包不等待行
#tcp_nopush on; # 大文件业务建议开启,提升网络传输效率
rewrite_log on;
keepalive_timeout 65;
.....
bash1.生成1个文档
[root@web01 /etc/nginx/conf.d]#cat /etc/services /etc/services /etc/services > /code/wp/1.txt
[root@web01 /etc/nginx/conf.d]#ll -h /code/wp/1.txt
-rw-r--r-- 1 root root 2.0M Apr 21 11:20 /code/wp/1.txt
2.浏览器访问1.txt看大小情况
访问大小为2M
wp.oldboy.com/1.txt
10个人同时访问我资源,我必须同时用于20M带宽才能返回,要进行压缩

bash3.配置gzip压缩
vim /etc/nginx/conf.d/wordpress.conf
...
location ~ .*\.(txt|xml|html|json|js|css)$ {
gzip on; #开启压缩
gzip_http_version 1.1; #压缩版本1.1
gzip_comp_level 6; #压缩等级,1-9,1速度最快,但压缩率最低,9速度最慢,但压缩率最高
gzip_types text/plain application/json application/x-javascript application/css application/xml text/javascript;
}
...
nginx -t
systemctl restart nginx
再次访问1.txt看大小
wp.oldboy.com/1.txt

bash压缩图片测试
#测试图片原始大小

bash'[root@web01 /etc/nginx/conf.d]#cat wp.conf
server {
listen 80;
server_name wp.oldboy.com;
location / {
index index.php idnex.html;
root /code/wp;
}
location ~\.php$ {
root /code/wp;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_keep_conn on;
}
location ~ .*\.(jpg|png|gif)$ {
root /code/images;
#gzip on;
#gzip_http_version 1.1;
#gzip_comp_level 7;
#gzip_types image/jpeg image/gif image/png;
}
}
[root@web01 /etc/nginx/conf.d]#vim wp.conf
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart ngi
#浏览器测试


bash指的是防止资源被其他网站恶意盗用
用户访问新电商平台,在新平台进行一系列操作,但是其中一部分资源是由电商平台响应给用户的,比如图片,用的是电商平台的
1.在WEB02准备静态图片资源
[root@web02 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 80;
server_name dbw.oldboy.com;
location / {
root /code/dbw;
index index.html index.htm;
}
}
host解析,访问

bash3.在WEB01配置盗链,获取WEB02的资源
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 80;
server_name xbw.oldboy.com;
root /code;
index index.html index.htm;
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]#systemctl restart nginx
[root@web01 /etc/nginx/conf.d]#cat /code/index.html
<html>
<head>
<meta charset="utf-8">
<title>oldboyedu.com</title>
</head>
<body style="background-color:pink;">
<img src="http://xbw.oldboy.com/11.jpg"/> #根据情况修改你的服务器地址
</body>
</html>
4.windows 解析
10.0.0.7 xbw.oldboy.com
10.0.0.8 dbw.oldboy.com
#访问测试

bash如果不想让其他网站盗取我们的资源;使用以下配置
[root@web02 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 80;
server_name xbw.oldboy.com;
root /code;
index index.html;
location ~* \.(gif|jpg|png|bmp)$ {
valid_referers none blocked *.baidu.com; # 只允许百度相关的域名获取我的资源
if ($invalid_referer) {
return 403;
#rewrite ^(.*)$ /daolian.png break;
}
}
}

bash如果跳转其他页面使用rewrite
[root@web02 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 80;
server_name xbw.linuxnc.com;
root /code/xbw;
index index.html;
location ~* \.(gif|jpg|png|bmp)$ {
valid_referers none blocked *.baidu.com;
if ($invalid_referer) {
#return 403; #注释返回代码
rewrite ^(.*)$ /t.png break; # 跳转到/code/xbw/t.png
#可以使用301和302跳转到公司网站的首页
}
}
}
#浏览器测试访问

bash什么是跨域访问,当我们通过浏览器访问a网站时,同时会利用ajax或其他方式,同时请求b网站,这样的话就出现了请求一个页面,使用两个域名,这种方式对浏览器是禁止的
WEB02跨域访问WEB01的内容
[root@web02 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 80;
server_name xbw.linuxnc.com;
root /code;
index index.html;
}
[root@web02 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 /etc/nginx/conf.d]#systemctl restart nginx
[root@web02 /etc/nginx/conf.d]#cat /code/index.html
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试ajax和跨域访问</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://xbw.oldboy.com", #请求的域名
success: function(data) {
alert("sucess 成功了!!!");
},
error: function() {
alert("fail!!,跨不过去啊,不让进去啊,只能...!");
}
});
});
</script>
<body>
<h1>跨域访问测试</h1>
</body>
</html>
#测试是否可以跨域,默认浏览器的禁止的。用web02访问测试


bashWEB01配置允许跨域
[root@web01 /etc/nginx/conf.d]#cat xbw.conf
server {
listen 80;
server_name xbw.oldboy.com;
root /code;
index index.html;
location ~ .*\.(html|htm)$ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
}
}
nginx -t
systemctl restart nginx
修改host解析
#然后在测试跨域请求。

bashcpu亲和减少进程间不断的频繁切换,减少性能损耗,其实现原理是把cpu核心和nginx进程工作绑定的方式,把每个work进程固定到对应的cpu执行,减少cpu的cache miss,获得更好的性能
nginx启动多少个work子进程和cpu的核心数有关,1核心只有1个子进程
没做亲和力测试之前
[root@web01 ~]#ps -eo pid,args,psr|grep [n]ginx
1142 nginx: master process /usr/ 2
1143 nginx: worker process 0
1144 nginx: worker process 0
1145 nginx: worker process 0
1146 nginx: worker process 1
#重启再测试,发现有的进程在闲置,虽然有4核,但是工作的cpu只有3核,没有平均的分配核心数
[root@web01 ~]#systemctl restart nginx
[root@web01 ~]#ps -eo pid,args,psr|grep [n]ginx
1433 nginx: master process /usr/ 2
1434 nginx: worker process 3
1435 nginx: worker process 1
1436 nginx: worker process 3
1437 nginx: worker process 2
[root@web01 ~]#cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
worker_cpu_affinity auto; #打开cpu亲和
#再测试,发现一个进程对应一个cpu核心
[root@web01 ~]#ps -eo pid,args,psr|grep [n]ginx
1468 nginx: master process /usr/ 0
1469 nginx: worker process 0
1470 nginx: worker process 1
1471 nginx: worker process 2
1472 nginx: worker process 3
bash#隐藏服务版本号,防止别人做最对性漏洞攻击

bash[root@web01 ~]#vim /etc/nginx/nginx.conf
..
http {
include
sendfile on;
#tcp_nopush on;
server_tokens off; #开启
...

bash1、CPU亲和、worker进程数默认和CPU核心数量相同
2、调整每个worker进程的最大连接数,默认是的1024 调整25532
3、开启tcp长连接,以及长连接超时时间keepalived
4、开启文件传输压缩gzip
5、开启静态文件expires缓存
6、隐藏nginx版本号
7、配置防盗链、以及跨域访问
8、优雅显示nginx错误页面 #定向到别的页面
9、文件的高效读取sendfile、nopush
10、文件的传输实时性、nodealy
11、nginx加密传输https优化
12、防DDOS、cc攻击,限制单IP并发连接,以及http请求 连接限制和请求限制。

bash1、DNS解析流程
1.浏览器输入域名www.baidu.com,会先查询浏览器缓存,如果有就返回IP与百度服务器建立连接,没有则查询本地的HOSTS文件
2.如果HOSTS有就返回IP与百度服务器建立连接,如果没有继续查询本地的DNS
3.本地DNS一般是我们自己配置的比如223.5.5.5、8.8.8.8、114.114.114.114,查询本地DNS是否有对应的IP,如果是223.5.5.5,那么就
去查询阿里云的dns,看是否有对应关系 如果有返回给浏览器 如果没有则查询根服务器
----------1-3过程称为递归查询---------------
4.根服务器不存储域名解析,会给LDNS返回顶级域.com的服务器IP地址
5.LDNS重新请求.com顶级域服务器 .com不存在域名解析,会返回权威域名服务器的IP地址给LDNS
6.LDNS重新请求baidu.com权威域名服务器,权威域服务器就是我们自己配置的A记录解析,将A记录对应的IP地址返回给LDNS
7.LDNS拿到后自己缓存一份 返回给浏览器一份
8.浏览器和拿到的百度服务器IP地址建立连接
---------迭代查询 有去有回
2、TCP三次握手
第一次握手:客户端向服务器端发送报文,内容是想建立连接SYN=1,并且客户端向服务器发送数据包编号seq=x(用x表示第一个数据包)
第二次握手:服务器端收到后会响应客户端表示已经收到ACK=1,并且发出建立连接请求SYN=1,并且我给你发送我的第一个数据包seq=y,并且还会
确认要给发我下一个数据包了Ack=x+1
第三次握手:客户端响应服务器端你的数据我收到了ACK=1,并且我给你发送第二个数据包了seq=x+1,并且你下次应该给我发第二个数据包了Ack=y+1
3、http请求
请求页面内容,请求头部,请求行,请求空行,请求内容主题。请求头部的话有请求主机名是什么,请求类型,请求是否压缩,请求长连接,请求编码,
请求语言,请求缓存。
请求后响应。响应服务器版本,响应主机,响应是否压缩,响应长连接,响应缓存等等。
请求方法get,post,put、delete
4、https
部署在表示层,首先服务器购买并部署证书,然后客户端通过浏览器去请求域名的时候,服务器端接收到请求并返回证书,证书里包含公钥,浏览器会
去CA机构验证这个证书的是否合法有效,如果不合法会给个警告提示,你访问的网站不安全,如果合法有效,浏览器这边把会生成一个随机数,
比如用R来表示,通过服务端返回的公钥对R进行加密,加密之后传给服务端。服务端用自己的私钥解自己的公钥,解开后获取到客户端传过来的R,
然后用R再进行加密,将加密后的内容传给自己的客户端,最后浏览器用自己的随机数进行解密。
5、如何实现负载均衡
proxy_pass+upstream模块实现负载均衡
6、负载均衡的调度算法
rr轮询 按时间顺序逐一分配到不同的后端服务器(默认)
weight 加权轮询,weight值越大,分配到的访问几率越高
ip_hash 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn 最少链接数,那个机器链接数少就分发
7、四层和七层的区别
四层是在传输层,已经将数据转发了,七层在应用层才能转发数据,四层基于ip+端口,七层是域名。
四层改写地址,解决负载均衡端口不够用,用lvs来实现 1.DR模式2.NAT模式3.TUN隧道模式4.FULLNAT模式
8、keepalived脑裂
因为防火墙开启阻止了arp,或者硬件老化,网络问题,解决脑裂就说在备用服务器写一个脚本,如果两边都有ip地址,随机杀死一个
9、nginx优化
10、location匹配规则
tcp握手之后就是请求静态/动态数据,请求动态数据就是到nginx,nginx将动态数据发给php-frm,php-frm用自己的工作进程warrap去解析,
如果可以解析就返回给浏览器,解析不了,tcp与数据库连接,数据库返回给php子进程,php再给nginx,最后再返回给浏览器。如果请求静态资源,
那么直接请求本地磁盘,如果通过nfs网络文件系统挂载的,那么会请求nfs磁盘
bash#虚拟机让其他人用windows进行访问
1、NAT改为桥接模式,此时网络已经连接不上,xshell连接不上
bash2、登录虚拟机进行操作
bash找个本地网络段没人用的ip

bash3、将虚拟机的ip改成跟本地相同网段的ip,重启网卡 systemctl restart network
bash百度可以访问


bash常见的协议包抓取: 1、ARP协议 2、ICMP协议 3、TCP协议 4、UDP协议 5、NDS协议 6、HTTP协议 捕获--选项,两种模式 普通模式:只接受正确的发到你网卡上的包,其他包抓不到 混杂模式:所有的都可以接收到,默认是混杂
bash在 Wireshark 中,过滤条件 tcp.flags.ack == 0 and tcp.flags.syn == 1 表示筛选出所有仅设置 SYN 标志位(值为 1)且未设置 ACK 标志位(值为 0) 的 TCP 数据包。这种数据包通常是 TCP 三次握手的第一步,即客户端向服务器发起连接请求的初始 SYN 包。

bash查看源地址是我本机发出去的包

bash查看源地址是我本机,目标地址是网关的包

l
本文作者:张龙龙
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!