临沂网站建设公司排名,品牌设计机构公司,郑州网页网站制作,php网站开发实验报告[正则表达式]
常用的[Nginx] 正则表达式 $ #xff1a;匹配输入字符串的结束位置* #xff1a;匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll” #xff1a;匹配前面的字符一次或多次。如“ol”能匹配“ol”及“oll”、“olll”#xff0c;但不能匹配“…[正则表达式]
常用的[Nginx] 正则表达式 $ 匹配输入字符串的结束位置* 匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll” 匹配前面的字符一次或多次。如“ol”能匹配“ol”及“oll”、“olll”但不能匹配“o”? 匹配前面的字符零次或一次例如“do(es)?”能匹配“do”或者“does””?”等效于”{0,1}”. 匹配除“\n”之外的任何单个字符若要匹配包括“\n”在内的任意字符请使用诸如“[.\n]”之类的模式\ 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符而“$”则匹配“$”\d 匹配纯数字\s 匹配空的空格或者制表符{n} 重复 n 次{n,} 重复 n 次或更多次{n,m} 重复 n 到 m 次[ ] 定义匹配的字符范围[c] 匹配单个字符 c[a-z] 匹配 a-z 小写字母的任意一个[a-zA-Z0-9] 匹配所有大小写字母或数字() 表达式的开始和结束位置| 或运算符
访问路由location
location的分类
location 大致可以分为三类
精准匹配location / {}一般匹配location / {}正则匹配location ~ / {}
location 常用的匹配规则 进行普通字符精确匹配也就是完全匹配。
^~ 表示普通字符匹配。使用前缀匹配。如果匹配成功则不再匹配其它 location。
~ 区分大小写的匹配。
~* 不区分大小写的匹配。
!~ 区分大小写的匹配取非。
!~* 不区分大小写的匹配取非 location [优先级]
首先精确匹配 其次前缀匹配 ^~其次是按文件中顺序的正则匹配 或*然后匹配不带任何修饰的前缀匹配最后是交给 / 通用匹配
location 示例说明
1location / {}
为精确匹配 / 主机名后面不能带任何字符串比如访问 / 和 /data则 / 匹配/data 不匹配
再比如 location /abc则只匹配/abc /abc/或 /abcd不匹配。若 location /abc则即匹配/abc 、/abcd/ 同时也匹配 /abc/。2location / {}
因为所有的地址都以 / 开头所以这条规则将匹配到所有请求 比如访问 / 和 /data, 则 / 匹配 /data 也匹配
但若后面是正则表达式会和最长字符串优先匹配最长匹配3location /documents/ {}
匹配任何以 /documents/ 开头的地址匹配符合以后还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时才会采用这一条4location /documents/abc {}
匹配任何以 /documents/abc 开头的地址匹配符合以后还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时才会采用这一条5location ^~ /images/ {}
匹配任何以 /images/ 开头的地址匹配符合以后停止往下搜索正则采用这一条6location ~* \.(gif|jpg|jpeg)$ {}
匹配所有以 gif、jpg或jpeg 结尾的请求
然而所有请求 /images/ 下的图片会被 location ^~ /images/ 处理因为 ^~ 的优先级更高所以到达不了这一条正则7location /images/abc {}
最长字符匹配到 /images/abc优先级最低继续往下搜索其它 location会发现 ^~ 和 ~ 存在8location ~ /images/abc {}
匹配以/images/abc 开头的优先级次之只有去掉 location ^~ /images/ 才会采用这一条9location /images/abc/1.html {}
匹配/images/abc/1.html 文件如果和正则 ~ /images/abc/1.html 相比正则优先级更高
优先级总结 (location ) (location 完整路径) (location ^~ 路径) (location ,* 正则顺序) (location 部分起始路径) (location /
实际网站使用中的三个匹配规则定义
第一个必选规则
直接匹配网站根通过域名访问网站首页比较频繁使用这个会加速处理比如说官网。 这里是直接转发给后端应用服务器了也可以是一个静态首页 location / {proxy_pass http://tomcat_server/;
}
第二个必选规则是处理静态文件请求
这是nginx作为http服务器的强项 有两种配置模式目录匹配或后缀匹配,任选其一或搭配使用 location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {root /webroot/res/;
第三个规则就是通用规则
比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器 非静态文件请求就默认是动态请求 location / {
proxy_pass http://tomcat_server;
}
访问重写rewrite
rewrite的概述
rewrite功能就是使用nginx提供的全局变量或自己设置的变量结合正则表达式和标志位实现url重写以及重定向。 rewrite只能放在server{},location{},if{}中并且默认只能对域名后边的除去传递的参数外的字符串起作用\
rewrite 执行顺序如下
(1) 执行 server 块里面的 rewrite 指令。 (2) 执行 location 匹配。 (3) 执行选定的 location 中的 rewrite 指令。
语法: rewrite regex replacement [flag]; regex 表示正则匹配规则。 replacement 表示跳转后的内容。 flag 表示 rewrite 支持的 flag 标记
###flag标记说明###
last 本条规则匹配完成后继续向下匹配新的location URI规则一般用在 server 和 if 中。
break 本条规则匹配完成即终止不再匹配后面的任何规则一般使用在 location 中。
redirect返回302临时重定向浏览器地址会显示跳转后的URL地址。
permanent返回301永久重定向浏览器地址栏会显示跳转后的URL地址。
rewrite 示例
实验1基于域名的跳转
现在公司旧域名www.cxk.com有业务需求变更需要使用新域名www.cxk2.com代替但是旧域名不能废除需要跳转到新域名上而且后面的参数保持不变
server {listen 80;server_name www.cxk.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.cxk.com-access.log main; #日志修改location / { #添加域名重定向if ($host www.cxk.com){ #$host为rewrite全局变量代表请求主机头字段或主机名rewrite ^/(.*)$ http://www.cxk2.com/$1 permanent; #$1为正则匹配的内容即域名后边的字符串}root html;index index.html index.htm;}
}mkdir -p /usr/local/nginx/html/testvim /usr/local/nginx/html/test/1.htmlecho 192.168.137.20 www.cxk.com www.cxk2.com /etc/hostssystemctl restart nginx #重启服务浏览器输入模拟访问 www.cxk.com/test/1.html… 会跳转到www.cxk2.com/test/1.html查看元素可以看到返回301实现了永久重定向跳转而且域名后的参数也正常跳转
实验2基于客户端 IP 访问跳转
要求今天公司业务新版本上线要求所有 IP 访问任何内容都显示一个固定维护页面只有公司 IP 192.168.137.15访问正常
server {listen 80;server_name www.zjl.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.cxk.com-access.log main; #日志修改#设置是否合法的IP标记set $rewrite true; #设置变量$rewrite变量值为boole值true#判断是否为合法IPif ($remote_addr 192.168.137.20){ #当客户端IP为192.168.184.200时将变量值设为false不进行重写set $rewrite false;}#除了合法IP其它都是非法IP进行重写跳转维护页面if ($rewrite true){ #当变量值为true时进行重写rewrite (.) /weihu.html; #重写在访问IP后边插入/weihu.html例如192.168.80.11/weihu.html}location /weihu.html {root /var/www/html; #网页返回/var/www/html/weihu.html的内容}location / {root html;index index.html index.htm;}
}mkdir -p /var/www/html/
echo h1We are busy now!/h1 /var/www/html/weihu.html
systemctl restart nginx实验3基于旧域名跳转到新域名后面加目录
现在访问的是 mail.cxk.com/post现在需要将这…
server {listen 80;server_name bbs.cxk.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.cxk.com-access.log main;#添加location /post {rewrite (.) http://www.cxk.com/mail$1 permanent; #这里的$1为位置变量代表/post}location / {root html;index index.html index.htm;}
}mkdir -p /usr/local/nginx/html/bbs
echo this is 1.html /usr/local/nginx/html/mail/1.html
echo 192.168.137.20 bbs.cxk.com /etc/hosts
systemctl restart nginx实验4基于参数匹配的跳转 listen 80;server_name www.cxk.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.cxk.com-access.log main;if ($request_uri ~ ^/100-(100|200)-(\d).html$) {rewrite (.*) http://www.cxk.com permanent;}location / {root html;index index.html index.htm;}
}systemctl restart nginx实验5基于目录下所有 php 结尾的文件跳转 listen 80;server_name www.cxk.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.cxk.com-access.log main;location ~* /upload/.*\.php$ {rewrite (.) http://www.cxk.com permanent;}location / {root html;index index.html index.htm;}
}
systemctl restart nginx
实验6基于最普通一条 url 请求的跳转 listen 80;server_name www.cxk.com; #域名修改 charset utf-8;access_log /var/log/nginx/www.cxk.com-access.log main;location ~* ^/abc/123.html {rewrite (.) http://www.cxk.com permanent;}location / {root html;index index.html index.htm;}
}
systemctl restart nginx