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.html nginx二级域名配置(rewrite)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值