需要使用泛域名解析,

并且加上空的判断,以保证没有二级域名的也可以访问

核心配置

server_name ~^(?<subdomain>.+)\.caipudq\.cn$;
if ( $subdomain = '') {
set $subdomain "tp5caipu";
}
if ( $subdomain = 'www') {
set $subdomain "tp5caipu";
}
root /mnt/www/$subdomain/public;

完整配置

server {
listen 80;
#server_name www.caipudq.cn caipudq.cn ;
#root /mnt/www/tp5caipu/public;
server_name ~^(?<subdomain>.+)\.caipudq\.cn$;
if ( $subdomain = '') {
set $subdomain "tp5caipu";
}
if ( $subdomain = 'www') {
set $subdomain "tp5caipu";
}
root /mnt/www/$subdomain/public;

location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
location ~ \.php {
access_log /var/log/nginx/caipudq/access.log; #access_log属于ngx_http_log_module的设置,缺省level为info

error_log /var/log/nginx/caipudq/error.log; #error_log属于core module, 缺省的level是error
index index.php index.html index.htm; #由于是PHP类型的动态页面为主,所以把index.php放在前面效率会更高些

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;
try_files $uri $uri/ /index.php?$args; #普通php网站因为没有rewrite的话,这个不需要
}
location ~ .*\.(jpg|gif|png|bmp)$ {

gzip on;
gzip_http_version 1.1;
gzip_comp_level 3;
gzip_types text/plain application/json application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png image/x-ms-bmp;

}
error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

}