nginx下配置二级域名指向子目录

今天终于把nginx的二级域名配置搞定了,哎之前在测试服务器上弄过一次,不过那个是在本地解析的hosts,把ip指向到域名上就ok,再在nginx.conf里改了下配置就好了,用同样的方法改了正式服务器上的nginx.conf(忘了正式服务器的是域名,这个域名是要DNS解析的)导致耗费了N多时间哇

谨记谨记!如果查了N多的资料,配置文件么有问题,记得DNS解析!

我是一个二级域名写了一个conf,在nginx.conf里引进了所有.conf结尾的文件include 你的配置目录/nginx/conf/vhosts/*.conf;


server {
  listen 80;
  server_name 二级域名;

  if ( $http_host ~* "^(.*?).test.com$")
  {
    set $domain $1;
  }
  location / {
    index index.php index.html index.htm;
    root 二级域名指向的目录;
  }

}

2015-12-2再次修改:

test.baidu.com指向根目录下的一个子目录

server {
        listen       80;
        server_name  test.baidu.com;
         
        location / {
            root   /根目录;
            index  index.php index.html index.htm;
               if (!-e $request_filename) {//这里是把index.php隐藏掉,这样才可以使用路由
                 rewrite ^(.*)$ /index.php?s=$1 last;
                 break;
               }
        }
    
          location ~ .php$ {
            root                www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /根目录/$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

错误记录:
同事的mac上装的lnmp环境,刚来配置虚拟主机,最开始出现404是因为没有rewrite把index.php隐藏掉,然后按上面的办法把root和index一起放在location里了,然后重启试过后,出现file not found和我在测服上的配置文件相比,发现不同之处:
1、他的是include fastcig.conf而我的是include fastcgi_params;网上查了后,说差异不大,只是conf文件里把scripte_filename定义好了,
2、他的root之前是在外面定义好的,我移到location里了,说明fastcgi.conf文件没有找到root,把root移到location外面就好了
我测服的虚拟主机是一起放在nginx.conf里,里面多个server,他的是分成文件,在nginx里包含进来,这个应该就是区别了吧
nginx配置文件分为好多块,常见的从外到内一次是[http]、[server]、[location]等等,就是说内层块是自动获取外层块的值作为缺省值
 
参考地址:http://jingyan.baidu.com/article/ca41422fe36c261eae99ed9f.html
原文地址:https://www.cnblogs.com/efan/p/3940122.html