利用nginx泛域名解析配置二级域名和多域名
三级域名解析
比如 我有2个三级域名的表达式*.a.sss.com *.b.sss.com
想转发到我实际程序的一个地址
http://www.sss.com/xxx/xxx.do?domain=三级域名(表达式里的*)&from=二级域名(a或b)
server {
        listen       80;
        server_name  *.sss.com;
        if ($http_host ~* "^(.*)\.(a|b)\.sss\.com$") {
            set $three $1;
            set $two $2;
            rewrite ^(.*)$ http://www.sss.com/xxx/xxx.do?domain=$three&from=$two  permanent;
        }
泛域名解析
if ( $host ~* (\b(?!www\b)\w+)\.\w+\.\w+ ) {
    set $subdomain /$1;
}
location / {
    root /html/$subdomain;
    index index.html;
}
使用方法,请将上面代码复制到 server {} 标签中,然后重启nginx即可。
稍作解释,以上的正则表达式表示不匹配 “www” 但可以匹配包含 “www” 的子域名。
server {
    listen 80;
    server_name *.kbook.org
    location / {
      add_header  Content-Type 'text/html; charset=utf-8';
        #根目录为$host,$PATH为$host所在的目录。
        root $PATH/$host;
        return 200 "$host";
    }
http {
#...
server {
    listen 80;
    server_name $host;#在server_name中使用$host而不用自己去一个一个绑定了。
    #...
    location / {
        #根目录为$host,$PATH为$host所在的目录。
        root $PATH/$host;
        #....
    }
}
nginx泛域名解析,实现多个二级域名
利用nginx泛域名解析配置二级域名和多域名,实现二级域名子站,用户个性独立子域名。
主要针对用户独立子域名这种情况,不可能在配置里面将用户子域名写完,因此需要通过nginx泛解析方式。
配置方法:
server_name ~^(? 通过匹配subdomain即可。而在下面的可以通过$subdomain这个变量获取当前子域名称。 这种情况下,只需要直接匹配就可以了,目录都是指向同一个地方的(一般)。 配置实例: 这样可以实现: user.m.yourdomain.com 跳转到用户自己页面 当然跳转逻辑需要自己在程序里面去实现。 网站的目录结构为 html html为nginx的安装目录下默认的存放源代码的路径。 bbs为论坛程序源代码路径 www为主页程序源代码路径 把相应程序放入上面的路径通过 http://www.youdomain.com 访问的就是主页 http://bbs.yourdomain.com 访问的就是论坛 其它二级域名类推。 配置实例: server { } nginx 泛域名 泛解析 ## 反过来: 带参数的动态地址重定向到静态地址 泛域名解析 view plaincopy to clipboardprint? #匹配三级或三级以上的域名 if ($host ~* ^(.+).([^.]+).([^.]+).([^.]+)$) { } 利用nginx泛域名解析配置二级域名和多域名 网站的目录结构为 html为nginx的安装目录下默认的存放源代码的路径。 bbs为论坛程序源代码路径 把相应程序放入上面的路径通过 server { 总结一下步骤就是 1.把上面的红色配置换成你的域名后添加到你的nginx.conf配置文件 2.确认要增加的二级域名,如bbs.yourdomain.com 3.设置bbs.yourdomain.com解析到你的nginx服务器ip 4.在html目录下创建bbs目录 5.把源码放入bbs目录 6.重新加载nginx配置 kill -HUP cat /usr/local/lnmp/nginx/nginx.conf (需要把上面命令的路径换成你的配置文件路径)
情况一:绑定子域名到统一目录,作为用户个性域名server {
    listen   80;
    server_name yourdomain.com www.yourdomain.cpm ~^(?<subdomain>.+)\.m\.yourdomain\.com$;
    index index.php index.html index.htm;
    set $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;
    }
}
情况二:绑定子域名到不同目录(子站)
├── bbs
└── www    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;
    }
rewrite "^(.*)/service/(.*)\.html$" $1/service.php?sid=$2 permanent;
if ($query_string ~* id=(.*)) {
set $id $1;
rewrite "^(.*)/article.asp$" $1/article/$id.htm last;
}
server_name www.w3cgroup.com *.w3cgroup.com;
server_name_in_redirect off;
#设置默认root
set $rootdir /usr/local/nginx/html/w3cgroup/;
#匹配三级域名
if ($host ~* ^([^.]+).([^.]+).([^.]+).([^.]+)$) {
#三级域名中有访问指定的目录则重定向到相应的二级域名下
rewrite "^.+upload/?(.)$" http://upload.w3cgroup.com/$1 permanent;
rewrite "^.+ijc/?(.)$" http://ijc.w3cgroup.com/$1 permanent;
break;
}
#匹配二级域名
if ($host ~* ^([^.]+).([^.]+).([^.]+)$) {
set $rs1 $1;
}
#设置www时root
if ($rs1 ~* ^www$) {
set $rootdir /usr/local/nginx/html/platform_ig/;
#二级域名中有访问指定的目录则重定向到相应的二级域名下,注意,这里要使用last
rewrite "^.+upload/?(.)$" upload/$1 last;
rewrite "^.+ijc/?(.)$" ijc/$1 last;
break;
}
#设置非www二级域名时root
if ($rs1 !~* ^www$) {
set $rootdir /usr/local/nginx/html/w3cgroup/$rs1;
#二级域名中有访问指定的目录则重定向到相应的二级域名下
rewrite "^.+upload/?(.)$" http://upload.w3cgroup.com/$1 permanent;
rewrite "^.+ijc/?(.)$" http://ijc.w3cgroup.com/$1 permanent;
break;
}
#应用root
root $rootdir;
index index.php index.html;
error_page 404 http://$host/;
html
├── bbs
└── www
www为主页程序源代码路径
http://www.youdomain.com 访问的就是主页
http://bbs.yourdomain.com 访问的就是论坛
其它二级域名类推。    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;
    }
}