nginx篇最初级用法之地址重写

nginx服务器的地址重写,主要用到的配置参数是rewrite

  • rewrite regex replacement flag
  • rewrite 旧地址 新地址 [选项]

支持的选项有:

  • last 不再读其他rewrite
  • break 不再读其他语句,结束请求
  • redirect 临时重定向
  • permament 永久重定向
  • 在nginx网页目录中添加两个页面进行测试
  • [root@proxy html]# echo "new page" > new.html
  • [root@proxy html]# echo "old page" > old.html
  • [root@proxy html]# curl http://localhost/old.html
  • old page
  • [root@proxy html]# curl http://localhost/new.html
  • new page

将old.html重定向到new.html

 重载nginx配置文件

[root@proxy sbin]# ./nginx -s reload

访问测试

可以看到重定向好处在不改变地址栏的情况下将也没重定向到其他页面,

比如说跳转到:对不起不访问的页面不存在....

当然也可以在rewrite后添加redirect 属性让地址栏也一起跳转.

rewrite /old.html /new.html redirect;

当然也可以多种用法例如将所有A网站的请求转发至B网站

rewrite ^/ http://www.baidu.com/;

也可以讲A网站下请求的所有子页面请求转向B网站访问同样的页面地址

rewrite ^/(.*)$ http://www.baidu.com/$1;

猜你喜欢

转载自www.cnblogs.com/lqinghua/p/11620299.html