文章目录
nginx应用
1. 平滑升级nginx并添加echo模块
1.1 获取模块
//获取nginx的echo模块
[root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/refs/heads/master.zip
[root@localhost ~]# unzip master.zip
[root@localhost ~]# ls
anaconda-ks.cfg echo-nginx-module-master master.zip
1.2 更新nginx
1.2.1 检查nginx版本并获取最新版的二进制包
[root@localhost ~]# nginx -v
nginx version: nginx/1.22.1
[root@localhost ~]# wget https://nginx.org/download/nginx-1.24.0.tar.gz
1.2.2 获取旧版本nginx编译参数,并编译
[root@localhost ~]# nginx -V
nginx version: nginx/1.22.1
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
//增加echo模块的编译参数
[root@localhost nginx-1.24.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=/root/echo-nginx-module-master/
[root@localhost nginx-1.24.0]# echo $?
0
1.2.3 备份原来的nginx并一条命令重启nginx
[root@localhost ~]# cp /usr/local/nginx/sbin/nginx /opt/nginx-20231019
[root@localhost ~]# cd nginx-1.24.0
[root@localhost nginx-1.24.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@localhost nginx-1.24.0]# make
[root@localhost nginx-1.24.0]# cd objs/
[root@localhost objs]# ls
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
[root@localhost objs]# systemctl stop nginx;\cp nginx /usr/local/nginx/sbin/;systemctl start nginx
2. location区段配置
location区段,通过指定模式来与客户端请求的URI相匹配
//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能
//语法:location [ 修饰符 ] pattern {......}
常用修饰符说明:
修饰符 | 功能 |
---|---|
= | 精确匹配 |
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始, 不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问, 如try_files或error_page等 |
//=精准匹配的优先级是最高的,和其他的修饰符在一起的时候=优先级最高,什么都没有则优先级最低,且等于号是精确匹配一字不差才行
location /123 {
echo "/123";
}
location =/123 {
echo "=/123";
}
location /abcd {
echo "/abcd";
}
location =/abcd {
echo "=/abcd";
}
location ~ /abcd { //区分大小写
echo "~/abcd";
}
location ~* /abcd { //不区分大小写
echo "~*/abcd";
}
优先级次序如下:
( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )
3. rewrite重定向
语法:rewrite regex replacement flag;
,如:
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
此处的$1用于引用(.*.jpg)匹配到的内容,又如:
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;
示例
上传一张图片到html下的plpl文件夹下
[root@localhost zxc]# pwd
/usr/local/nginx/html/zxc
[root@localhost zxc]# ls
1.jpg
//修改配置文件
location /imgs {
rewrite ^/imgs/(.*\.jpg)$ /zxc/$1 break;
}
增加这一项
//重启nginx
[root@localhost conf]# systemctl restart nginx
可以访问
也可以不使用本地路径改用url
location /imgs {
rewrite ^/imgs/(.*\.jpg)$ http://www.86ps.com/PSSC/zrqx/4190.html redirect;
}