利用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

相关文章
相关标签/搜索