需要使用泛域名解析,
並且加上空的判斷,以保證沒有二級域名的也可以訪問
核心配置
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 {
}
}