nginx 基础5 rewrite 重新

1.开启rewrite日志

rewrite_log on; #http 段加入

error_log logs/xxxerror.log notice; #在将错误日志级别调低

2.跳转域名

 location / {
         rewrite / https://www.baidu.com;
        }

#表示,只要访问这个域名直接跳转到 baidu
nginx 基础5 rewrite 重新
#查看日志能看到记录用"/"访问了"111.com",跳转到了"baidu"
修改下代码

location /rewrite/ {
         rewrite / https://www.baidu.com;
        }

#表示只有用域名后面跟着"/rewrite/",文件夹才会跳转(只有/rewrite/才会触发跳转,其他文件夹正常转发)
如下图
nginx 基础5 rewrite 重新
#我这边用"www.111.com/rewrite/123.com" 访问才跳转了

3.使用正则跳转

例子1,
www.111.com/111/index.html 跳转到 www.111.com/222/index.html

location / {
         rewrite ^/111/(.*)$  /222/$1 ;
        }

#"^"表示根的意思,就表示 www.111.com 的的意思,(.*) 匹配所有的意思,后面"$1"调用,
nginx 基础5 rewrite 重新
#日志,能就看出/111/index.html 跳转到 /222/index.html

4.rewrite指令

last    终止在本location块中处理接收到的URI,并将此处重写的URI作为新的URI使用其他location进行处理。(只是终止当前location的处理)
break   将此处重写的URI作为一个新的URI在当前location中继续执行,并不会将新的URI转向其他location。
redirect    将重写后的URI返回个客户端,状态码是302,表明临时重定向,主要用在replacement字符串不以“http://”,“ https://”或“ $scheme” 开头;
permanent   将重写的URI返回客户端,状态码为301,指明是永久重定向;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章