chaihongjun.me

Nginx泛解析匹配域名绑定到子目录配置方法

# tree /dalidata1/wwwroot/yourdomain.com
/dalidata1/wwwroot/yourdomain.com
├── bbs
│    └── index.html
└── www    
      └── index.html
2 directories, 2 files

/dalidata1/wwwroot/yourdomain.com

为nginx的安装目录下默认的存放源代码的路径。bbs是论坛程序路径,www是网站主页程序路径。

通过 bbs.yourdomain.com  和 www.yourdomain.com分别可以访问BBS和网站主页

两种配置方式:

1.

server {
listen 80;
#################注意  ~^(?<subdomain>.+)  ############
server_name ~^(?<subdomain>.+).yourdomain.com$;
access_log /dalidata1/wwwlogs/yourdomain.com_nginx.log combined;
index index.html index.htm index.php;
#################注意  $subdomain  ############
root /dalidata1/wwwroot/yourdomain.com/$subdomain/;
location ~ .php$ {
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
        }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
      }
    location ~ .*\.(js|css)?$ {
    expires 30d;
     }
 }

2.

server 
{
listen 80;
server_name *.yourdomain.com;
access_log /alidata1/wwwlogs/yourdomain.com_nginx.log combined;
index index.html index.htm index.php;
if ($host ~* ^([^\.]+)\.([^\.]+\.[^\.]+)$) {
    set $subdomain $1;
    set $domain $2;}location / {
    root /dalidata1/wwwroot/yourdomain.com/$subdomain/;
    index index.php index.html index.htm;}location ~ .php$ {
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
       }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
       }
    location ~ .*\.(js|css)?$ {
    expires 30d;
       }
    }

推荐使用第一种方法

知识共享许可协议本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。作者:柴宏俊»