nginx rewrite 实现二级域名跳转

nginx rewrite 实现二级域名跳转

当访问 http://abc.test.com 跳转到 http://www.test.com/test/abc/
方法一:
这种方法浏览器地址会变 www.test.com/test/abc
实现访问如下:
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; 
                                                            } 
     

      }
 
实现结果
 
C:\>wget.exe -S http://abc.test.com/
--2009-07-10 00:46:12--    http://abc.test.com/
Resolving abc.test.com... 127.0.0.1
Connecting to abc.test.com|127.0.0.1|:80... connected.
HTTP request sent, awaiting response...
    HTTP/1.1 302 Moved Temporarily
    Server: nginx/0.8.4
    Date: Thu, 09 Jul 2009 16:46:12 GMT
    Content-Type: text/html
    Content-Length: 160
    Connection: keep-alive
    Location: http://www.test.com/test/abc/
Location: http://www.test.com/test/abc/ [following]
--2009-07-10 00:46:12--     http://www.test.com/test/abc/
Resolving www.test.com... 127.0.0.1
Reusing existing connection to abc.test.com:80.
HTTP request sent, awaiting response...
    HTTP/1.1 200 OK
    Server: nginx/0.8.4
    Date: Thu, 09 Jul 2009 16:46:12 GMT
    Content-Type: text/html
    Content-Length: 21
    Last-Modified: Thu, 09 Jul 2009 16:27:41 GMT
    Connection: keep-alive
    Accept-Ranges: bytes
Length: 21 [text/html]
Saving to: `index.html.19'

100%[======================================>] 21                    --.-K/s     in 0s

2009-07-10 00:46:12 (936 KB/s) - `index.html.19' saved [21/21]
 
方法二、
 
当访问 http://abc.test.com 跳转到 http://www.test.com/test/abc/
这样配置浏览器的地址就会显示成 http://abc.test.com
 
server {
                listen 80;
                server_name *.test.com;
                root /usr/local/www;
                location ~ ^/(test|images|styles)/ 这是里可以加多个目录,如果不加目录,会无法访问到abc.test.com/目录下的文件,如图片目录/images
                { 
                      proxy_redirect        off; 
                      proxy_set_header    Host   www.test.com ; 
                      proxy_pass       http://192.168.1.2:8080 ;
                }

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

你可能感兴趣的:(nginx,职场,休闲)