nginx泛解析

网站的目录结构为:

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;
     }
 }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • 第一章 Nginx简介 Nginx是什么 没有听过Nginx?那么一定听过它的“同行”Apache吧!Ngi...
    JokerW阅读 32,795评论 24 1,002
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,285评论 25 708
  • 朋友圈经济欣欣向荣 吃喝玩乐应有尽有 工作创业两不误 唯我独坐窗台 看车水马龙 思绪万千 恨自己 无为 怨 人间 ...
    Mrwsc阅读 140评论 0 0
  • 忙忙碌碌的日子已经在记忆里模糊了。淅淅沥沥的雨让我忘记了这是个夏天。高三的日子曾经充斥着生活啊,现在不但不...
    如果华晨宇是光阅读 263评论 1 1