利用nginx泛域名解析配置二級域名和多域名,實現二級域名子站,用戶個性獨立子域名。php
主要針對用戶獨立子域名這種狀況,不可能在配置裏面將用戶子域名寫完,所以須要經過nginx泛解析方式。
css
配置方法:html
server_name ~^(?<subdomain>.+)\.yourdomain\.com$;
經過匹配subdomain便可。而在下面的能夠經過$subdomain這個變量獲取當前子域名稱。
nginx
這種狀況下,只須要直接匹配就能夠了,目錄都是指向同一個地方的(通常)。dom
配置實例:
網站
server {
url
listen 80;
spaserver_name yourdomain.com www.yourdomain.cpm ~^(?<subdomain>.+)\.m\.yourdomain\.com$;
.net
index index.php index.html index.htm;
codeset $root_path '/var/www/yanue.net';
root $root_path;
try_files $uri $uri/ @rewrite;
location @rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
}
這樣能夠實現:
user.m.yourdomain.com 跳轉到用戶本身頁面
固然跳轉邏輯須要本身在程序裏面去實現。
網站的目錄結構爲
html
├── bbs
└── www
html爲nginx的安裝目錄下默認的存放源代碼的路徑。
bbs爲論壇程序源代碼路徑
www爲主頁程序源代碼路徑
把相應程序放入上面的路徑經過
http://www.youdomain.com 訪問的就是主頁
http://bbs.yourdomain.com 訪問的就是論壇
其它二級域名類推。
配置實例:
server {
listen 80;
server_name ~^(?<subdomain>.+)\.yourdomain\.com$;
root html/$subdomain;
index index.html index.htm index.php;
fastcgi_intercept_errors on;
error_page 404 = /404.html;
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't
# break when using query string
try_files $uri $uri/ =404;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param domain $subdomain;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
參考:http://www.nginx.cn/612.html