全国知名网站建设,企业vi设计公司旅游公司logo,wordpress视频 小程序,动态wordpress动态主题Rewrite主要实现url地址重写#xff0c;以及重定向#xff0c;就是把传入web的请求重定向到其他url的过程。
分以下几种场景使用
1、地址跳转#xff0c;用户访问一个URL#xff0c;将其定向到另一个URL
2、协议跳转#xff0c;用户通过http协议请求网站时#xff0c;…Rewrite主要实现url地址重写以及重定向就是把传入web的请求重定向到其他url的过程。
分以下几种场景使用
1、地址跳转用户访问一个URL将其定向到另一个URL
2、协议跳转用户通过http协议请求网站时将其重新跳转至https协议方式
3、伪静态动态页面静态化为了搜素引擎收录。
4、搜索引擎SEO优化依赖于url路径好记的url便于支持搜索引擎录入
Rewrite标记
每个Rewrite后面都有flag标记主要有以下几种
flag规则last停止当前匹配并重新发送请求barek终止匹配不发送新请求redirector临时跳转关闭nginx请求就不跳转了302premanent永久跳转访问过一次就不会访问原站了301第一次请求会保存缓存到浏览器中通过浏览器缓存跳转
更改配置文件准备代码文件进行测试last与break
[rootWeb01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite/;location / {rewrite /1.html /2.html;rewrite /2.html /3.html;}location /2.html {rewrite /2.html /a.html;}location /3.html {rewrite /3.html /b.html;
rewrite.conf 18L, 343C written
[rootWeb01 conf.d]# systemctl restart nginx
[rootWeb01 conf.d]# mkdir -p /code/rewrite
[rootWeb01 conf.d]# echo 1.html /code/rewrite/1.html
[rootWeb01 conf.d]# echo 2.html /code/rewrite/2.html
[rootWeb01 conf.d]# echo 3.html /code/rewrite/3.html
[rootWeb01 conf.d]# echo a.html /code/rewrite/a.html
[rootWeb01 conf.d]# echo b.html /code/rewrite/b.html
发现访问1.html实际重定向到了b.html 添加last标记
[rootWeb01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite/;location / {rewrite /1.html /2.html last;rewrite /2.html /3.html;}location /2.html {rewrite /2.html /a.html;}location /3.html {rewrite /3.html /b.html;
rewrite.conf 18L, 348C written
[rootWeb01 conf.d]# systemctl restart nginx 跳过了当前location进行下一location重定向最终跳转到a.html 添加down标记
[rootWeb01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite/;location / {rewrite /1.html /2.html break;rewrite /2.html /3.html;}location /2.html {rewrite /2.html /a.html;}location /3.html {rewrite /3.html /b.html;
rewrite.conf 18L, 349C written
[rootWeb01 conf.d]# systemctl restart nginx
break后不再进行重定向操作最终定向到2.html redirect与permanent测试
[rootWeb01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code;location /test {#临时重定向rewrite ^(.*)$ http://www.koten.vip redirect; #return 302 http://www.koten.vip#永久重定向#rewrite ^(.*)$ http://www.koten.vip permanent; #return 301 http://www.koten.vip;}
}
~
~
rewrite.conf 12L, 356C written
[rootWeb01 conf.d]# systemctl restart nginx访问rewrite.koten.com/test定向到www.koten.vip Rewrite使用案例
我们先开启rewrite日志对规则进行匹配调试 rewrite_log on; #加入到/etc/nginx/nginx.conf中
案例1用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html中
[rootWeb01 conf.d]# mkdir -p /code/rewrite/ccc/bbb/
[rootWeb01 conf.d]# echo /ccc/bbb/2.html /code/rewrite/ccc/bbb/2.html
[rootWeb01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite;location /abc {rewrite (.*) /ccc/bbb/2.html redirect;#return 302 /ccc/bbb/2.html}
}
~
~
~
~
~
rewrite.conf 10L, 217C written
[rootWeb01 conf.d]# systemctl restart nginx 案例2用户访问/2018/ccc/bbb/2.html实际上真实访问的是/2023/ccc/bbb.2.html
[rootWeb01 conf.d]# mkdir -p /code/rewrite/2023/ccc/bbb/
[rootWeb01 conf.d]# echo /2023/ccc/bbb/2.html /code/rewrite/2023/ccc/bbb/2.html
[rootWeb01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite;location /2018 {rewrite ^/2018/(.*) /2023/$1 redirect;}
}
~
~
~
~
~
~
rewrite.conf 9L, 188C written
[rootWeb01 conf.d]# systemctl restart nginx 案例3用户访问/test实际上访问的是https://www.koten.vip
[rootWeb01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;location /test {rewrite (.*) https://www.koten.vip redirect;}
}
~
~
~
~
~
~
~
rewrite.conf 8L, 154C written
[rootWeb01 conf.d]# systemctl restart nginx案例4访问course-11-22-33.html实际真实访问/course/11/22/33/course_33.html
[rootWeb01 conf.d]# mkdir -p /code/rewrite/course/
11/22/33
[rootWeb01 conf.d]# echo /code/rewrite/course/11/22/33
/code/rewrite/course/11/22/33/course_33.html
[rootWeb01 conf.d]# vim /etc/nginx/conf.d/rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite;index index.html;location / {rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;}
}
~
~
~
~
~
rewrite.conf 10L, 230C written
[rootWeb01 conf.d]# systemctl restart nginx 案例5将http请求跳转到https
[rootWeb01 conf.d]# vim /etc/nginx/conf.d/rewrite.conf
server {listen 443;server_name rewrite.koten.com;location / {root /code;index index.php index.html;}
}server {listen 80;server_name rewrite.koten.com;rewrite ^(.*) https://$server_name$1 redire
ct;
rewrite.conf 17L, 285C written
[rootWeb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[rootWeb01 conf.d]# systemctl restart nginx 案例6错误页跳转
[rootWeb01 rewrite]# cat /etc/nginx/conf.d/rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite;error_page 403 404 500 501 502 error_test;location error_test {rewrite ^(.*)$ /404.png break;}
}[rootWeb01 rewrite]# systemctl restart nginx 案例7在跳转的请求行后加上想要的参数showoffline1
[rootWeb01 rewrite]# vim /etc/nginx/conf.d/rewrite.conf
server {listen 80;server_name rewrite.koten.com;set $args showoffline1;location / {root /code/rewrite;index index.html;}if ($remote_addr 10.0.0.1 ){rewrite (.*) http://rewrite.koten.com$1;}
}
~
~
~
rewrite.conf 13L, 252C written
[rootWeb01 rewrite]# systemctl restart nginx案例8网站维护指定IP正常访问其他IP跳转至维护页面
[rootWeb01 rewrite]# vim /etc/nginx/conf.d/rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite;charset utf-8,gbk;location / {index index.html;if ($remote_addr ! 10.0.0.2){rewrite ^(.*)$ /网站维护.jpg break; #如
果来源IP不等于10.0.0.1则跳转维护页面}}}
rewrite.conf 16L, 321C written
[rootWeb01 rewrite]# systemctl restart nginx Nginx内置参数
$args #这个变量等于请求行中的参数。
$content_length #请求头中的Content-length字段。
$content_type #请求头中的Content-Type字段。
$document_root #当前请求在root指令中指定的值。
$host #请求主机头字段否则为服务器名称。
$http_user_agent #客户端agent信息
$http_cookie #客户端cookie信息
$limit_rate #这个变量可以限制连接速率。
$request_body_file #客户端请求主体信息的临时文件名。
$request_method #客户端请求的动作通常为GET或POST。
$remote_addr #客户端的IP地址。
$remote_port #客户端的端口。
$remote_user #已经经过Auth Basic Module验证的用户名。
$request_filename #当前请求的文件路径由root或alias指令与URI请求生成。
$query_string #与$args相同。
$scheme #HTTP方法如httphttps。
$server_protocol #请求使用的协议通常是HTTP/1.0或HTTP/1.1。
$server_addr #服务器地址在完成一次系统调用后可以确定这个值。
$server_name #服务器名称。
$server_port #请求到达服务器的端口号。
$request_uri #包含请求参数的原始URI不包含主机名如”/foo/bar.php?argbaz”。
$uri #不带请求参数的当前URI$uri不包含主机名如”/foo/bar.html”。
$document_uri #与$uri相同。
$X-Forwarded-For:HTTP的请求端真实的IP只有在通过了HTTP 代理或者负载均衡服务器时才会添加该项。标准格式如下X-Forwarded-For: client1, proxy1, proxy2 我是koten10年运维经验持续分享运维干货感谢大家的阅读和关注 文章转载自: http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.krxzl.cn.gov.cn.krxzl.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.wqbzt.cn.gov.cn.wqbzt.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.qyhcg.cn.gov.cn.qyhcg.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.sqhtg.cn.gov.cn.sqhtg.cn http://www.morning.trrpb.cn.gov.cn.trrpb.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.wktbz.cn.gov.cn.wktbz.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn http://www.morning.dbtdy.cn.gov.cn.dbtdy.cn http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn http://www.morning.liyixun.com.gov.cn.liyixun.com http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.zdydj.cn.gov.cn.zdydj.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.pqcbx.cn.gov.cn.pqcbx.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.21r000.cn.gov.cn.21r000.cn http://www.morning.nytqy.cn.gov.cn.nytqy.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn http://www.morning.muniubangcaishui.cn.gov.cn.muniubangcaishui.cn http://www.morning.tgcw.cn.gov.cn.tgcw.cn http://www.morning.yfmwg.cn.gov.cn.yfmwg.cn http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.xrpjr.cn.gov.cn.xrpjr.cn http://www.morning.ljwyc.cn.gov.cn.ljwyc.cn http://www.morning.gnghp.cn.gov.cn.gnghp.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn http://www.morning.bpmz.cn.gov.cn.bpmz.cn http://www.morning.csnmd.cn.gov.cn.csnmd.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.ypnxq.cn.gov.cn.ypnxq.cn http://www.morning.rpms.cn.gov.cn.rpms.cn http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.chzbq.cn.gov.cn.chzbq.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.bynf.cn.gov.cn.bynf.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.rgwz.cn.gov.cn.rgwz.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.kmwbq.cn.gov.cn.kmwbq.cn http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.jgncd.cn.gov.cn.jgncd.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.dbrpl.cn.gov.cn.dbrpl.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.bgpch.cn.gov.cn.bgpch.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn