记录Nginx实现内容重定向的两种方式:rewrite和反向代理


第一种:rewrite

server {

  listen                80;

       server_name   www.test.org;

       root                  /temp/test;

       #当访问www.test.org/uri,如果uri资源在项目中不存在,就会重定向到www.test.org/demo/index

       location ~.* {

               if (!-e $request_filename) {

                    rewrite ^ http://www.test.org/demo/index redirect;

               }

       }

}

第二种:反向代理(如果需要重定向到另外一个服务器)

server {

  listen                80;

       server_name   www.test.org;

       root                  /temp/test;

       #当访问www.test.org/uri,如果uri资源在项目中不存在,就会重定向到www.aa.org/demo/index

       location ~.* {

               if (!-e $request_filename) {

                    rewrite ^ /demo/index break;
                    proxy_pass http://www.aa.org;

               }

       }

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM