nginx平滑升级与location修饰符
一、nginx平滑升级
1、部署nginx
//创建系统用户nginx
[root@10 ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@10 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@10 ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@10 ~]# mkdir -p /var/log/nginx
[root@10 ~]# chown -R nginx.nginx /var/log/nginx
[root@10 ~]#
//下载nginx
[root@10 ~]# cd /usr/src/
[root@10 src]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
--2022-10-10 14:15:30-- http://nginx.org/download/nginx-1.22.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073322 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.22.0.tar.gz’
nginx-1.22.0.tar.gz 100%[====================================================================================>] 1.02M 301KB/s in 3.5s
2022-10-10 14:15:34 (301 KB/s) - ‘nginx-1.22.0.tar.gz’ saved [1073322/1073322]
[root@10 src]#
//编译安装
[root@10 src]# ls
debug kernels nginx-1.22.0.tar.gz
[root@10 src]# tar xf nginx-1.22.0.tar.gz
[root@10 src]# cd nginx-1.22.0
[root@10 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 \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@10 nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//配置环境变量
[root@10 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@10 ~]# . /etc/profile.d/nginx.sh
[root@10 ~]#
//服务控制方式,使用nginx命令
-t //检查配置文件语法
-v //输出nginx的版本
-c //指定配置文件的路径
-s //发送服务控制信号,可选值有{stop|quit|reopen|reload}
//启动nginx
[root@10 ~]# nginx
[root@10 ~]# 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 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@10 ~]#
2、获取之前安装nginx的编译参数
[root@10 ~]# nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (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-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 --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@10 ~]#
3、下载新模块
[root@10 ~]# wget https://github.com/openresty/echo-nginx-module
[root@10 ~]# ls
anaconda-ks.cfg echo-nginx-module-master.zip
4、重新编译软件
//安装unzip工具
[root@10 ~]# yum -y install unzip
//解压新的模块包
[root@10 ~]# unzip echo-nginx-module-master.zip
//再次解压nginx源码包
[root@10 ~]# ls
anaconda-ks.cfg echo-nginx-module-master echo-nginx-module-master.zip nginx-1.22.0.tar.gz
[root@10 ~]# tar -zxf nginx-1.22.0.tar.gz
//添加新的模块进行编译安装
[root@10 ~]# cd nginx-1.22.0
[root@10 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 \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--add-module=../echo-nginx-module-master
//查看objs目录下没有nginx程序
[root@10 nginx-1.22.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@10 nginx-1.22.0]# ls objs/
addon autoconf.err Makefile ngx_auto_config.h ngx_auto_headers.h ngx_modules.c src
//使用make编译
[root@10 nginx-1.22.0]# make
//编译完成之后再次进行查看objs目录下是否有nginx程序
[root@10 nginx-1.22.0]# ls objs/
addon autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
[root@10 nginx-1.22.0]#
5、备份源程序并停止、覆盖、启动服务
//先查看升级前和升级后的版本区别,主要看编译参数
//升级前
[root@10 nginx-1.22.0]# nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (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-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 --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@10 nginx-1.22.0]#
//升级后
[root@10 nginx-1.22.0]# objs/nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (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-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 --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../echo-nginx-module-master
//停止服务
[root@10 nginx-1.22.0]# nginx -s stop
//备份
[root@10 nginx-1.22.0]# cp /usr/local/nginx/sbin/nginx /opt/
//覆盖
[root@10 nginx-1.22.0]# cp objs/nginx /usr/local/nginx/sbin/
cp: overwrite '/usr/local/nginx/sbin/nginx'? y
//启动服务
[root@10 nginx-1.22.0]# /usr/local/nginx/sbin/nginx //前面配置了环境变量,此处能直接nginx启动服务
//查看端口
[root@10 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 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@10 nginx-1.22.0]#
//查看nginx信息
[root@10 nginx-1.22.0]# nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (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-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 --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../echo-nginx-module-master
[root@10 nginx-1.22.0]#
6、测试–引用echo模块
[root@10 ~]# vim /usr/local/nginx/conf/nginx.conf
//修改location参数内容就行
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
echo "mike";
}
//使用nginx -t 检查配置文件内容
[root@10 ~]# 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
//重载nginx
[root@10 ~]# nginx -s reload
使用浏览器访问,发现浏览器无法支持echo模块,会当成文件进行下载
使用命令进行访问
二、location案例
location区段,通过指定模式来与客户端请求的URI相匹配 功能: 允许根据用户请求的URI来匹配定义的各个location,匹配时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能 语法: location [修饰符] pattern {…} 修饰符 = 精确匹配 ~ 正则表达式模式匹配,区分大小写 ~* 正则表达式模式匹配,不区分大小写 ^~ 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 @ 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或者error_page等
//增加一个location字段
[root@10 ~]# cd /usr/local/nginx/conf/
[root@10 conf]# vim nginx.conf
location / {
echo "jan";
}
[root@10 conf]# nginx -s reload
//用“=”精准匹配
[root@10 conf]# vim nginx.conf
location = /147 {
echo "jan";
}
[root@10 conf]# nginx -s reload
// ~:表示指定的正则表达式要区分大小写
[root@10 conf]# vim nginx.conf
location ~ /666$ {
echo "jan";
}
[root@10 conf]# nginx -s reload
//当“=”和“~”同时存在时,此时时 = 优先于 ~
[root@10 conf]# vim nginx.conf
location ~ /666$ {
echo "jan";
}
location = /666 {
echo "mike";
}
[root@10 conf]# nginx -s reload
// ~*:表示指定的正则表达式不区分大小写:
[root@10 conf]# vim nginx.conf
location ~* ^/666abc$ {
echo "jan";
}
[root@10 conf]# nginx -s reload
查找顺序和优先级:由高到低 1、带有“=”的精确匹配优先 2、正则表达式按照他们在配置文件中定义的顺序 3、带有“^~”修饰符的,开头匹配 4、带有或者*修饰符的,如果正则表达式与URI匹配 5、没有修饰符的精确匹配
location = 路径 location ^~ 路径 location ~ 正则 location ~* 正则 location 路径
|