Nginx二级域名自动或手动匹配子目录
Author: 咳嗽di小鱼 Date: March 30, 2011 Category: Network
说写就写.
刚刚搞定了Nginx二级域名的自动rewrite, 只要把自动匹配的配置放在几个特殊server定义之前, 就不会相互影响, 还挺简单的.
需要在nginx.conf里修改http里增加新的server部分
先说说标准的二级域名rewrite怎么写:
server { 
    listen 80; 
    server_name sub.domain.com; 
    index index.html index.htm index.php;
    root /***/wwwroot/sub_domain; 
    include other.conf; 
    location ~ .*\.(php|php5)?$ { 
        fastcgi_pass unix:/tmp/php-cgi.sock; 
        fastcgi_index index.php; 
        include fcgi.conf; 
    } 
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { 
        expires 30d; 
    } 
    location ~ .*\.(js|css)?$ { 
        expires 12h; 
    } 
    access_log off; 
}
基本上在server_name里写上地址, root里写上对应的目录就万事大吉了
然后是自动匹配的部分, 区别只在server_name和root的解析上:
server_name *.domain.com; 
set $subdomain "default"; 
if ( $host ~* (.*)\.(.*)\.(.*)) { 
    set $subdomain $1; 
} 
root /***/wwwroot/$subdomain;
第一篇结束...哈哈
注: 以上配置是以Lnmp一键安装包自动生成的配置文件为基础修改的