[root@localhost nginx-1.21.3]# objs/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 nginx-1.21.3]# objs/nginx -s reload
[root@localhost ~]# curl http://192.168.129.33/test
test
[root@localhost nginx-1.21.3]# \cp objs/nginx /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.21.3]# ll objs/nginx /usr/local/nginx/sbin/nginx
-rwxr-xr-x. 1 root root 7069888 10月 27 23:55 objs/nginx
-rwxr-xr-x. 1 root root 7069888 10月 28 00:12 /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.21.3]# objs/nginx -s stop;nginx
[root@localhost nginx-1.21.3]# ps -ef | grep nginx
root 48446 1 0 00:50 ? 00:00:00 nginx: master process nginx
nginx 48447 48446 0 00:50 ? 00:00:00 nginx: worker process
root 48938 1494 0 00:50 pts/0 00:00:00 grep --color=auto nginx
location区段,通过指定模式来与客户端请求的URI相匹配
//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能
//语法:location [ 修饰符 ] pattern {…}
常用修饰符说明:
| 修饰符 | 功能 |
| :-: | :-- |
| = | 精确匹配 |
| ~ | 正则表达式模式匹配,区分大小写 |
| ~* | 正则表达式模式匹配,不区分大小写 |
| ^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
| @ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
没有修饰符表示必须以指定模式开始,如:
[root@localhost local]# vim nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
}
location /test {
echo “test”;
}
[root@localhost ~]# nginx -s reload
那么如下内容就可正确匹配:
[root@localhost ~]# curl http://192.168.129.33/test
test
[root@localhost ~]# curl http://192.168.129.33/test/
test
[root@localhost ~]# curl http://192.168.129.33/test?test
test
=:表示必须与指定的模式精确匹配,如:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
…
location / {
root html;
index index.html index.htm;
}
location /test { #匹配/test下的所有
echo “test”;
}
location =/test {
echo “111”;
}
[root@localhost ~]# nginx -s reload
那么如下内容就可正确匹配:
[root@localhost ~]# curl http://192.168.129.33/test
111
如下内容则无法匹配:
[root@localhost ~]# curl http://192.168.129.33/test/
test
[root@localhost ~]# curl http://192.168.129.33/test/hh
test
[root@localhost ~]# curl http://192.168.129.33/testtest
test
~:表示指定的正则表达式要区分大小写,如:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
…
location / {
root html;
index index.html index.htm;
}
location /test {
echo “test”;
}
location ~ ^/test$ {
echo “大小写”;
}
[root@localhost ~]# nginx -s reload
那么如下内容就可正确匹配:
[root@localhost ~]# curl http://192.168.129.33/test
大小写
如下内容则无法匹配:
[root@localhost ~]# curl http://192.168.129.33/test/
test
[root@localhost ~]# curl http://192.168.129.33/testkllk
test
~*:表示指定的正则表达式不区分大小写,如:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
…
location ~ ^/test$ {
echo “大小写”;
}
[root@localhost ~]# nginx -s reload
那么如下内容就可正确匹配:
[root@localhost ~]# curl http://192.168.129.33/test
不分大小写
[root@localhost ~]# curl http://192.168.129.33/TEST
不分大小写
[root@localhost ~]# curl http://192.168.129.33/TEst
不分大小写
如下内容则无法匹配:
[root@localhost ~]# curl http://192.168.129.33/TEst/
404 Not Found
[root@localhost ~]# curl http://192.168.129.33/testas
404 Not Found
~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
…
location / {
root html;
index index.html index.htm;
}
location /test {
echo “无”;
}
location ~ ^/test$ {
echo “分大小写”;
}
location ~* ^/test$ {
echo “不分大小写”;
}
[root@localhost ~]# nginx -s reload
那么如下内容就可正确匹配:
[root@localhost ~]# curl http://192.168.129.33/test
分大小写
[root@localhost ~]# curl http://192.168.129.33/tesT
不分大小写
[root@localhost ~]# curl http://192.168.129.33/test/
无
[root@localhost ~]# curl http://192.168.129.33/test/asda
无
[root@localhost ~]# curl http://192.168.129.33/testasda
无
查找顺序和优先级:由高到底依次为
-
带有
=
的精确匹配优先 -
正则表达式按照他们在配置文件中定义的顺序
-
带有
^~
修饰符的,开头匹配 -
带有
~
或~*
修饰符的,如果正则表达式与URI匹配 -
没有修饰符的精确匹配
优先级次序如下:
( location = 路径 ) --> ( locat
ion ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( locatio
用于location段
allow:设定允许哪台或哪些主机访问,多个参数间则换行
deny:设定禁止哪台或哪些主机访问,多个参数间则换行
示例:
allow 192.168.1.1/32 ;
allow 192.168.2.1/32 ;
deny all;
示例:
[root@localhost ~]# mkdir /usr/local/nginx/html/test -p
[root@localhost ~]# cat > /usr/local/nginx/html/test/index.html >>EOF
EOF
[root@localhost ~]# nginx -s reload
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
…
location / {
root html;
index index.html index.htm;
}
location /test {
deny 192.168.129.1; ## 黑名单(除了自己谁都能访问)
root html;
index index.html;
}
…
[root@localhost ~]# curl http://192.168.129.33/test/index.html
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
…
location / {
root html;
index index.html index.htm;
}
location /test {
allow 192.168.129.1; #白名单(除了自己谁都不能访问)
deny all;
root html;
index index.html;
}
…
[root@localhost ~]# curl http://192.168.129.33/test/index.html