nginx增加echo模块
//下载nginx echo模块
[root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.18.0.tar.gz v0.61.tar.gz
nginx-1.18.0 original-ks.cfg
[root@localhost ~]# tar xf v0.61.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.18.0 original-ks.cfg
echo-nginx-module-0.61 nginx-1.18.0.tar.gz v0.61.tar.gz
[root@localhost ~]#
//升级nginx
[root@localhost ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
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@localhost ~]# cd nginx-1.18.0
[root@localhost nginx-1.18.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-0.61/ //要先进入模块的目录
[root@localhost nginx-1.18.0]# make //直接make就好了
//备份原来的编译文件以防意外
[root@localhost objs]# cp /usr/local/nginx/sbin/nginx /opt/
//把编译好的文件把原来的覆盖
[root@localhost objs]# cp nginx /usr/local/nginx/sbin/ //nginx没有使用时
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y
[root@localhost objs]# nginx -s stop;cp nginx /usr/local/nginx/sbin/;nginx //nginx在使用时
//没有修饰符
[root@localhost objs]# vim /usr/local/nginx/conf/nginx.conf
location /abc {
echo 'abc';
}
[root@localhost objs]# nginx -s reload
常用修饰符说明
修饰符 | 功能 |
---|---|
= | 精确匹配 |
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
= | 精确匹配 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
没有修饰符,只要开头是abc,后面不管接什么,都可以匹配到
//=精确匹配
[root@localhost objs]# vim /usr/local/nginx/conf/nginx.conf
location =/abc {
echo 'abc';
}
[root@localhost objs]# nginx -s reload
能匹配到
http://192.168.20.135/abc
http://192.168.20.135/abc?p1=1111
不能匹配到
http://192.168.20.135/abc/
精确匹配开头结尾定死了(abc),只能是这个,但可以接参数
// ~ 表示指定的正则表达式要区分大小写
location =/abc {
echo 'abc';
}
location ~ /abc {
echo 'abc';
}
可以看出精确匹配比~的要高
//把精确匹配删了在测试
location ~ /abc {
echo 'abc';
}
// ~*表示不区分大小写正则表达式
location ~*/abc {
echo 'ABC';
}
location ~/abc {
echo 'abc';
}
能匹配到
http://192.168.20.135/abc
http://192.168.20.135/abcalja
不能匹配到
http://192.168.20.135/abc1
http://192.168.20.135/abc/
http://192.168.2./135/abc/abc
http://192.168.20.135/ABC
因为优先级相同,所以会匹配到写在前面的
location ~/abc {
echo 'abc';
}
location ~*/abc {
echo 'ABC';
}
//只匹配/abc
location ~^/abc$ {
echo 'abc';
}