利用nginx泛域名解析配置二級域名和多域名,實現二級域名子站,用戶個性獨立子域名。

利用nginx泛域名解析配置二級域名和多域名,實現二級域名子站,用戶個性獨立子域名。php

主要針對用戶獨立子域名這種狀況,不可能在配置裏面將用戶子域名寫完,所以須要經過nginx泛解析方式。
css

配置方法:html

server_name  ~^(?<subdomain>.+)\.yourdomain\.com$;

經過匹配subdomain便可。而在下面的能夠經過$subdomain這個變量獲取當前子域名稱。
nginx

狀況一:綁定子域名到統一目錄,做爲用戶個性域名

這種狀況下,只須要直接匹配就能夠了,目錄都是指向同一個地方的(通常)。dom

配置實例
網站

 
  1. server {
    url


  2.     listen   80;
    spa

  3.     server_name yourdomain.com www.yourdomain.cpm ~^(?<subdomain>.+)\.m\.yourdomain\.com$;
    .net


  4.     index index.php index.html index.htm;
    code

  5.     set $root_path '/var/www/yanue.net';

  6.     root $root_path;


  7.     try_files $uri $uri/ @rewrite;


  8.     location @rewrite {

  9.         rewrite ^/(.*)$ /index.php?_url=/$1;

  10.     }


  11.     location ~ \.php {

  12.             fastcgi_pass   127.0.0.1:9000;

  13.     }


  14.     location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {

  15.         root $root_path;

  16.     }


  17.     location ~ /\.ht {

  18.         deny all;

  19.     }

  20. }

這樣能夠實現:

user.m.yourdomain.com 跳轉到用戶本身頁面

固然跳轉邏輯須要本身在程序裏面去實現。

狀況二:綁定子域名到不一樣目錄(子站)

網站的目錄結構爲

html
├── bbs
└── www

html爲nginx的安裝目錄下默認的存放源代碼的路徑。

bbs爲論壇程序源代碼路徑

www爲主頁程序源代碼路徑

把相應程序放入上面的路徑經過

http://www.youdomain.com 訪問的就是主頁

http://bbs.yourdomain.com 訪問的就是論壇

其它二級域名類推。

配置實例

 
  1. server {

  2.         listen       80;

  3.         server_name  ~^(?<subdomain>.+)\.yourdomain\.com$;

  4.         root   html/$subdomain;

  5.         index  index.html index.htm index.php;

  6.         fastcgi_intercept_errors on;

  7.         error_page  404      = /404.html;

  8.         location / {

  9.                 # This is cool because no php is touched for static content.

  10.                 # include the "?$args" part so non-default permalinks doesn't

  11.                 # break when using query string

  12.                 try_files $uri $uri/ =404;

  13.        }


  14.         # redirect server error pages to the static page /50x.html

  15.         #

  16.         error_page   500 502 503 504  /50x.html;

  17.         location = /50x.html {

  18.             root   html;

  19.         }


  20.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

  21.         #

  22.         location ~ \.php$ {

  23.             fastcgi_pass   127.0.0.1:9000;

  24.             fastcgi_index  index.php;

  25.             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

  26.             fastcgi_param  domain $subdomain;

  27.             include        fastcgi_params;

  28.         }


  29.         # deny access to .htaccess files, if Apache's document root

  30.         # concurs with nginx's one

  31.         #

  32.         location ~ /\.ht {

  33.             deny  all;

  34.         }

  35.     }

參考:http://www.nginx.cn/612.html

相關文章
相關標籤/搜索