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 rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load. Even if you don’t expect to handle thousands of simultaneous requests, you can still benefit from NGINX’s high-performance and small memory footprint. NGINX scales in all directions: from the smallest VPS all the way up to large clusters of servers.


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、準備源碼包:

wget  http://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程序)

[root@localhost sbin]# 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, reload 向主進程發起信號

  -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 reload



7、Nginx的信號機制:

 

TERM, INT

快速關閉

QUIT

從容關閉

HUP

重新加載,用新的配置開始新的工作進程

USER1

重新打開日誌文件

USER2

平滑升級可執行程序

WINCH

從容關閉工作進程

 

[root@localhost ~]# ps -aux|grep nginx

Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ

root      3457  0.0  0.0  44628  1756 ?        Ss   Oct15   0:00 nginx: master process ./nginx

nginx    11157  0.0  0.0  45072  1792 ?        S    16:58   0:00 nginx: worker process

root     11221  0.0  0.0 103332   860 pts/2    S+   17:17   0:00 grep nginx

[root@localhost ~]# kill -quit 3457

[root@localhost ~]# ps -aux|grep nginx

Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ

root     11225  2.0  0.0 103328   856 pts/2    S+   17:17   0:00 grep nginx







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章