Nginx下配置子域名到相应文件夹虚拟主机规则的设定

2015-02-11 15:34:07    2015-02-11 15:34:07

以下配置文件可以用于在Nginx下方便地设置同一域名下各子域名的虚拟主机规则。

比如abc.MYDOMAIN.TLD会被映射到/var/www/MYDOMAIN.TLD/abc/public下面,而xyz.MYDOMAIN.TLD会被映射到/var/www/MYDOMAIN.TLD/xyz/public下面

server {

        listen 80;
        server_name ~^(?.+).MYDOMAIN.TLD$;
        access_log   /var/log/nginx/MYDOMAIN.TLD.access.log;
        error_log    /var/log/nginx/MYDOMAIN.TLD.error.log;

        root /var/www/MYDOMAIN.TLD/$subdomain/public;

        index index.php index.html;

        location / {
                try_files $uri $uri/ /index.php?$args; 
        }

        location ~ .php$ {
                try_files $uri /index.php;
                include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }

}