当前位置: 代码迷 >> Web前端 >> nginx二级域名配备(rewrite)
  详细解决方案

nginx二级域名配备(rewrite)

热度:1110   发布时间:2012-08-21 13:00:21.0
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;?
http://blog.sina.com.cn/s/blog_470302440100eveq.htmlnginx二级域名配置(rewrite)