記錄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