(
1
)准备软件包并查看旧版软件
[root@YH1 ~]# ls
公共 视频 ⽂档 ⾳乐 anaconda-ks.cfg nginx-
1.22.1.tar.gz #旧版nginx
模板 图⽚ 下载 桌⾯ initial-setup-ks.cfg nginx-
1.24.0.tar.gz #新版nginx
[root@YH1 nginx]# nginx -V #查看nginx版本信息及安
装的模块
nginx version: nginx/1.22.1 #当前版本为1.22.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-15)
(GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --
user=nginx --group=nginx --with-http_ssl_module --
with-http_stub_status_module --with
http_realip_module --with-stream
[root@YH1 ~]# systemctl start nginx #启动nginx[root@YH1 ~]# tar -xf nginx-1.24.0.tar.gz -C
/usr/src #解压1.24.0版本
[root@YH1 ~]# cd /usr/src/nginx-1.24.0/ #移动到
解压好的1.24.0的nginx安装包⽬录
[root@YH1 nginx-1.24.0]# ls
auto CHANGES.ru configure html man
src
CHANGES conf contrib LICENSE README
[root@YH1 nginx-1.24.0]# ./configure --
prefix=/usr/local/nginx --user=nginx --group=nginx
--with-http_ssl_module --with
http_stub_status_module --with-http_realip_module
--with-stream
......省略部分预配置信息......
[root@YH1 nginx-1.24.0]# make #预配置后进⾏编译,
不要进⾏make install编译安装
......省略部分编译信息......
[root@YH1 nginx-1.24.0]# mv
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx.old
#此次mv修改的是1.22.1版本的nginx执⾏程序,将1.22.1nginx
程序修改为nginx.old,以防不与⼀会的新版本重名出现错误,整个
逻辑需要注意
[root@YH1 nginx-1.24.0]# cp /usr/src/nginx-
1.24.0/objs/nginx /usr/local/nginx/sbin
#将1.24.0版本的nginx的可执⾏程序复制到1.22.1版本的nginx
的程序⽬录下(2)向进程发信号实现热升级
虽然主程序完成了升级,但此时正在⼯作的 nginx 进程并未完成
升级,依旧是 1.22.1 ⽼版本的 nginx 在运⾏,所以这⾥要向⽼
版本 nginx 进程发送信号。
[root@YH1 nginx-1.24.0]# nginx -v #查询nginx版本号
nginx version: nginx/1.24.0
[root@YH1 ~]# ps aux | grep nginx | grep -v grep
#查询当前运⾏的nginx的pid号
USER PID %CPU %MEM VSZ RSS TTY
STAT START TIME COMMAND
root 7314 0.0 0.0 26660 736 ?
Ss 22:18 0:00 nginx: master process
/usr/local/nginx/sbin/nginx
nginx 7315 0.0 0.6 58824 4868 ? S
22:18 0:00 nginx: worker process
[root@YH1 ~]# kill -USR2 7314 #发送USR2信号:向⽼
版本的主进程发送该信号,nginx会启动新版本的进程与⽼版本进程
⼀起处理请求
[root@YH1 ~]# ps aux | grep nginx | grep -v grep
#看到⽼版本与新版本nginx的主进程和⼯作进程都在启动中
root 7314 0.0 0.3 26660 2672 ?
Ss 22:18 0:00 nginx: master process
/usr/local/nginx/sbin/nginx
11.防盗链
作⽤:防盗链就是防⽌别⼈盗⽤服务器中的图⽚、⽂件、视频等
相关资源。在 nginx 中,通过 location + return 实现。
nginx 7315 0.0 0.6 58824 4868 ? S
22:18 0:00 nginx: worker process #⽼版本
root 10374 0.0 0.7 26672 5996 ? S
22:34 0:00 nginx: master process
/usr/local/nginx/sbin/nginx
nginx 10375 0.0 0.6 58836 5040 ? S
22:34 0:00 nginx: worker process #新版本
[root@YH1 ~]# kill -WINCH 7314 #发送WINCH信号:向
⽼版本主进程发送该信号,它会逐步关闭⾃⼰的⼯作进程(主进程不
会退出),此时的处理请求全部交与新版本的nginx处理
[root@YH1 ~]# kill -QUIT 7314 #发送QUIT信号:向⽼
版本的主进程发送该信号,让旧版本退出
[root@YH1 ~]# ps aux | grep nginx | grep -v grep
#可以看到⽼版本的nginx已经完全退出,热升级完成
root 10374 0.0 0.7 26672 5996 ? S
22:34 0:00 nginx: master process
/usr/local/nginx/sbin/nginx
nginx 10375 0.0 0.6 58836 5040 ? S
22:34 0:00 nginx: worker process1、搭建 nginx 主服务器
实例操作需要两台主机做防盗链操作,所以这⾥我⽤我的两台主
机(server:192.168.33.100)和(YH1:192.168.33.11)做防
盗链演示操作。
下⾯是 server:192.168.33.100 主机的搭建过程,代码及解释
如下:
[root@server ~]# vim
/usr/local/nginx/conf/nginx.conf #去nginx配置⽂件中
找到nginx存放⾸⻚索引⽂件的根⽬录
43 location / {
44 root html; #nginx⾸⻚索引⽂件的
根⽬录,默认在nginx软件⽬录下,也就
是/usr/local/nginx/html
45 index index.html index.htm;
#nginx索引⽂件的格式
46 }
:q #因为配置⽂件未做修改,只是找需要的⽂件位置,所以q退出
即可,⽆需w再保存
[root@server ~]# vim
/usr/local/nginx/html/index.html #按照如下格式写
内容,什么意思不⽤细究
1 <html>2、测试访问主服务器
浏览器输⼊已经搭好的 nginx 主服务器,192.168.33.100
访问成功,如下图:
2 <head>
3 <title>192.168.33.100</title>
4 </head>
5 <body>
6 192.168.33.100 #我的server
主机IP
7 <img src="./123.jpg">
#./123.jpg表示在/usr/local/nginx/html的123.jpg图⽚⽂
件,随便导⼊⼀张图⽚改成123.jpg即可
8 </body>
9 </html>
:wq
[root@server ~]# cd /usr/local/nginx/html/ #到这
个⽬录下
[root@server html]# ls
50x.html 微信截图_20230915204153.jpg index.html
#⼿动拖⼊⼀张jpg格式的图⽚,注意格式
[root@server html]# mv 微信截图_20230915204153.jpg
123.jpg #将图⽚改名为刚才在index.html中提到的123.jpg
[root@server html]# systemctl reload nginx.service
#重载nginx,尽量以后少重启服务,避免到了企业中养成坏习惯
3、搭建模拟“盗图”的服务器
搭建 YH1:192.168.33.11 模拟“盗图”的服务器
此时需要⽤到另⼀台主机,代码及解释如下:4、测试访问“盗图”服务器
浏览器输⼊ IP,如下图:
[root@YH1 ~]# vim /usr/local/nginx/html/index.html
#知道索引⽂件位置,⽆需再去nginx主配置⽂件中查看
1 <html>
2 <head>
3 <title>192.168.33.11</title>
4 </head>
5 <body>
6 192.168.33.11
7 <img
src="http://192.168.33.11/123.jpg">
8 </body>
9 </html>
:wq #保存并退出
[root@server html]# systemctl reload nginx.service
#重载nginx,前提nginx服务已启动
5、为主服务器设置防盗链
修改被盗⽤主机(我的是 server:192.168.33.100)的 nginx 配
置⽂件,代码解释如下:
[root@server ~]# vim
/usr/local/nginx/conf/nginx.conf
35 server {
36 listen 80;
37 server_name localhost;
38
39 #charset koi8-r;40
41 #access_log logs/host.access.log
main;
42
43 location / {
44 root html;
45 index index.html index.htm;
46 }
47 #在第⼀个location下⾯添加⼀个location即可,格式如
下:
48 location ~* \.(mp3|jpg|png|gif|rar)$ {
#匹配以mp3、jpg、png、gif、rar格式的⽂件
49 valid_referers none blocked
192.168.33.100; #valid_referers参数⽤于指定允许访
问图⽚的域名,⽽none表示不允许任何外部域名访问,blocked表
示拒绝所有外部域名访问
50 if ($invalid_referer) {
#在blocked后⾯可以添加指定的域名和IP,表示允许某个IP或域名
的主机访问(本机IP为192.168.33.100)
51 return 403;
52 } #表示如果请求的
referer不在valid_referers参数指定的域名列表中,则返回403
错误
53 }
:wq #保存并退出
[root@server ~]# systemctl reload nginx.service6、清理浏览器缓存,再次访问测试
浏览器输⼊两个⽹站,⼀个被盗图的服务器 IP,⼀个盗图的服
务器 IP。
做出对⽐,可以看到 192.168.33.11 图⽚失效,⽽
192.168.33.100 图⽚可显示。
![](https://i-blog.csdnimg.cn/direct/0ccb370de69f42d9afacbf329b375d2d.png)
12.⽇志分割
随着访问量的增⻓,⽇志⽂件会越来越⼤,时间⻓既会影响访问
的速度,也会占⽤⼀部分磁盘空间,下⾯使⽤脚本⽅式,按天切
割 access 和 error ⽇志。
脚本⽅式:
1. 剪切⽇志后,使⽤ kill -USR1 向 nginx 发送信号重新⽣成⽇志⽂
件,同时还不影响⽹站请求处理进程
2. 错误时通过echo命令将错误显示的同时写⼊到⽇志⽂
件/var/log/messages。
3. 脚本代码及解释如下:
[root@server ~]# vim cut_nginx_logs.sh #新建⼀个脚
本⽂件
#!/bin/bash
#脚本所需环境,shell脚本必备语句
# 获取⽇期
d=$(date +%Y-%m-%d)
# 定义存储⽬录
dir="/usr/local/nginx/logs"
# 定义需要分割的源⽇志
logs_file="/usr/local/nginx/logs/access.log"
logs_error="/usr/local/nginx/logs/error.log"# 定义nginx的pid⽂件
pid_file="/usr/local/nginx/logs/nginx.pid"
#如果$dir定义的变量⽬录不存在,那么执⾏then后⾯的命令
if [ ! -d "$dir" ]
then
mkdir $dir
fi #if条件结束语
# 移动⽇志并重命名⽂件
if [ -f $pid_file ] #如果$pid_file定义的变量⽂件存在
then #那么
mv $logs_file $dir/access_$d.log
mv $logs_error $dir/error_$d.log
kill -USR1 $(cat $pid_file) #发送kill -USR1信
号给Nginx的主进程号,让Nginx重新⽣成⼀个新的⽇志⽂件
find $dir -mtime +30 | xargs rm -f #清理$dir
定义的变量⽇志⽬录下,30天以上的⽂件
else #否则
echo "Error,nginx is not working!" >>
/var/log/messages #将“nginx未运⾏”这条消息追加到系统
内核⽇志/var/log/messages
fi
:wq
[root@server ~]# chmod +x cut_nginx_logs.sh #给脚
本⼀个可执⾏权限
[root@server ~]# crontab -e
13.反向代理及获取客户端 IP 地址
反向代理:反向代理服务器位于⽤户与⽬标服务器之间,但是对
于⽤户⽽⾔,反向代理服务器就相当于⽬标服务器,即⽤户直接
访问反向代理服务器就可以获得⽬标服务器的资源。
获取客户端 IP:经过反向代理后,由于在客户端和 web 后端服
务器之间增加了中间层(反向代理服务器),因此 web 后端服
务器⽆法直接拿到客户端的 IP,⽽通过请求获取 IP 等操作后,
即可从后端服务器获得客户端真实 IP。
1、 搭建反向代理服务器
(1)⽤两台主机进⾏反向代理及测试。
⼀台反向代理服务器 server:192.168.33.100,⼀台后端服务器
YH1:192.168.33.11
0 0 * * * /usr/bin/bash /root/cut_nginx_logs.sh
#在每天的0点执⾏该脚本
[root@server ~]# ./cut_nginx_log.sh #启动脚本
[root@server ~]# cd /usr/local/nginx/logs/ #查看
⽇志是否以时间分隔
[root@server logs]# ls
access_2023-09-15.log error_2023-09-15.log
nginx.pid
access.log error.log(2)将 YH1 的 nginx ⾸⻚索引⽂件修改为如下内容:
[root@YH1 ~]# vim /usr/local/nginx/html/index.html
192.168.33.11 这⾥是真实的后端服务器YH1
:wq
(3)配置反向代理服务器 server:192.168.33.100 的 nginx 主配置
⽂件,代码及解释如下:
[root@server ~]# vim
/usr/local/nginx/conf/nginx.conf
......省略部分配置信息.......
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://192.168.33.11:80;
#在location内添加该条参数,表示为将33.100所收到的访问请求
代理转发到33.11的80端⼝
}
:wq
[root@server ~]# systemctl reload nginx.service
(4)当浏览器访问代理服务器 server:192.168.33.100 时,其实就
访问到了后端服务器 YH1:192.168.33.11 上,如下图:
2、搭建可获取客户端 IP 的服务器
(1)配置代理服务器 server:192.168.33.100 的 nginx 主配置,使
后端服务器 YH1:192.168.33.11 可获取客户端真实 IP。
proxy_pass http://192.168.33.11:80;
#在location内添加该条参数,表示为将33.100所收到的访问请求
代理转发到33.11的80端⼝
}
:wq
[root@server ~]# systemctl reload nginx.service[root@server ~]# vim
/usr/local/nginx/conf/nginx.conf
......省略部分配置信息......
location / {
root html;
index index.html index.htm;
proxy_pass http://192.168.33.11:80;
proxy_set_header X-Real-IP
$remote_addr; #代理服务器为后端服务器请求客户端真实IP
地址
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
}
:wq
[root@server ~]# systemctl reload nginx.service
(2)配置后端服务器 YH1:192.168.33.11 的 nginx 主配置
14.Nginx 的 web 缓存服务
Nginx 作为 Web 缓存服务器,它介于客户端和应⽤服务器之
间,当⽤户通过浏览器访问⼀个 URL 时,Web 缓存服务器会去
应⽤服务器获取要展示给⽤户的内容,将内容缓存到⾃⼰的服务
器上,当下⼀次请求到来时,如果访问的是同⼀个 URL,Web
缓存服务器就会直接将之前缓存的内容返回给客户端,⽽不是向
应⽤服务器再次发送请求。
Web 缓存降低了应⽤服务器、数据库的负载,减少了⽹络延
迟,提⾼了⽤户访问的响应速度,增强了⽤户的体验。
[root@YH1 ~]# vim /usr/local/nginx/conf/nginx.conf
......省略部分配置信息......
location / {
root html;
index index.html index.htm;
set_real_ip_from 192.168.33.100; #获
取客户端的真实IP来源于192.168.33.100,也可写⼀个IP范围
real_ip_header X-Forwarded-For; #⽤
于设置使⽤哪个头来替换IP地址
real_ip_recursive on; #是
否递归解析
}
:wq
[root@YH1 ~]# systemctl reload nginx.service(1)搭建反向代理缓存服务器 server:192.168.33.100
[root@server ~]# vim
/usr/local/nginx/conf/nginx.conf
......省略部分配置信息......
http {
include mime.types;
default_type application/octet-stream;
proxy_cache_path /usr/local/proxy_temp
levels=1:2 keys_zone=cache_item:200m inactive=1d
max_size=20g;
#指定缓冲路径,指定缓存⽬录级别⼆层,指定缓存块名称和⼤
⼩,指定缓存数据存储时间,指定占⽤硬盘最⼤值20g
......省略部分配置信息......
location / {
proxy_cache cache item; #指定缓
冲区
proxy_cache_key
$schemesproxy_hostsrequest_uri; #指定参数key值
proxy_cache_valid 200 5d; #返回值
200缓存5天
proxy_cache_valid 404 30s; #返回值
404缓存30秒
proxy_cache_valid any 1m; #其他的
⼀分钟(2)当反向代理缓存服务器搭建完成后,即使nginx服务关闭也可
以访问⾸⻚。
15.nginx 的 https 协议
1、⽣成密钥⽂件
proxy_pass http://192.168.33.11;
#反向代理,将请求转发给后端web服务器
}
......省略部分配置信息......
:wq
[root@server ~]# systemctl reload nginx.service