rewrite跳转应用场景
rewrite跳转的实现
rewrite的主要作用
rewrite中命令执行顺序
rewrite的使用语法
flag标记类型
location的分类
location常用匹配规则
location匹配优先级规则
设置nginx基于域名跳转
server {
listen 80;
server_name www.test.com; //修改域名
#charset koi8-r;
access_log /var/log/nginx/www.test.com-access.log; //开启日志
location / {
if ($host = 'www.test.com'){ //判断输出的是否是test.com
rewrite ^/(.*)$ http://www.ceshi.com/$1 permanent; //是就跳转到ceshi.com 类型是永久跳转,$1匹配的位置变量,就是域名后面的字符串
}
root html;
index index.html index.htm;
}
设置nginx基于客户端ip访问跳转
server {
listen 80;
server_name www.test.com;
#charset koi8-r;
access_log /var/log/nginx/www.test.com-access.log;
set $rewrite true; //设置变量的布尔值
if ($remote_addr = "12.0.0.8"){
set $rewrite false; //判断ip地址,对了就打上false标签
}
if ($rewrite = true) { //当变量值为true时跳转下面的页面
rewrite (.+) /weihu.html;
}
location = /weihu.html {
root /var/www/html; //页面返回此文件内容
}
location / {
root html;
index index.html index.htm;
}
创建weihu.html,写入提示信息并重启服务
[root@server ~]# cd /var/www/html/
[root@server html]# ls
weihu.html
[root@server html]# cat weihu.html
<h1>正在维护中。。。</h1>
[root@server html]#
[root@server html]# systemctl stop nginx.service
[root@server html]# systemctl start nginx.service
给自己的浏览器清清缓存~
应该显示的是自己设置的图片
而位于其他机器上的浏览器显示的是👇
跳转后转到新域名后并加目录
- 修改nginx配置文件
server { listen 80; server_name bbs.123.com; //修改域名 #charset koi8-r; access_log /var/log/nginx/www.ceshi.com-access.log; location /post { rewrite ^(.+) http://www.ceshi.com/bbs$1 permanent; } //匹配到post跳转,$1代表/post location / { root html; index index.html index.htm; }
重启服务
-
[root@server html]# 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@server html]# systemctl stop nginx.service [root@server html]# systemctl start nginx.service
- 然后再写一个bbs的页面并写入hello
[root@server post]# pwd /usr/local/nginx/html/bbs/post [root@server post]# ls 1.html [root@server post]# cat 1.html <h1>hello</h1>
清缓存,用bbs.123.com/post/1.html 访问网页
基于多余参数匹配跳转
- 首先修改nginx配置文件
-
server { listen 80; server_name www.test.com; #charset koi8-r; access_log /var/log/nginx/www.test.com-access.log; if ($request_uri ~ ^/100-(10|20|30)-(\d+)\.html$){ //设置正则匹配,在com/后面跟100-10或20或30-随机数字 rewrite (.*) http://www.test.com permanent; //匹配到上面的就会跳转到www.test.com } location / { root html; index index.html index.htm; }
然后重启访问页面,清缓存
-
基于目录下的所有php结尾的文件跳转
- 首先配置nginx文件
-
server { listen 80; server_name www.test.com; #charset koi8-r; access_log /var/log/nginx/www.test.com-access.log; location ~* /123/.*\.php$ { //正则匹配test文件下的以php结尾的文件 rewrite (.+) http://www.test.com permanent; //匹配到就跳转到www.test.com } location / { root html; index index.html index.htm; }
然后重启访问页面,清缓存
基于最普通的一条URL请求跳转
- 首先修改nginx配置
-
server { listen 80; server_name www.test.com; #charset koi8-r; access_log /var/log/nginx/www.test.com-access.log; location ~* /123/123.html { //正则匹配123下面的123.html rewrite (.+) http://www.test.com permanent; //匹配到就跳转到www.test.com } location / { root html; index index.html index.htm; }
先重启然后访问页面,清缓存
TIPS
Rewrite使用场景
在Nginx中使用Rewrite实现跳转有以下三种场景:
- 直接用Rewrite进行匹配跳转
- 使用if匹配全局变量进行跳转
- 使用location匹配再进行跳转
所以说rewrite语句只允许放在server{ },if{ },location{ }中