[TOC]

在配置Nginx的时候其实有很多东西可以学习的呢。

基础了解

  1. 配置中的http{ server{ location{ }}}分别表示为HTTP服务器提供配置上下文,为其中一个虚拟主机提供配置(可配多个),为某个URI提供配置(可配多个)。

  2. root为请求设置根目录,如

    location /i/ {
        root /data/w3;
    }
    

    把URI中/i/a.php响应到/data/w3/i/a.php上来。当server和location同时存在root的时候,会遵循一个继承的机制,底层继承高层,底层覆盖高层。

  3. server中应该包含如下内容,为Nginx提供PHP服务

    location ~ .php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
     }
    

为泛域名配置二级域名多域名

  1. 去域名解析商添加泛域名*解析到IP

  2. 添加如下关键代码,根据二级域名响应到对应的文件夹

    server {
        server_name ~^(?<subdomain>.+).dulcim.com$;
        root /data/wwwroot/html/$subdomain;
    }
    

YII中实现路由匹配

如果路径不是真实存在,就rewrite到index.php脚本。据说这种方法需要pathinfo所以有一点性能问题,而且有一个安全问题,还没时间研究。

location / {
    if (!-e $request_filename){
        rewrite ^/(.*) /index.php last;
    }
}

参考文档

  1. Nginx配置模块文档
  2. Rewrite基本语法