其实初识Nginx,我们知道它是一个类似Apache的HTTP服务器。
那么如何像Apache那样用Nginx展示一个页面呢?
关于Nginx的配置信息都是存放在安装目录的conf/nginx.conf中的,这个和Apache中的conf/http.conf是类似的。
启动Nginx的方式很简单,这里的论述主要侧重于配置方面。
为了满足展示页面这一简单要求,需要在nginx.conf中定义一个虚拟主机。
它的声明信息需要写在http段中。
如下:
- server{
- listen 80;
- server_name 192.168.116.254 ServerCenter;
- index index.html index.htm index.jsp index.php;
- root /data/www/miffy/ServerCenter;
- charset utf-8;
- access_log logs/ServerCenter.access.log main;
- location / {
- index index.html index.htm index.php;
- }
- }
就像上面定义的那样,存放网站文件的根目录位于:/data/www/miffy/ServerCenter,它的首页文件默认是index.html/index.htm/index.jsp/index.php中任意一个。
- [root@ServerCenter ServerCenter]# pwd
- /data/www/miffy/ServerCenter
- [root@ServerCenter ServerCenter]# ls
- [root@ServerCenter ServerCenter]#
就像这里呈现的这样,如果这个路径下没有任何文件,或者这个路径不存在,那么Nginx都会默认把对网站(虚拟主机)的访问引导向一个错误的反馈页面:
如果:
- [root@ServerCenter ServerCenter]# pwd
- /data/www/miffy/ServerCenter
- [root@ServerCenter ServerCenter]# cat wrong
- Good morning, Mr.Right
- [root@ServerCenter ServerCenter]#
像这样:其中包含的文件是错误的(非nginx.conf中的index定义),你一样还是会得到上面的“403 Forbidden”的Error Page。
- [root@ServerCenter ServerCenter]# cp wrong index.html
- [root@ServerCenter ServerCenter]# cat index.html
- Good morning, Mr.Right
- [root@ServerCenter ServerCenter]#
直到,你按照它的要求创建了index.html,网页才可以正常的被展示出来。
That is the "Hello world" in Nginx i told before we started。