Nginx泛解析匹配绑定域名子目录
网站的目录结构为:
1# tree /data1/wwwroot/361way.com
2/data1/wwwroot/361way.com
3├── bbs
4│ └── index.html
5└── www
6 └── index.html
72 directories, 2 files
/data1/wwwroot/361way.com为nginx的安装目录下默认的存放源代码的路径。
- bbs为论坛程序源代码路径
- www为主页程序源代码路径
把相应程序放入上面的路径通过
- https://blog.361way.com 访问的就是主页
- http://bbs.361way.com 访问的就是论坛
其它二级域名类推。
实现方法有两种。
方法一:
1server {
2listen 80;
3server_name ~^(?<subdomain>.+).361way.com$;
4access_log /logs/www/361way.com_nginx.log combined;
5index index.html index.htm index.php;
6root /data1/wwwroot/shop/$subdomain/;
7location ~ .php$ {
8 fastcgi_pass unix:/dev/shm/php-cgi.sock;
9 fastcgi_index index.php;
10 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
11 include fastcgi_params;
12 }
13location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
14 expires 30d;
15 }
16location ~ .*\.(js|css)?$ {
17 expires 7d;
18 }
19}
方法二:
1server {
2listen 80;
3server_name *.361way.com;
4access_log /logs/www/361way.com_nginx.log combined;
5index index.html index.htm index.php;
6if ($host ~* ^([^\.]+)\.([^\.]+\.[^\.]+)$) {
7 set $subdomain $1;
8 set $domain $2;
9}
10location / {
11 root /data1/wwwroot/$domain/$subdomain/;
12 index index.php index.html index.htm;
13}
14location ~ .php$ {
15 fastcgi_pass unix:/dev/shm/php-cgi.sock;
16 fastcgi_index index.php;
17 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
18 include fastcgi_params;
19 }
20location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
21 expires 30d;
22 }
23location ~ .*\.(js|css)?$ {
24 expires 7d;
25 }
26}
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/nginx-subdomain/4730.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.