一、Nginx简介

    Nginx("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004104日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

    它已经在众多流量很大的俄罗斯网站上使用了很长时间,这些网站包括YandexMail.RuVKontakte,以及Rambler。据Netcraft统计,在20128月份,世界上最繁忙的网站中有11.48%使用Nginx作为其服务器或者代理服务器。目前互联网主流公司360、百度、新浪、腾讯、阿里等,目前中国互联网企业70%以上公司都在使用nginx作为自己的web服务器。

    Nginx特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。

Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location blocklocationNginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。(以上简介摘自网络)

Nginx相对于Apache优点:

1)  高并发响应性能非常好,官方Nginx处理静态文件并发5w/s

2)  反向代理性能非常强。(可用于负载均衡)

3)  内存和cpu占用率低。(为Apache1/5-1/10

4)  对后端服务有健康检查功能。

5)  支持PHP cgi方式和fastcgi方式。

6)  配置代码简洁且容易上手。


二、Nginx的工作原理

Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location blocklocationNginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。

Nginx的模块从结构上分为核心模块、基础模块和第三方模块:

核心模块HTTP模块、EVENT模块和MAIL模块

基础模块:HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块,

第三方模块:HTTP Upstream Request Hash模块、Notice模块和HTTPAccess Key模块。

Nginx的高并发得益于其采用了epoll模型,与传统的服务器程序架构不同,epolllinux内核2.6以后才出现的。Nginx采用epoll模型,异步非阻塞,而apache采用的是select模型:

Select特点:select 选择句柄的时候,是遍历所有句柄,也就是说句柄有事件响应时,select需要遍历所有句柄才能获取到哪些句柄有事件通知,因此效率是非常低。

epoll的特点:epoll对于句柄事件的选择不是遍历的,是事件响应的,就是句柄上事件来就马上选择出来,不需要遍历整个句柄链表,因此效率非常高。


三、Nginx的安装

1、安装环境依赖包

[root@nginx ~]# yum -y install pcre pcre-devel gcc openssl-devel

2、下载Nginx并安装

[root@nginx ~]# wget http://nginx.org/download/nginx-1.6.3.tar.gz
[root@nginx ~]# tar -zxf nginx-1.6.3.tar.gz
[root@nginx ~]# cd nginx-1.6.3
[root@nginx nginx-1.6.3]# ./configure  --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@nginx ~]#make && make  install
[root@nginx nginx-1.6.3]#/usr/local/nginx/sbin/nginx   ##启动Nginx
[root@nginx nginx-1.6.3]# ps -ef | grepnginx
root     4432     1  0 22:12 ?        00:00:00 nginx: master process/usr/local/nginx/sbin/nginx
nobody   4433  4432  0 22:12 ?        00:00:00 nginx: worker process     
root     4437  2037  0 22:12 pts/2    00:00:00 grep nginx
[root@nginx nginx-1.6.3]#

wKioL1VTe5nCLvmAAAIPe-Hz3kA424.jpg

Nginx安装成功!


四、Nginx虚拟主机的配置

[root@nginx conf]# vim/usr/local/nginx/conf/nginx.conf

http下加入一行

include vhosts.conf;

/usr/local/nginx/conf下新建vhosts.conf并添加以下内容。

[root@nginx conf]# cat vhosts.conf
server {
   listen       80;
   server_name  www.a.com;
 
   location / {
       root   /usr/local/nginx/html/a;
       index  index.html index.htm;
    }
}
 
server {
   listen       80;
   server_name  www.b.com;
 
   location / {
       root   /usr/local/nginx/html/b;
       index  index.html index.htm;
    }
}
 
server {
   listen       80;
   server_name  www.c.com;
 
   location / {
       root   /usr/local/nginx/html/c;
       index  index.html index.htm;
    }
}
[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# mkdir {a,b,c}
[root@nginx html]# echo aaaaaa  > a/index.html
[root@nginx html]# echo bbbbbb >b/index.html
[root@nginx html]# echo cccccc >c/index.html
[root@nginx conf]#/usr/local/nginx/sbin/nginx -t
nginx: the configuration file/usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file/usr/local/nginx/conf/nginx.conf test is successful
[root@nginx conf]#
[root@nginx conf]#/usr/local/nginx/sbin/nginx -s reload
[root@nginx conf]#

在客户端配置hosts(C:\Windows\System32\drivers\etc\hosts)

hosts中加入一行

192.168.1.2 www.a.com www.b.com www.c.com


wKiom1VTeqjAGdHkAABSY_s1zj8895.jpg

wKioL1VTfCTycWvxAABhQP0wlnU877.jpg

wKiom1VTeqqRAfCPAABdB-CTum8350.jpg

虚拟主机配置完成!

上边的虚拟主机是最最简单的配置,每个虚拟主机里边都可以定义或加载很多东西,将在后续章节中介绍!