rewrite地址转换

1.Rewrite使用场景、实现原理。

1.地址跳转:www.360buy.com —> jd.com
2.协议跳转: http升级至https。 ( Rewrite方式来实现。 )
3.URL静态化,将动态URL地址显示为静态URL地址的一种技术。

2.Rewrite URI重写配置语法

	set			设定变量
	if			条件判断
	return		返回数据	
1.set if实现方法

需求1: 将用户请求url.wyk.com.zh跳转至url.wyk.com/zh
需求2: 将用户请求url.wyk.com.jp跳转至url.wyk.com/jp

[root@web01 ~]# cat /etc/nginx/conf.d/url.wyk.com.conf
server {
	listen 80;
	server_name url.wyk.com.zh url.oldxu.com.jp;

	location / {
		#判断用户请求的域名是zh还是jp
		if ( $http_host ~* "zh" ) {
			set $language zh;
		}

		if ( $http_host ~* "jp" ) {
			set $language jp;
		}

		#配置rewrite跳转规则
		rewrite ^/$ http://url.wyk.com/$language/ permanent;
	}
}

server {
	listen 80;
	server_name url.oldxu.com;
	root /opt;

	location / {
		index index.html;
	}
}
[root@web01 ~]# mkdir /opt/zh -p
[root@web01 ~]# mkdir /opt/jp -p
[root@web01 ~]# echo "zh.." > /opt/zh/index.html
[root@web01 ~]# echo "jp.." > /opt/jp/index.html
[root@web01 ~]# nginx -t
[root@web01 ~]# systemctl reload nginx

需求3: 过滤 Nginx 请求中包含 a1=3526 的http请求到 10.16.3.5 的 8080 端口处理。

server {
        listen 80;
        server_name url.wyk.com;
        root /opt;

        location / {
                index index.html;

                #如果用户请求的uri中a1=3256,我们通过反向代理代理到10.16.3.5:8080端口
                if ( $request_uri ~* 'a1=3256' ) {
                        #proxy_pass http://10.16.3.5:8080;		#这个地址是不存在的
                        return 200 'ok....!';				#所以使用return来替代模拟 
                }
        }
}

	测试的curl命令: curl -L -HHost:url.wyk.com http://10.0.0.7?a1=3256
		-L 跟随跳转
		-H 指定Host头,具体要请求的域名是
2.return:主要用来返回数据|返回字符串|返回url地址。

1.如果用户使用 IE 访问 url.wyk.com 则返回字符串 ( 请更换您的浏览器。 ) chrome [返回数据]

server {
	listen 80;
	server_name url.wyk.com;
	root /opt;
	charset gbk,utf-8;
	location / {
		index index.html;
		default_type text/html;
		#判断用户使用的是否是 chrome浏览器,如果是则 返回一段话,如果不是则正常访问。
		if ( $http_user_agent ~* "chrome|MSIE" ) {
			return 200 'Please change Browser!!!';
		}
	}
}

2.如果使用IE访问,直接报错 500. [返回状态码]

server {
	listen 80;
	server_name url.wyk.com;
	root /opt;
	charset gbk,utf-8;
	location / {
		index index.html;
		default_type text/html;
		#判断用户使用的是否是 chrome浏览器,如果是则 返回一段话,如果不是则正常访问。
		if ( $http_user_agent ~* "chrome|MSIE" ) {
			return 500;
		}
	}
}

3.如果使用IE访问,直接跳转至浏览器下载页面。http://www.firefox.com.cn/ [返回一个url地址]

server {
	listen 80;
	server_name url.wyk.com;
	root /opt;
	charset gbk,utf-8;
	location / {
		index index.html;
		default_type text/html;
		#判断用户使用的是否是 chrome浏览器,如果是则 返回一段话,如果不是则正常访问。
		if ( $http_user_agent ~* "chrome|MSIE" ) {
			return 302 http://www.firefox.com.cn;
		}
	}
}

3.Rewrite Flag标记?

rewrite主要是用来重写URL、或者说是用来做URL地址跳转的。

	关键字    正则表达式      替换成什么样子	标记
Syntax:	rewrite   regex        replacement     [flag];
Default:	
Context:	server, location, if
1.last匹配演示

last: 匹配成功,表示要停止当前location匹配重新向server发起匹配,这时因为之前匹配到2.html,所有会找/2.html进行匹配。
请求的是1.HTML,最终的访问结果是a.html
因为:在location{}内部,遇到last,本location{}内后续指令不在执行。
匹配成功后,会重新像Server{}标签发起请求,从头到尾在匹配一遍规则,那个匹配则执行哪个。

[root@web01 ~]# cat /etc/nginx/conf.d/url.wyk.com.conf 
server {
    listen 80;
    server_name url.wyk.com;
    root /opt;

    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;
    }
}
[root@web01 ~]# curl -L -HHost:url.wyk.com http://10.0.0.7/1.html
a.html

2.break匹配演示

break: 匹配成功,表示要停止继续匹配。
请求的是1.HTML,最终的访问结果是2.html
因为:在location {} 内部遇到了break,本location内以及后面的所有的location{}内的指令都不在执行。

[root@web01 ~]# cat /etc/nginx/conf.d/url.wyk.com.conf 
server {
    listen 80;
    server_name url.wyk.com;
    root /opt;

    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;
    }
}
[root@web01 ~]# curl -L -HHost:url.wyk.com http://10.0.0.7/1.html
2.html

  • break和lost区别
break与last区别说明:
	当rewrite规则遇到break后,本location{}与其他location{}的所有rewrite/return规则都不再执行。
	当rewrite规则遇到last后,本location{}里后续rewrite/return规则不执行,但重写后的url再次从头开始执行所有规则,哪个匹配执行哪个。
3.permanent永久跳转

http–https场景下:
permanent:状态码301,永久跳转。 新跳转的网站有排名,旧网站排名会被清空。 1.html(排名会被清空) 2.html(有排名)

[root@web01 ~]# cat /etc/nginx/conf.d/url.wyk.com.conf 
server {
    listen 80;
    server_name url.wyk.com;
    root /opt;

    location / {
        rewrite /1.html /2.html permanent;
}
}

4.redirect临时跳转

redirect: 状态码302,临时跳转。 旧网站排名无影响,新网站没有排名。 1.html(不影响) 2.html(没有排名)

[root@web01 ~]# cat /etc/nginx/conf.d/url.wyk.com.conf 
server {
    listen 80;
    server_name url.wyk.com;
    root /opt;

    location / {
        rewrite /1.html /2.html redirect;
}
}

永久跳转后关了nginx访问还可以跳转,临时不可以

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值