nginx rewrite 實現二級域名跳轉

當訪問http://cbs.test.com跳轉到http://www.test.com/test/cbs/
方法一: (這種方法瀏覽器地址會變www.test.com/test/cbs)
server {
     listen 80;
     server_name www.test.com;
     location / {
         root /data/test;
         index index.html;
         }

      }


server {
     listen 80;
     server_name *.test.com;
     if ( $http_host ~* "^(.*)\.test\.com$") {
          set $domain $1;
          rewrite ^(.*) http://www.test.com/test/$domain/ break;
         }  

     }

方法二: (這樣配置瀏覽器的地址就會顯示成http://cbs.test.com)

server {
               listen 80;
               server_name *.test.com;
               root /usr/local/www;
               location ~ ^/(test|p_w_picpaths|styles)/ 這是裏可以加多個目錄,如果不加目錄,會無法訪問到cbs.test.com/目錄下的文件,如圖片目錄/p_w_picpaths
               {
                     proxy_redirect        off;
                     proxy_set_header    Host   www.test.com;
                     proxy_pass      http://192.168.1.3:80;
               }

               location / {
                               set $domain default;
                               if ( $http_host ~* "^(.*)\.test\.com$") {
                                               set $domain $1;
                               }
                               rewrite ^/(.*)    /test/$domain/$1 last;
               }
                                           }
               access_log off;
}

詳細過程

如:bs.myweb.com   訪問/data0/htdocs/bs

vi /usr/local/webserver/nginx/conf/nginx.conf

 server
 {
   listen       80;
   server_name  bs.myweb.com;
   index index.html index.htm index.php;
   root  /data0/htdocs/bs;

   location ~ .*\.(php|php5)?$
   {
     #fastcgi_pass  unix:/tmp/php-cgi.sock;
     fastcgi_pass  127.0.0.1:9000;
     fastcgi_index index.php;
     include fcgi.conf;
   }
 }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章