Nginx基本功能可以分為基本的HTTP服務、高階HTTP服務和郵寄服務三大類。
Nginx提供的基本HTTP服務,可以作為HTTP代理伺服器和反向代理伺服器,透過快取加速訪問;透過FastCGI,uwsgi,SCGI等提供快取,支援負載均衡和容災;支援SSL和TLS SNI協議等。
Nginx提供高階的HTTP服務,基於名稱和IP的虛擬伺服器;基於IP、密碼、子請求的訪問控制;支援FLV和MP4媒體傳輸;嵌入perl語言等。
Nginx作為郵件代理伺服器支援IMAP、POP3代理服務功能;透過外部HTTP身份驗證伺服器進行使用者身份驗證,並將連線重定向到內部SMTP伺服器。
為什麼選擇Nginx,會必不可少地和Apache伺服器做個比較,WEB伺服器鼻祖Apache採用的是同步阻塞型的、多行程模型,一個連線對應一個行程,高併發下會消耗大量記憶體,行程間切換也會消耗大量CPU資源
;Nginx 採用的是非同步非阻塞工作模型,具備高併發、低資源消耗的特性,高度模組化設計使 Nginx
具備很好的擴充套件性,在處理靜態檔案、反向代理請求等方面,Nginx 表現出很大的優勢。
從netcraft網站最新統計情況可以看到Nginx份額逐漸在升高。已經逐漸接近Apache佔用量。

首先,Nginx支援高併發的同時資源消耗相比其他的伺服器要低很多,據統計一般情況下10000個非活躍的HTTP連線在nginx中僅消耗2.5M記憶體,單機峰值能達到10W連線。
支援熱部署,Master管理行程和worker工作行程的分離設計,使得Nginx能夠提供熱部署功能。
高可靠效能各大網站選擇Nginx的基本條件,很多家高流量的網站核心伺服器上都大規模使用Nginx。
BSD協議許可,不止允許免費使用,而且允許使用者在自己的專案中直接使用或修改nginx原始碼。
Nginx優點不止於此,期待各位的發掘。
可在http://nginx.org/en/download.html官方網站下載nginx各個版本。可以看到有主流版本(Mainline
version),也可以說是開發版本,穩定版本(Stableversion)和過期版本(Legacy versions)。

編譯Nginx原始碼,需要標準的gcc編譯器,還需要automake完成竄國家makefile的工作,依賴一些第三方庫,通常有pcre庫(支援rewrite模組)、zlib庫(支援gzip模組)、openssl庫(支援ssl模組)。
yum -y install gcc automake make pcre-devel zlib-devel openssl-devel
下載檔案後解壓可以看到目錄如下:
[root@lsl-test1 nginx-1.16.0]$ ls -ltotal 752drwxr-xr-x. 6 1001 1001 4096 Jun 18 23:19 auto-rw-r--r--. 1 1001 1001 296223 Apr 23 21:13 CHANGES-rw-r--r--. 1 1001 1001 451813 Apr 23 21:13 CHANGES.rudrwxr-xr-x. 2 1001 1001 168 Jun 18 23:19 conf-rwxr-xr-x. 1 1001 1001 2502 Apr 23 21:12 configuredrwxr-xr-x. 4 1001 1001 72 Jun 18 23:19 contribdrwxr-xr-x. 2 1001 1001 40 Jun 18 23:19 html-rw-r--r--. 1 1001 1001 1397 Apr 23 21:12 LICENSEdrwxr-xr-x. 2 1001 1001 21 Jun 18 23:19 man-rw-r--r--. 1 1001 1001 49 Apr 23 21:12 READMEdrwxr-xr-x. 9 1001 1001 91 Jun 18 23:19 src
Auto目錄放著大量指令碼檔案和configure指令碼程式相關:
- CHANGES檔案存放著本版本的改變,CHANGE.ru是俄語的說明,因為Nginx是俄羅斯人開發的;
- conf目錄放著配置檔案;
- configure檔案是Nginx軟體的自動指令碼程式,檢察環境和生成編譯程式碼需要的makefile檔案;
- contrib存放著幾個特殊指令碼,目錄下readme有說明;
- html存放著兩個.html的靜態網頁檔案;
- man存放了Nginx軟體說明檔案;
- src目錄存放了Nginx軟體的原始碼。
可以看下配置的幫助,每個引數後面都有英文解釋
[]$ ./configure --help--help print this message--prefix=PATH set installation prefix--sbin-path=PATH set nginx binary pathname--modules-path=PATH set modules path--conf-path=PATH set nginx.conf pathname--error-log-path=PATH set error log pathname--pid-path=PATH set nginx.pid pathname--lock-path=PATH set nginx.lock pathname--user=USER set non-privileged user for worker processes--group=GROUP set non-privileged group for worker processes--build=NAME set build name--builddir=DIR set build directory......
使用configure指令碼生成make檔案
[root-test1 nginx-1.16.0]$./configure --sbin-path=/nginx/nginx --conf-path=/nginx/nginx.conf --user=nginx
使用make進行原始碼編譯
make
make install安裝Nginx
make install
編譯安裝完成之後啟動Nginx
[root@lsl-test1 contrib]$ nginx[root@lsl-test1 contrib]$ps -ef|grep nginxroot 8264 1 0 23:30 ? 00:00:00 nginx: master process nginxnginx 8265 8264 0 23:30 ? 00:00:00 nginx: worker processroot 9068 2726 0 23:47 pts/1 00:00:00 grep --color=auto nginx
可以使用-?或-h檢視Nginx具體命令引數
[root@lsl-test1 ~]$ nginx -hnginx version: nginx/1.15.12Usage: nginx [-?hvVtTq] [-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-T : test configuration, dump it 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: /etc/nginx/)-c filename : set configuration file (default: /etc/nginx/nginx.conf)-g directives : set global directives out of configuration file
編譯後的Nginx主要檔案組成有Nginx二進位制可執行檔案,nginx.conf配置檔案,access.log訪問日誌,error.log錯誤日誌。
這裡主要說下nginx.conf配置檔案,Nginx和它的模組工作方式由配置檔案決定,配置檔案預設名為nginx.conf ,放在/usr/local/nginx/conf, /etc/nginx,或者/usr/local/etc/nginx目錄下。
下麵是Nginx初始化預設的一個配置檔案
user nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;# Load dynamic modules. See /usr/share/nginx/README.dynamic.include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;}http {log_format main '$remote_addr - $remote_user [$time_local] "$request" '$body_bytes_sent "$http_referer" '"$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;include /etc/nginx/mime.types;default_type application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;server {listen 80 default_server;listen [::]:80 default_server;server_name _;root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}...}
配置檔案由指令和指令塊構成,每條指令以分號結尾,指令和引數間以空格分分隔,指令快以大括號{}將所多條指令組織到一起,include陳述句可以組合多個配置檔案。使用#新增註釋,提高可隊形。使用$符號獲取變數。
下麵我們看看nginx.conf各個指令塊的作用。
全域性塊是預設配置檔案從開始到events塊之間的一部分,例如下麵節選的部分預設配置檔案。主要設定一些影響Nginx伺服器整體執行的配置質量,通常包括執行的使用者,工作行程數,pid存放路徑,日誌路徑及配置檔案等。
user nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;# Load dynamic modules. See /usr/share/nginx/README.dynamic.include /usr/share/nginx/modules/*.conf;
Events塊設計指令主要是影響Nginx與使用者網路連線,主要包括是否允許同時接收多個網路連線,選擇哪個事件驅動模型處理請求,每個工作行程支援的最大連線數等。
Http指令塊是Nginx伺服器配置中的重要部分。Http指令塊中可以包含自己的全域性塊,也可以包含多個server指令塊,server指令塊又可以包含location指令塊。Http全域性塊中可以定義MIME_TYPE,檔案引入,自定義日誌、連線超時時間、連線請求上限數量等。
Server指令塊可以包含多個location指令塊,最常見的配置是監聽埠配置和服務名稱或者IP地址。
Location指令塊可以對特定請求進行處理。地址的定向、資料快取和應答控制都在這部分實現。
Nginx後臺行程有一個主行程,和若干個工作行程。主行程的作用是讀取和評估配置,並維護工作行程。工作行程處理請求。Nginx依賴事件模型和依靠作業系統機制,有效地分發工作行程間的請求。工作行程數量配置在配置檔案中,並且可以修改為給定的配置或者根據CPU核數動態調整行程數。
[root@lslt ~]# ps -ef|grep nginx |grep -v greproot 21960 1 0 Jun12 ? 00:00:00 nginx: master process /usr/sbin/nginxnginx 21961 21960 0 Jun12 ? 00:00:00 nginx: worker processnginx 21962 21960 0 Jun12 ? 00:00:00 nginx: worker processnginx 21963 21960 0 Jun12 ? 00:00:00 nginx: worker processnginx 21964 21960 0 Jun12 ? 00:00:00 nginx: worker process
可以看到實際執行行程工作行程和配置檔案數量是一致的。
[]worker_processes 4;worker_connections 1024;
Nginx為什麼採用管理行程和工作行程分開的樣式呢?主要好處有以下兩點:
關於 zCloud
zCloud在實現資料庫資源雲化的基礎上,透過標準化、自動化、智慧化實現資料庫的自動化和智慧化運維,減少大型企業中資料庫維護人員的日常運維工作,讓資料庫運維人員減少被動性救火工作,增強資料庫執行的穩定性以及提高效能,以達到資料庫雲平臺對資料庫服務的SLA要求。
zCloud目前已經在金融、運營商、政府等多個領域有了最佳實踐案例,幫助組織構建平穩、高效的資料庫管理平臺。
知識星球
朋友會在“發現-看一看”看到你“在看”的內容