欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2022-06-29 23:29:34
网站的目录结构为:  # tree /home/wwwroot/exehack.net /home/wwwroot/exehack.net ├── bbs...

网站的目录结构为:

 # tree /home/wwwroot/exehack.net
/home/wwwroot/exehack.net
├── bbs
│   └── index.html
└── www
    └── index.html
2 directories, 2 files

/home/wwwroot/exehack.net为nginx的安装目录下默认的存放源代码的路径。
bbs为论坛程序源代码路径;www为主页程序源代码路径;把相应程序放入上面的路径通过;http://www.exehack.net 访问的就是主页http://bbs.exehack.net 访问的就是论坛,其它二级域名类推。
有2种方法,推荐方法一

 server {
listen 80;
server_name ~^(?<subdomain>.+).exehack.net$;
access_log /data/wwwlogs/exehack.net_nginx.log combined;
index index.html index.htm index.php;
root /home/wwwroot/linuxeye/$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 7d;
  }
}

方法二、

 server {
listen 80;
server_name *.exehack.net;
access_log /home/wwwlogs/exehack.net_nginx.log combined;
index index.html index.htm index.php;
if ($host ~* ^([^\.]+)\.([^\.]+\.[^\.]+)$) {
  set $subdomain $1;
  set $domain $2;
}
location / {
  root /home/wwwroot/exehack.net/$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 7d;
  }
}

nginx将泛解析的匹配域名绑定到子目录的配置方法如下

复制代码 代码如下:

server {
    listen        80;
    server_name   domain.com    *.domain.com;

    if ($host ~* ^([^\.]+)\.([^\.]+\.[^\.]+)$) {
        set $subdomain $1;
        set $domain $2;
    }

    location / {
        root    /home/wwwroot/$domain/$subdomain/;
        index   index.php index.html index.htm;
        #include /home/wwwroot/$domain/$subdomain/.ngx.htaccess;
    }

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {
        root   html;
    }

    location ~ \.php$ {
        root           /home/wwwroot/$domain/$subdomain/;
        fastcgi_pass   127.0.0.1:9100;
        fastcgi_index  index.php;
        fastcgi_param  script_filename  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}