其实初识Nginx,我们知道它是一个类似Apache的HTTP服务器。

那么如何像Apache那样用Nginx展示一个页面呢?

关于Nginx的配置信息都是存放在安装目录的conf/nginx.conf中的,这个和Apache中的conf/http.conf是类似的。

启动Nginx的方式很简单,这里的论述主要侧重于配置方面。

为了满足展示页面这一简单要求,需要在nginx.conf中定义一个虚拟主机。

它的声明信息需要写在http段中。

如下:

 
  1. server{  
  2.    listen 80;  
  3.    server_name 192.168.116.254 ServerCenter;  
  4.    index index.html index.htm index.jsp index.php;  
  5.    root /data/www/miffy/ServerCenter;  
  6.    charset utf-8;  
  7.    access_log logs/ServerCenter.access.log main;  
  8.  
  9.    location / {  
  10.       index index.html index.htm index.php;  
  11.    }  

就像上面定义的那样,存放网站文件的根目录位于:/data/www/miffy/ServerCenter,它的首页文件默认是index.html/index.htm/index.jsp/index.php中任意一个。

 
  1. [root@ServerCenter ServerCenter]# pwd  
  2. /data/www/miffy/ServerCenter  
  3. [root@ServerCenter ServerCenter]# ls  
  4. [root@ServerCenter ServerCenter]# 

就像这里呈现的这样,如果这个路径下没有任何文件,或者这个路径不存在,那么Nginx都会默认把对网站(虚拟主机)的访问引导向一个错误的反馈页面:

如果:

 
  1. [root@ServerCenter ServerCenter]# pwd  
  2. /data/www/miffy/ServerCenter  
  3. [root@ServerCenter ServerCenter]# cat wrong   
  4. Good morning, Mr.Right  
  5. [root@ServerCenter ServerCenter]# 

像这样:其中包含的文件是错误的(非nginx.conf中的index定义),你一样还是会得到上面的“403 Forbidden”的Error Page。

 
  1. [root@ServerCenter ServerCenter]# cp wrong index.html  
  2. [root@ServerCenter ServerCenter]# cat index.html   
  3. Good morning, Mr.Right  
  4. [root@ServerCenter ServerCenter]# 

直到,你按照它的要求创建了index.html,网页才可以正常的被展示出来。

That is the "Hello world" in Nginx i told before we started。