nginx升级
环境说明
系统 | 主机名 | ip |
---|---|---|
centos7 | localhost | 192.168.152.136 |
1.把nginx高版本的压缩包拷到服务器上,解压到/usr/local/目录下
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.18.0.tar.gz
nginx-1.14.2.tar.gz
[root@localhost ~]# tar -xf nginx-1.18.0.tar.gz -C /usr/local
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin games lib libexec nginx-1.18.0 share
etc include lib64 nginx sbin src
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.18.0]# make
2.把之前的nginx文件备份
进入/usr/local/nginx/sbin/目录
[root@localhost ~]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# mv nginx nginx-old-copy
[root@localhost sbin]# ls
nginx-old-copy
//然后返回nginx-1.14.2下的objs目录,把新的nginx拷贝过来
[root@localhost ~]# mv /usr/local/nginx-1.18.0/objs/nginx /usr/local/nginx/sbin/
[root@localhost ~]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls
nginx nginx-old-copy
3.到/usr/local/nginx/sbin下查看nginx版本,检出配置文件,启动nginx
[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.18.0
[root@localhost sbin]# ./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@localhost sbin]# ./nginx
[root@localhost sbin]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@localhost sbin]# nginx -v
nginx version: nginx/1.18.0
nginx location匹配规则
语法规则
官方文档
常用修饰符说明:
修饰符 | 功能 |
---|---|
= | 精确匹配 |
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
没有修饰符表示必须以指定模式开始,如:
server {
server_name www.idfsoft.com;
location /abc {
......
}
}
那么如下内容就可正确匹配:
http://www.idfsoft.com/abc
http://www.idfsoft.com/abc?p1=11&p2=22
http://www.idfsoft.com/abc/
=:表示必须与指定的模式精确匹配,如:
server {
server_name www.idfsoft.com;
location = /abc {
......
}
}
那么如下内容就可正确匹配:
- 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
~:表示指定的正则表达式要区分大小写,如:
server {
server_name www.idfsoft.com;
location ~ ^/abc$ {
......
}
}
那么如下内容就可正确匹配:
- 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
~*:表示指定的正则表达式不区分大小写,如:
server {
server_name www.idfsoft.com;
location ~* ^/abc$ {
......
}
}
那么如下内容就可正确匹配:
- 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匹配
- 没有修饰符的精确匹配
匹配实例
安装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
nginx-1.14.2.tar.gz v0.61.tar.gz
[root@localhost ~]# tar xf v0.61.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.18.0.tar.gz
echo-nginx-module-0.61 v0.61.tar.gz
nginx-1.14.2.tar.gz
//编译
[root@localhost ~]# cd /usr/local
[root@localhost local]# 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=/root/echo-nginx-module-0.61
[root@localhost nginx-1.18.0]# make
[root@localhost nginx-1.18.0]# cd objs/
[root@localhost objs]# ls
addon nginx ngx_auto_headers.h src
autoconf.err nginx.8 ngx_modules.c
Makefile ngx_auto_config.h ngx_modules.o
//备份正在在使用的nginx
[root@localhost objs]# cp /usr/local/nginx/sbin/nginx /opt/
//替换原来的nginx
[root@localhost objs]# nginx -s stop;cp nginx /usr/local/nginx/sbin/;nginx
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y
无修饰符
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
//任何未匹配到其它location的请求都会匹配到
location /abc {
echo "无修饰符";
}
}
}
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl http://192.168.152.136/abc
无修饰符
[root@localhost ~]# curl http://192.168.152.136/ABc
无修饰符
[root@localhost ~]# curl http://192.168.152.136/ABc/
无修饰符
= 对普通字符精确匹配
location = /uri = 开头表示精确匹配,只有完全匹配上才能生效。
location /abc {
echo "无修饰符";
}
location = /abc {
echo "精确匹配";
}
}
[root@localhost ~]# curl http://192.168.152.136/abc
精确匹配
[root@localhost ~]# curl http://192.168.152.136/abc/
无修饰符
[root@localhost ~]# curl http://192.168.152.136/abc/abcc
无修饰符
[root@localhost ~]# curl http://192.168.152.136/abc?a=1\$b=2
精确匹配
^~ 匹配字符串开头
location ^~ /abc {
echo "匹配开头";
}
}
[root@localhost ~]# curl http://192.168.152.136/abcdee
匹配开头
[root@localhost ~]# curl http://192.168.152.136/abc
匹配开头
[root@localhost ~]# curl http://192.168.152.136/abc/
匹配开头
~ 区分大小写的匹配
location ~ /abc/ {
echo "区分大小写的匹配";
}
}
[root@localhost ~]# curl http://192.168.152.136/ABC/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
[root@localhost ~]# curl http://192.168.152.136/abc/
区分大小写的匹配
[root@localhost ~]# curl http://192.168.152.136/abc/ABC
区分大小写的匹配
~* 不区分大小写的匹配
location ~* ^/abc$ {
echo "不区分大小写的匹配";
}
[root@localhost ~]# curl http://192.168.152.136/abc
不区分大小写的匹配
[root@localhost ~]# curl http://192.168.152.136/ABC
不区分大小写的匹配
[root@localhost ~]# curl http://192.168.152.136/abcdee
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
访问控制
用于location段
allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开
deny:设定禁止哪台或哪些主机访问,多个参数间用空格隔开
示例:
allow 192.168.1.1/32 172.16.0.0/16;
deny all;