文章目录
nginx平滑升级
进入这个网站:https://search.gitee.com/?skin=rec&type=repository&q=echo
[root@nginx ~]# dnf -y install git
Last metadata expiration check: 0:52:00 ago on Thu 13 Oct 2022 12:44:16 AM CST.
Package git-2.27.0-1.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@nginx ~]# git clone https://gitee.com/wujunze/nginx_module_echo.git
[root@nginx ~]# ls
anaconda-ks.cfg nginx-1.20.2.tar.gz nginx_module_echo //必须在同级目录
nginx-1.20.2 nginx-1.22.0.tar.gz
[root@nginx ~]#
解压,必须是同级目录下
[root@nginx ~]# tar xf nginx-1.22.0.tar.gz
[root@nginx ~]# ls
anaconda-ks.cfg nginx-1.20.2.tar.gz nginx-1.22.0.tar.gz
nginx-1.20.2 nginx-1.22.0 nginx_module_echo
预编译
[root@nginx ~]# cd nginx-1.22.0/
[root@nginx nginx-1.22.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@nginx nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=../nginx_module_echo/
[root@nginx nginx-1.22.0]# make -j 4
查看版本
[root@nginx nginx-1.22.0]# ls objs/
addon nginx ngx_auto_headers.h src
autoconf.err nginx.8 ngx_modules.c
Makefile ngx_auto_config.h ngx_modules.o
[root@nginx nginx-1.22.0]# objs/nginx -v
nginx version: nginx/1.22.0 //新编译的
[root@nginx nginx-1.22.0]# nginx -v
nginx version: nginx/1.20.2 //目前的
[root@nginx nginx-1.22.0]#
备份老版本程序
[root@nginx nginx-1.22.0]# mv /usr/local/nginx/sbin/nginx{,-bak}
[root@nginx nginx-1.22.0]# ls /usr/local/nginx/sbin/
nginx-bak
[root@nginx nginx-1.22.0]#
暂停老版本程序的进程
将新版本程序复制到老版本所在位置替换掉老版本
[root@nginx nginx-1.22.0]# pkill nginx
[root@nginx nginx-1.22.0]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@nginx nginx-1.22.0]# cp objs/nginx /usr/local/nginx/sbin/
[root@nginx nginx-1.22.0]# nginx -v
nginx version: nginx/1.22.0
[root@nginx nginx-1.22.0]#
启动新版本
[root@nginx nginx-1.22.0]# systemctl restart nginx.service
[root@nginx nginx-1.22.0]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 192.168.64.129:8081 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@nginx nginx-1.22.0]#
也可以一天命令解决
[root@nginx nginx-1.22.0]# cp /usr/local/nginx/sbin/nginx{,-bak};pkill nginx ;\cp objs/nginx /usr/local/nginx/sbin/nginx;systemctl start nginx
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
echo "hello world";
}
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# systemctl reload nginx.service
[root@nginx ~]#
location实战
location区段,通过指定模式来与客户端请求的URI相匹配
//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能
//语法:location [ 修饰符 ] pattern {…}
常用修饰符说明:
修饰符 | 功能 |
---|---|
= | 精确匹配 |
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
‘=’ 精确匹配
location / {
echo "hello world";
}
location = / {
echo "haha";
}
[root@nginx ~]# systemctl reload nginx.service
The “” request will match configuration A, the “” request will match configuration B, the “” request will match configuration C, the “” request will match configuration D, and the “” request will match configuration E.
//index.html/documents/document.html/images/1.gif/documents/1.jpg
location = / {
echo "[ configuration A ]";
}
location / {
echo "[ configuration B ]";
}
location /documents/ {
echo "[ configuration C ]";
}
location ^~ /images/ {
echo "[ configuration D ]";
}
location ~* \.(gif|jpg|jpeg)$ {
echo "[ configuration E ]";
}
[root@nginx ~]# systemctl reload nginx.service
另一种方法
location /abc {
echo "hello world";
}
[root@nginx ~]# !system
systemctl reload nginx.service
=:表示必须与指定的模式精确匹配,如:
location = /abc {
echo "hello world";
}
location / {
echo "haha";
}
[root@nginx ~]# !system
systemctl reload nginx.service
那么如下内容就可正确匹配:
http://www.idfsoft.com/abc
http://www.idfsoft.com/abc?p1=11&p2=22
如下内容则无法匹配:
http://www.idfsoft.com/abc/
http://www.idfsoft.com/abc/abcde
~:表示指定的正则表达式要区分大小写,如:
location /abc {
echo "hello world";
}
location ~ ^/abc$ {
echo "wawa";
}
[root@nginx ~]# systemctl reload nginx.service
那么如下内容就可正确匹配:
http://www.idfsoft.com/abc
http://www.idfsoft.com/abc?p1=11&p2=22
如下内容则无法匹配:
http://www.idfsoft.com/abc/
http://www.idfsoft.com/ABC
http://www.idfsoft.com/abcde
~*:表示指定的正则表达式不区分大小写,如:
location ~* ^/abc$ {
echo "wawa";
}
[root@nginx ~]# systemctl reload nginx.service
那么如下内容就可正确匹配:
http://www.idfsoft.com/abc
http://www.idfsoft.com/abc?p1=11&p2=22
http://www.idfsoft.com/ABC
如下内容则无法匹配:
http://www.idfsoft.com/abc/
http://www.idfsoft.com/abcde
~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式
查找顺序和优先级:由高到底依次为
带有=的精确匹配优先
正则表达式按照他们在配置文件中定义的顺序
带有^~修饰符的,开头匹配
带有或*修饰符的,如果正则表达式与URI匹配
没有修饰符的精确匹配
优先级次序如下:
( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )