nginx rewrite 基础

Posted 借风拥你

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx rewrite 基础相关的知识,希望对你有一定的参考价值。

、跳转到首页

 如果请求的页面不存在的话就跳转到首页

 location / {

     if (!-e $request_filename){

       rewrite ^/(.*) /index.php last;

     }

 }

  

二、地址跳转

格式:

rewirte  要跳转的页面      跳转的目标页面

 

例子:

rewrite a.html http://192.168.3.10/b.html last;

 

要跳转的页面:a.html

跳转的目标页面:b.html

注意:跳转的目标页面必须写"全路径":比如以下

http://192.168.3.10/b.html

 

 

实例:

echo " <h1>ye mian 1</h1> " > a.html

echo " <h1>ye mian 2</h1> " > b.html

 

vim /usr/local/nginx/conf/nginx.conf

修改43行下的内容

        location / {

             root   /web/benet;

             index  index.html index.htm;

             if (!-e $request_filename){

             rewrite ^/(.*) /index.html last;

             }

           rewrite a.html http://192.168.3.10/b.html last;

         }

 

测试:

访问不存在的页面,会自动跳转到首页

访问a.html会自动跳转到b.html

以上是关于nginx rewrite 基础的主要内容,如果未能解决你的问题,请参考以下文章

继续了解nginx与rewrite跳转!

Nginx rewrite

Nginx的Rewrite跳转!

Nginx的Rewrite跳转!

10. Nginx Rewrite(重定向)

Nginx rewrite配置