正则表达式
代码 | 说明 |
---|---|
^ | 匹配搜索字符串开始位置 |
$ | 匹配搜索字符串结束位置 |
. | 匹配换行符\n之外的任何单个字符 |
\ | 转义字符,将下一个字符标记为特殊字符 |
[xyz] | 字符集,与任意一个指定字符匹配 |
[a-z] | 字符范围,匹配置顶范围内的任意字符 |
\w | 与以下任意字符匹配A- Z,a-z,0-9和下划线,等效于A-Za-z0-9 |
\d | 数字字符匹配,等效于[0-9] |
{n} | 正好匹配n次 |
{n,} | 至少匹配n次 |
{n,m} | 匹配至少n次,最多m次 |
* | 零次或多次,等效于{0,} |
+ | 一次或多次,等效于{1,} |
? | 零次或一次,等效于{0,1} |
测试:
server {
listen 80;
server_name ~^www\.\w+\.com;
}
# 以www开头,com结尾,中间随便什么字符都可以
添加一个括号来进行测试
server {
listen 80;
server_name ~^www\.(\w+)\.com;
}
location / {
default_type text/plain;
return 200 '=====>access====>$1';
}
# 其中 "\w+"代表1次或多次,用$1来获取第1个括号里的值;
server_name匹配执行顺序
default_type text/plain;
server {
listen 80;
server_name ~^www\.(\w+)\.com;
return 200 'regex_success';
}
server {
listen 80;
server_name www.itheima.*;
return 200 'wildcard_after_sucess';
}
server{
listen 80;
server_name *.itheima.com;
return 200 'wildcard_before_sucesss';
}
server {
listen 80;
server_name www.itheima.com;
return 200 'exact_success';
}
server {
listen 80;
server_name _;
return 444 'default_server not found server';
}
结论:
1.exact #精准匹配
2.wilcard_before #通配符在前
3.wilcard_after #通配符在后
4.regex #正则表达式
5.not found server#默认的,如果没有指定就找第一个server
localtion指令
用来设置请求的URL
语法 | 六种:location [ = | ~ | ~* | ^* | ^~ | @ ] URL{…} |
---|---|
默认值 | — |
位置 | server,location |
测试1:不带任何符号
server {
listen 80;
server_name localhost;
location /abc
default_type text/plain;
retunrn 200 "success"
}
# 只要开头是为abc的都能访问,例如localhost/abcefg
测试2:加个=,变成精准匹配
server {
listen 80;
server_name localhost;
location =/abc
default_type text/plain;
retunrn 200 "success"
}
可以匹配到:
localhost/adb
localhost/abc?p1=TOM
匹配不到:
localhost/abc/
localhost/abcdef
测试3:
~:表示当前包含了正则表达式,并且区分大小写
~*:表示当前包含正则表达式,并且不区分大小写
server {
listen 80;
server_name localhost;
location ~^/abc\w$
default_type text/plain;
retunrn 200 "success"
}
# /abc开头,任何字符结尾,都可以访问到,但总共只有4个字符
测试4:加^~和不加的区别
server {
listen 80;
server_name localhost;
location /abcd {
default_type text/plain;
return 200 "xxx";
}
location ~*^/abc\w$ {
default_type text/plain;
return 200 "success";
}
}
问:当我访问localhost/abcd,两个条件都满足,会访问到哪个?
答案是第二个。但加了^~就会匹配到第一个。
原因是,不加符号会先s就不会了!
设置请求资源的目录root / alias的指令
区别
举例说明
在C盘目录创建一个images目录,并放入一张图片
location /images{
root html;
}
#可以看到图片
location /images{
alias html/images;
}
#404
原因:
root的处理方式是:root路径+location路径
alias的处理结果是:使用alias替换location路径
如果location路径是以/结尾,则alias也必须以/结尾,root没有要求
index指令
作用:设置网站默认首页
语法 | index file; |
---|---|
默认值 | index index.html |
位置 | http、server、location |
index 后面可以跟多个设置,如果访问的时候没有指定具体资源,则会依次查找,直到找到第一个为止。
error_page指令
作用:设置网站的错误页面
语法 | error_page code |
---|---|
默认值 | – |
位置 | http、server、location |
当出现对应的响应code后,如何来处理。
举例:
(1)可以指定具体跳转的地址
server {
error_page 404 https://zhifeng-like-dufen.xyz
}
(2)可以指定重定向地址
server {
error_page 500 502 503 504 404 /50.html;
location =/50.html{
root html;
}
}
(3)使用location的@符合完成错误信息展示
server {
error_page 404 @jump_to_error;
location @jump_to-err0r {
default_tpye text/plain;
return 404 'Not found Page...'
}
}
# 访问不存在的页面会提示
(4)可选性=[response]的作用来将相应代码更改为另一个
server {
error_page 404 =200 /50x.html;
location =/50x.html{
root html;
}
}
# 这样的话,当返回404找不到对应资源的时候,在浏览器上可以看到,返回状态是200,
# 这块需要注意下,编写error_page后面的内容,404需要加空格,200前不能加空格。