Nginx初探
一、什么是Nginx在nginx官方站点是这样描述它的(nginx.org):
NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
NGINX is one of a handful of servers written to address the C10K problem. Unlike traditional servers, NGINX doesn’t> NGINX powers several high-visibility sites, such as Netflix, Hulu, Pinterest, CloudFlare, Airbnb, WordPress.com, GitHub, SoundCloud, Zynga, Eventbrite, Zappos, Media Temple, Heroku, RightScale, Engine Yard, MaxCDN and many others.
Getting StartedCommunity3rd Party ModulesContributing
总结:nginx其功能丰富,既可以作为web服务器,也可以作为反向代理服务,imap/pop3反向代理服务器;能够快速响应静态页面的请求,且支持FastCGI,ssl,URL rewrite ,Gzip等大量功能,并且支持大量的第三方模块扩展。
二、Nginx的特性
1.采用模块化设计,较好的扩展性; 2.高可靠性,nginx基于master/worker;
3.支持热部署,可平滑升级到新版本;
4.低内存消耗,10000个keep-alive连接模式下的非活动连接仅消耗2.5M内存;
5.支持异步非阻塞模式及事件驱动模型,每个进程处理多个请求;
6.内存映射:由进程内存直接映射磁盘上的内容至内存
三、Nginx模块化结构
nginx的模块化结构包括核心模块,标准模块,第三方模块
核心模块:核心模块是nginx正常运行不可缺少的模块,主要为nginx提供最基本的核心模块,包括进程管理,权限管理等
标准模块:在编译安装后所包含的模快,包括可选http模块,邮件服务模块等
第三方模块:第三方模块由各程序人员或公司开源出来的自研模块;nginx站点第三方模块地址:https://www.nginx.com/resources/wiki/modules/
四、Nginx服务器进程
nginx服务进程分为三大类:
1:Master主进程:主进程主要功能用于管理内部其他进程及平滑升级功能
2:Worker工作进程:由主进程生成,生成数量可有配置文件进行指定,其主要功能用于响应其请求
3:缓存相关进程主要分为两类:1)cache loader :载入缓存对象;2)cache manager 管理缓存对象
五、Nginx服务器的web请求处理机制
Nginx在处理请求时采用了异步非阻塞及事件驱动模型方式;
具体过程
当某个工作进程接收到客户端发送的请求以后,调用IO进行处理,如果不能立即得到结果,将请求放入一个待处理的事件列表,使用非阻塞IO方式调用“事件处理器”来处理该请求,事件驱动模型让工作进程可以同时处理多个请求,也不必关心IO具体状态,IO的具体调用完全是由事件驱动模型来管理。待IO调用完毕后再通知工作进程事件准备就绪,当该工作进程收到通知后会暂时挂起当前处理事务,去响应客户端的请求。
事件处理库又被成为多路复用,最常见的就包括以下三种;select;poll;epoll
epoll库是被公认为最优秀的事件驱动模型,epoll库通过调用通知内核创建一个有N个描述符的事件列表,然后给这些描述符设置所关注事件,并将其添加到内核事件列表中去。其他事件处理模型请查看本文附件‘事件驱动模型种类’;
使用异步非阻塞及时间驱动模型方式减少了对工作进程在IO调用上的阻塞延迟及进程占用的压力。大大加速了Nginx的响应和处理能力
六、安装使用Nginx
1、安装开发包
yum install -y "Development Tools"
2、解决依赖
yum install -y pcre-devel openssl-devel zlib-devel
[*] pcre-devel用于URL rewrite ,需要使用到正则表达式
[*] zlib-devel用于提供数据传送送可以支持压缩
[*] openssl-devel用于支持https
3、创建用户
创建系统用户nginx
useradd -r nginx
4、准备源码包:
wgethttp://nginx.org/download/nginx-1.8.0.tar.gz
5、安装
1)解压
tar -xf nginx-1.8.0.tar.gz
2)检查安装环境及自定义安装模块并生成Makefile
./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio 注:centos 6不支持http_v2
3)编译依赖的源码,生成所有相关的目标文件
make
4)将可执行程序安装
make install
5)创建执行环境
/etc/profile
export PATH=/usr/local/nginx/sbin:$PATH
6、nginx服务的启动(注意:在这之前一定要关掉其他占用同端口80程序)
# nginx -h
nginx version: nginx/1.8.0
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help 显示帮助信息
-v : show version and exit显示其版本
-V : show version and configure options then exit显示器版本信息及配置
-t : test configuration and exit测试配置正确性
-q : suppress non-error messages during configuration testing测试配置只显示错误
-s signal : send signal to a master process: stop, quit, reopen,> -p prefix : set prefix path (default: /usr/local/nginx/)指定服务器路径前缀至文件路径
-c filename : set configuration file (default: /etc/nginx/nginx.conf)指定配置文件路径
-g directives : set global directives out of configuration file附加配置文件路径指定
nginx服务启动可直接执行二进制文件./nginx
重载服务:nginx -s> 7、Nginx的信号机制:
TERM, INT 快速关闭
QUIT
从容关闭
HUP
重新加载,用新的配置开始新的工作进程
USER1
重新打开日志文件
USER2
平滑升级可执行程序
WINCH
从容关闭工作进程
# ps -aux|grep nginx
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 34570.00.0446281756 ? Ss Oct15 0:00 nginx: master process ./nginx
nginx 111570.00.0450721792 ? S 16:58 0:00 nginx: worker process
root 112210.00.0 103332 860 pts/2 S+ 17:17 0:00 grep nginx
# kill -quit 3457
# ps -aux|grep nginx
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 112252.00.0 103328 856 pts/2 S+ 17:17 0:00 grep nginx
页:
[1]