- I/O模型
- Nginx介紹
- Nginx的安裝和目錄結構
- Nginx的配置
- Nginx的編譯安裝
一、I/O模型
(一)I/O的基本概念
-
I/O過程的兩個階段
- 第一步:將數據從硬件加載至內核內存空間(緩沖區),等待數據準備完成,時間較長
- 第二步:將數據從內核緩沖區復制到用戶空間的進程的內存中,時間較短
-
同步與異步:消息通知機制
- 同步(synchronous):調用者主動等待被調用者返回其運行狀態信息
- 異步(asynchronous):被調用者通過狀態、通知、回調機制主動通知調用者其運行狀態
-
阻塞與非阻塞:調用者等待結果返回前的狀態
- 阻塞(blocking):I/O操作徹底完成后才返回用戶空間,調用結果返回之前,調用者被掛起
- 非阻塞(nonblocking):調用I/O操作后立即返回用戶一個狀態值,最終調用結果返回之前,調用者不會被掛起
(二)I/O模型
- 分類:阻塞型、非阻塞型、復用型、信號驅動型、異步型
(1)同步阻塞I/O模型
- 用戶線程在內核進行I/O操作時被阻塞
- 整個I/O請求的過程中,用戶線程是被阻塞的,這導致用戶在發起I/O請求時,不能做任何事情,對CPU的資源利用率不夠
(2)同步非阻塞I/O模型
- 用戶線程需要不斷地發起I/O請求,直到數據到達后,才真正讀取到數據,繼續執行。即“輪詢”機制
- 比較浪費CPU的方式,一般很少直接使用這種模型
(3)I/O多路復用模型
- 進程阻塞在select或者poll這兩個系統調用上,而不是阻塞在真正的I/O操作上
- 由于使用了會阻塞線程的select系統調用,因此I/O多路復用只能稱為異步阻塞I/O模型,而非真正異步I/O
- I/O多路復用是最常使用的I/O模型
(4)信號驅動I/O模型
- 等待數據報到達期間進程不被阻塞。用戶主程序可以繼續執行,只要等待來自信號處理函數的通知
- 該模型并不常用
(5)異步I/O模型
- 內核通知用戶進程I/O操作完成
- 信號驅動I/O當內核通知觸發信號處理程序時,信號處理程序還需要阻塞在從內核空間緩沖區拷貝數據到用戶空間緩沖區這個階段,而異步IO直接是在第二個階段完成后內核直接通知可以進行后續操作了
- 該模型并不常用
(6)五種I/O模型對比
(三)I/O模型的具體實現
(1)Linux系統實現
- Select:實現I/O復用模型,BSD 4.2最早實現
- Poll:實現I/O復用模型,System V最早實現
- Epoll:實現I/O復用模型,具有信號驅動I/O模型的某些特性
(2)Select
- POSIX規定,良好的跨平臺支持
- 監聽端口的數量有限:
cat /proc/sys/fs/file-max
- 對socket是線性掃描,即采用輪詢的方法,效率較低
- 采取了內存拷貝方法來實現內核將FD消息通知給用戶空間
(3)Poll
- 本質上和select沒有區別
- 沒有最大連接數的限制
- 水平觸發
(4)Epoll
- 在Linux 2.6內核中提出的select和poll的增強版本
- 沒有最大并發連接的限制
- 非輪詢的方式,只管理“活躍”的連接,而跟連接總數無關
- 使用mmap減少復制開銷
二、Nginx介紹
(一)Nginx提供的功能:
- 靜態資源的web服務器
- 結合FastCGI/uWSGI/SCGI等協議反向代理動態資源請求
- http協議反向代理服務器
- pop3/imap4協議反向代理服務器
- tcp/udp協議的請求轉發
(二)Nginx服務器的架構
(1)Nginx程序架構:Master/Worker結構
- 一個master進程
負載加載和分析配置文件、管理worker進程、平滑升級 - 一個或多個worker進程
處理并響應用戶請求
(2)Nginx模塊
- 高度模塊化,新版本支持DSO機制,實現動態加載/卸載模塊
- 模塊分類:
- 核心模塊:core module
- 標準模塊:
- HTTP 模塊:ngx_http_*
HTTP Core modules 默認功能
HTTP Optional modules 需編譯時指定 - Mail模塊:ngx_mail_*
- Stream 模塊:ngx_stream_*
- HTTP 模塊:ngx_http_*
- 第三方模塊
三、Nginx的安裝和目錄結構
(一)Nginx的安裝:三種方法
- 官網上下載rpm包,使用yum安裝
- 通過EPEL源安裝
- 編譯安裝
(二)Nginx的目錄結構
- /etc/nginx/nginx.conf:主配置文件
- /usr/lib/systemd/system/nginx.service:Nginx服務
- /usr/lib64/nginx/modules:存放模塊
- /usr/sbin/nginx:主程序
- /usr/share/nginx/html/:web服務文件的默認存放位置
(三)nginx指令:
- 格式:
nginx [options]
默認沒有選項為啟動nginx服務
-h:查看幫助選項
-t:測試nginx語法錯誤
-c filename:指定配置文件(default: /etc/nginx/nginx.conf)
-s signal:發送信號給master進程,signal可為:stop, quit, reopen, reload
-g directives:在命令行中指明全局指令
四、Nginx的配置
(一)配置文件的組成部分
- 主配置文件:/etc/nginx/nginx.conf
- 子配置文件:/etc/nginx/conf.d/*.conf
在主配置文件中使用include 子配置文件路徑;
的格式加載其設置 - fastcgi, uwsgi, scgi等協議相關的配置文件
- mime.types:支持的mime類型
- 配置文件的配置指令格式:
directive value [value2 ...];
,指令必須以分號結尾 - 配置指令支持使用配置變量
- 內建變量:由Nginx模塊引入,可直接引用
- 自定義變量:由用戶使用set命令定義
set variable_namevalue; - 引用變量:$variable_name
(二)配置文件的結構
- 主配置文件結構:四部分
// 主配置段,即全局配置段,對http,mail都有效
main block
event {
... // 事件驅動相關的配置
}
// http, https 協議相關配置段
http {
...
}
// mail 協議相關配置段
mail {
...
}
// stream 服務器相關配置段
stream {
...
}
(三)全局配置段
(1)正常運行必備的配置
user user [group];
指定worker進程的運行身份,如組不指定,默認和用戶名同名
默認為nginxpid /PATH/TO/PID_FILE;
指定存儲nginx主進程PID的文件路徑,服務關閉時文件消失include file | mask;
指明包含進來的其它配置文件片斷load_module file;
模塊加載配置文件:/usr/share/nginx/modules/*.conf
指明要裝載的動態模塊路徑: /usr/lib64/nginx/modules
(2)性能優化相關的配置
worker_processes number | auto;
worker進程的數量,通常應該為當前主機的cpu的物理核心數-
worker_cpu_affinity cpumask...;
worker_cpu_affinity auto [cpumask] 減少進程切換,提高緩存命中率
CPU MASK:以右端為開始從0數起命名CPU,如果啟用則該位為1,不啟用則該位為0,例如“00101010”代表系統共有8顆CPU,啟用第1, 3, 5號CPU- 測試:系統共有4顆CPU,現在要求worker進程綁定第0,2顆CPU
vim /etc/nginx/nginx.conf
worker_processes 2;
worker_cpu_affinity 0100 0001;
systemctl restart nginx
watch -n 0.5 'ps axo pid,cmd,psr,ni | grep nginx'
- worker_priority number;
指定worker進程的nice值,設定worker進程優先級:[-20,20]
- worker_rlimit_nofile number;
worker進程所能夠打開的文件數量上限
(3)事件驅動相關的配置
- 格式:
events {
...
}
worker_connections number;
每個worker進程所能夠打開的最大并發連接數數量
總最大并發數:worker_processes* worker_connectionsuse method;
指明并發連接請求的處理方法,默認自動選擇最優方法
use epoll;accept_mutex on | off;
處理新的連接請求的方法;on指由各個worker輪流處理新請求,off指每個新請求的到達都會通知所有的worker進程,但只有一個進程可獲得連接,造成“驚群”,影響性能,默認on
(4)調試和定位配置
daemon on | off;
是否以守護進程方式運行nignx,默認是守護進程方式master_process on | off;
是否以master/worker模型運行nginx;默認為on,off 將不啟動workererror_log file [level];
錯誤日志文件及其級別
(四)http協議配置段
(1)ngx_http_core_module模塊相關配置
server { ... }:配置虛擬機;
一般在括號內添加listen, server_name, root等配置listen PORT|address[:port] [default_server];
監聽不同端口或者不同IP地址,并且可以設置為默認服務器-
server_name name ...;
虛擬主機的主機名稱后可跟多個由空白字符分隔的字符串
支持*通配任意長度的任意字符
e.g.server_name *.magedu.com www.magedu.*
支持~起始的字符做正則表達式模式匹配,性能原因慎用
e.g.server_name~^www\d+\.magedu\.com$
:\d 表示[0-9]匹配優先級機制從高到低:
(1) 首先是字符串精確匹配如:www.magedu.com
(2) 左側*通配符如:*.magedu.com
(3) 右側*通配符如:www.magedu.*
(4) 正則表達式如:~^.*\.magedu\.com$
(5) default_server需要配合dns使用,實際中很少如此使用
tcp_nodelay on | off;
在keepalived模式下的連接是否啟用TCP_NODELAY選項
當為off時,延遲發送,合并多個請求后再發送
默認on時,不延遲發送,推薦采用默認設置
可用于:http, server, locationsendfile on | off;
是否啟用sendfile功能,在內核中封裝報文直接發送
默認off,推薦采用onserver_tokens on | off | build | string;
是否在響應報文的Server首部顯示nginx版本root
設置web資源的路徑映射;用于指明請求的URL所對應的文檔的目錄路徑,可用于http, server, location, if in location
- 實驗4-1:配置兩個虛擬主機
www.a.com
和www.b.com
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.a.com;
root /app/website1;
}
server {
listen 80;
server_name www.b.com;
root /app/website2;
}
mkdir -p /app/website{1,2}
echo /app/website1/index.html > /app/website1/index.html
echo /app/website2/index.html > /app/website2/index.html
nginx -t
systemctl reload nginx
- 配置DNS或在測試機上修改hosts文件后,訪問指定網址成功
- location [ = | ~ | ~* | ^~ ] uri{ ... }
location @name { ... }
在一個server中location配置段可存在多個,用于實現從uri到文件系統的路徑映射;ngnix會根據用戶請求的URI來檢查定義的所有location,并找出一個最佳匹配,而后應用其配置
匹配符號
=:對URI做精確匹配;
^~:對URI的最左邊部分做匹配檢查,不區分字符大小寫
~:對URI做正則表達式模式匹配,區分字符大小寫
~*:對URI做正則表達式模式匹配,不區分字符大小寫
不帶符號:匹配起始于此uri的所有的uri匹配優先級從高到低:
=, ^~, ~/~*, 不帶符號實驗4-2:在實驗4-1的基礎上,實現當訪問
www.a.com/admin/
時指向的磁盤目錄為/app/website3/admin
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.a.com;
root /app/website1;
location /admin {
root /app/website3;
}
}
server {
listen 80;
server_name www.b.com;
root /app/website2;
}
mkdir -p /app/website3/admin
echo /app/website3/admin/index.html > /app/website3/admin/index.html
systemctl reload nginx
- alias path;
路徑別名,文檔映射的另一種機制;僅能用于location上下文
注意:location中使用root指令和alias指令的意義不同
(a) root:給定的路徑對應于location中的/uri/左側的/
(b) alias:給定的路徑對應于location中的/uri/右側的/實驗4-3:在實驗4-2的基礎上,實現當訪問
www.b.com/admin/
時指向的磁盤目錄為/app/website3
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80 ;
server_name www.a.com;
root /app/website1;
location /admin {
root /app/website3;
}
}
server {
listen 80;
server_name www.b.com;
root /app/website2;
location /admin {
alias /app/website3;
}
}
echo /app/website3/index.html > /app/website3/index.html
systemctl reload nginx
index file ...;
指定默認網頁資源,注意位于ngx_http_index_module模塊error_pagecode ... [=[response]] uri;
模塊:ngx_http_core_module
定義錯誤頁,以指定的響應狀態碼進行響應
可用位置:http, server, location, if in location
- 實驗4-4:在實驗4-3的基礎上,實現當訪問www.a.com不存在的地址時,返回指定的404錯誤文件,并且響應碼應為200
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80 ;
server_name www.a.com;
root /app/website1;
location /admin {
root /app/website3;
}
error_page 404 =200 /404.html;
location /404.html {
root /app/website1/error_page;
}
}
server {
listen 80;
server_name www.b.com;
root /app/website2;
location /admin {
alias /app/website3;
}
}
mkdir /app/website1/error_page
echo /app/website1/error_page/404.html > /app/website1/error_page/404.html
systemctl restart nginx
- try_files file ... uri;
try_files file ... =code;
按順序檢查文件是否存在,返回第一個找到的文件或文件夾(結尾加斜線表示為文件夾),如果所有的文件或文件夾都找不到,會進行一個內部重定向到最后一個參數。只有最后一個參數可以引起一個內部重定向,之前的參數只設置內部URI的指向。最后一個參數是回退URI且必須存在,否則會出現內部500錯誤
- 實驗4-5:在實驗4-4的基礎上,實現訪問www.a.com/txt時,依次在/txt, /txt/index.html, /txt.html路徑查詢是否有相應文件,若都沒有,則返回404錯誤
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80 ;
server_name www.a.com;
root /app/website1;
location /admin {
root /app/website3;
}
location /txt {
try_files $uri $uri/index.html $uri.html =404;
}
error_page 404 =200 /404.html;
location /404.html {
root /app/website1/error_page;
}
}
nginx -t
systemctl reload nginx
echo /app/website1/txt > /app/website1/txt
echo /app/website1/txt.html > /app/website1/txt.html
// 第一次登錄www.a.com/txt
rm -rf /app/website1/txt
mkdir /app/website1/txt
echo /app/website1/txt/index.html > /app/website1/txt/index.html
// 第二次登錄www.a.com/txt
rm -rf /app/website1/txt
// 第三次登錄www.a.com/txt
rm -rf /app/website1/txt.html
// 第四次登錄www.a.com/txt
keepalive_timeout timeout [header_timeout];
設定保持連接超時時長,0表示禁止長連接,默認為75skeepalive_requests number;
在一次長連接上所允許請求的資源的最大數量
默認為100keepalive_disable none | browser ...
對哪種瀏覽器禁用長連接send_timeout time;
向客戶端發送響應報文的超時時長,此處是指兩次寫操作之間的間隔時長,而非整個響應過程的傳輸時長client_body_buffer_size size;
用于接收每個客戶端請求報文的body部分的緩沖區大?。荒J為16k;超出此大小時,其將被暫存到磁盤上的由client_body_temp_path指令所定義的位置client_body_temp_path path [level1 [level2 [level3]]];
設定用于存儲客戶端請求報文的body部分的臨時存儲路徑及子目錄結構和數量
目錄名為16進制的數字;
client_body_temp_path /var/tmp/client_body 1 2 2
1:1級目錄占1位16進制,即2^4=16個目錄0-f
2 :2級目錄占2位16進制,即2^8=256個目錄00-ff
2 :3級目錄占2位16進制,即2^8=256個目錄00-fflimit_rate rate;
限制響應給客戶端的傳輸速率,單位是bytes/second
默認值0表示無限制limit_except method ... { ... },僅用于location
限制客戶端使用除了指定的請求方法之外的其它方法
method: GET, HEAD, POST, PUT, DELETE, MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, PATCH
- 實驗4-6:實現只有192.168.136.229主機可以使用除了GET和HEAD之外的其他方法訪問www.a.com/admin
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80 ;
server_name www.a.com;
root /app/website1;
location /admin {
root /app/website3;
limit_except GET {
allow 192.168.136.229;
deny all;
}
}
}
nginx -s reload
來自192.168.136.229的主機可以使用POST方法
來自192.168.136.129的主機不可以使用POST方法
aio on | off | threads[=pool];
是否啟用aio功能directio size | off;
是否同步(直接)寫磁盤,而非寫緩存,在Linux主機啟用O_DIRECT標記,則文件大于等于給定大小時使用,例如directio 4mopen_file_cache off;
open_file_cache max=N [inactive=time];
nginx可以緩存以下三種信息:
(1) 文件元數據:文件的描述符、文件大小和最近一次的修改時間
(2) 打開的目錄結構
(3) 沒有找到的或者沒有權限訪問的文件的相關信息
max=N:可緩存的緩存項上限;達到上限后會使用LRU算法實現管理
inactive=time:緩存項的非活動時長,在此處指定的時長內未被命中的或命中的次數少于open_file_cache_min_uses指令所指定的次數的緩存項即為非活動項,將被刪除open_file_cache_errors on | off;
是否緩存查找時發生錯誤的文件一類的信息
默認值為offopen_file_cache_min_uses number;
open_file_cache指令的inactive參數指定的時長內,至少被命中此處指定的次數方可被歸類為活動項
默認值為1open_file_cache_valid time;
緩存項有效性的檢查頻率
默認值為60s
(2)ngx_http_access_module模塊相關配置
- 實現基于ip的訪問控制功能
- allow address | CIDR | unix: | all;
- deny address | CIDR | unix: | all;
上下文:http, server, location, limit_except
自上而下檢查,一旦匹配,將生效,條件嚴格的置前
- 實驗4-7:實現只有192.168.136.229主機可以登錄www.a.com
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80 ;
server_name www.a.com;
root /app/website1;
allow 192.168.136.229;
deny all;
}
nginx -s reload
(3)ngx_http_auth_basic_module模塊相關配置
- 實現基于用戶的訪問控制,使用basic機制進行用戶認證
- auth_basic string | off;
- auth_basic_user_file file;
由htpasswd命令(httpd-tools提供)實現加密文本文件
- 實驗4-8:實現使用basic機制進行用戶認證
server {
listen 80 ;
server_name www.a.com ;
root /app/website1;
location /admin {
root /app/website3;
auth_basic "admin auth";
auth_basic_user_file /etc/nginx/conf.d/.auth_passwd;
}
error_page 404 =200 /404.html;
location /404.html {
root /app/website1/error_page;
}
}
nginx -s reload
htpasswd -c /etc/nginx/conf.d/.auth_passwd user1
(4)ngx_http_stub_status_module模塊
- 用于輸出nginx的基本狀態信息
location /status {
stub_status;
}
- 輸出信息:
Active connections:當前狀態,活動狀態的連接數
accepts:統計總值,已經接受的客戶端請求的總數
handled:統計總值,已經處理完成的客戶端請求的總數
requests:統計總值,客戶端發來的總的請求數
Reading:當前狀態,正在讀取客戶端請求報文首部的連接的連接數
Writing:當前狀態,正在向客戶端發送響應報文過程中的連接數
Waiting:當前狀態,正在等待客戶端發出請求的空閑連接數
(5)ngx_http_log_module模塊
指定日志格式記錄請求
log_format name string ...;
string可以使用nginx核心模塊及其它模塊內嵌的變量access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
訪問日志文件路徑,格式及相關的緩沖的配置
- 實驗4-9:指定訪問日志文件的路徑/var/lib/nginx/tmp/nginx-access.log,緩沖區大小16k
vim /etc/nginx/nginx.conf
// log_format必須在http上下文中
log_format compression '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/lib/nginx/tmp/nginx-access.log compression buffer=16k;
nginx -s reload;
- open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
緩存各日志文件相關的元數據信息
max:緩存的最大文件描述符數量
min_uses:在inactive指定的時長內訪問大于等于此值方可被當作活動項
inactive:非活動時長
valid:驗證緩存中各緩存項是否為活動項的時間間隔
(6)ngx_http_gzip_module模塊
用gzip方法壓縮響應數據,節約帶寬
gzip on | off;
啟用或禁用gzip壓縮gzip_comp_level level;
壓縮比由低到高:1 到9
默認:1gzip_disable regex ...;
匹配到客戶端瀏覽器不執行壓縮gzip_min_length length;
啟用壓縮功能的響應報文大小閾值gzip_http_version 1.0 | 1.1;
設定啟用壓縮功能時,協議的最小版本
默認:1.1gzip_buffers number size;
支持實現壓縮功能時緩沖區數量及每個緩存區的大小
默認:32 4k 或16 8kgzip_types mime-type ...;
指明僅對哪些類型的資源執行壓縮操作;即壓縮過濾器
默認包含有text/html,不用顯式指定,否則出錯gzip_vary on | off;
如果啟用壓縮,是否在響應報文首部插入“Vary: Accept-Encoding”gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
nginx對于代理服務器請求的響應報文,在何種條件下啟用壓縮功能
off:對被代理的請求不啟用壓縮
expired, no-cache, no-store, private:對代理服務器請求的響應報文首部Cache-Control值任何一個,啟用壓縮功能
- 實驗4-10 開啟壓縮,響應報文首部插入“Vary: Accept-Encoding”,支持壓縮html, js, css, txt文件
server {
listen 80 ;
server_name www.a.com ;
root /app/website1;
gzip on;
gzip_comp_level 9;
gzip_min_length 32;
gzip_types text/css application/javascript text/plain;
gzip_proxied any;
gzip_vary on;
}
nginx -s reload
(7)ngx_http_ssl_module模塊
ssl on | off;
為指定虛擬機啟用HTTPS protocol,建議用listen指令代替ssl_certificate file;
當前虛擬主機使用PEM格式的證書文件ssl_certificate_key file;
當前虛擬主機上與其證書匹配的私鑰文件ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
支持ssl協議版本,默認為后三個ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
builtin[:size]:使用OpenSSL內建緩存,為每worker進程私有
[shared:name:size]:在各worker之間使用一個共享的緩存ssl_session_timeout time;
客戶端連接可以復用ssl session cache中緩存的ssl參數的有效時長,默認5m
server {
listen 443 ssl ;
server_name www.a.com ;
root /app/website1;
ssl_certificate /etc/nginx/conf.d/ssl/nginx1.crt;
ssl_certificate_key /etc/nginx/conf.d/ssl/nginx1.key;
ssl_session_cache shared:ssl_cache:5m;
ssl_session_timeout 5m;
}
server {
listen 443 ssl ;
server_name www.b.com ;
root /app/website2;
ssl_certificate /etc/nginx/conf.d/ssl/nginx2.crt;
ssl_certificate_key /etc/nginx/conf.d/ssl/nginx2.key;
ssl_session_cache shared:ssl_cache:5m;
ssl_session_timeout 5m;
}
// 建立自簽名證書
cd /etc/pki/tls/certs/
make nginx1.crt
make nginx2.crt
openssl rsa -in nginx1.key -out nginx1.key
openssl rsa -in nginx2.key -out nginx2.key
mkdir -p /etc/nginx/conf.d/ssl
cp nginx* /etc/nginx/conf.d/ssl
nginx -s reload
(8)ngx_http_rewrite_module模塊
- rewrite regex replacement [flag]
將用戶請求的URI基于regex所描述的模式進行檢查,匹配到時將其替換為replacement指定的新的URI
注意:如果在同一級配置塊中存在多個rewrite規則,那么會自上而下逐個檢查;被某條件規則替換完成后,會重新一輪的替換檢查;
隱含有循環機制,但不超過10次;如果超過,提示500響應碼,[flag]所表示的標志位用于控制此循環機制
如果replacement是以http://或https://開頭,則替換結果會直接以重向返回給客戶端-
[flag]
- last:重寫完成后停止對當前URI在當前location中后續的其它重寫操作,而后對新的URI啟動新一輪重寫檢查;提前重啟新一輪循環,不建議在location中使用
- break:重寫完成后停止對當前URI在當前location中后續的其它重寫操作,而后直接跳轉至重寫規則配置塊之后的其它配置;結束循環,建議在location中使用
- redirect:臨時重定向,重寫完成后以臨時重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求;不能以http://或https://開頭,使用相對路徑,狀態碼:302
- permanent:重寫完成后以永久重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求,狀態碼:301
return
return code [text];
return code URL;
return URL;
停止處理,并返回給客戶端指定的響應碼rewrite_log on | off;
是否開啟重寫日志, 發送至error_log(notice level)set $variable value;
用戶自定義變量
注意:變量定義和調用都要以$開頭if (condition) { ... }
引入新的上下文,條件滿足時,執行配置塊中的配置指令;server, location
-
condition:
- 比較操作符:
==:相同
!=:不同
~:模式匹配,區分字符大小寫
~*:模式匹配,不區分字符大小寫
!~:模式不匹配,區分字符大小寫
!~*:模式不匹配,不區分字符大小寫 - 文件及目錄存在性判斷:
-e, !-e:存在(包括文件,目錄,軟鏈接)
-f, !-f:文件
-d, !-d:目錄
-x, !-x:執行
- 比較操作符:
實驗4-12:實現當訪問www.a.com/zz時自動跳轉至www.a.com/zhengzhou
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80 ;
server_name www.a.com ;
root /app/website1;
location /zz {
rewrite ^/zz/(.*)$ /zhengzhou/$1 permanent;
}
location /zhengzhou{
alias /app/website1/zhengzhou;
}
}
mkdir /app/website1/zhengzhou
echo /app/website1/zhengzhou/index.html > /app/website1/zhengzhou/index.html
nginx -s reload
(9)ngx_http_referer_module模塊
用來阻止referer首部無有效值的請求訪問,可防止盜鏈
- valid_referers none | blocked | server_names | string...;
定義referer首部的合法可用值,不能匹配的將是非法值
none:請求報文首部沒有referer首部
blocked:請求報文有referer首部,但無有效值
server_names:參數,其可以有值作為主機名或主機名模式
arbitrary_string:任意字符串,但可使用*作通配符
regular expression:被指定的正則表達式模式匹配到的字符串,要使用~開頭
vim /app/website2/index.html
/app/website2/index.html

// 第一次測試,登錄www.b.com,應該能看到圖片
server {
listen 80 ;
server_name www.a.com ;
root /app/website1;
valid_referers none block server_names *.a.com ~\.baidu\.;
if ($invalid_referer) {
return 403;
}
}
server {
listen 80;
server_name www.b.com;
root /app/website2;
}
nginx -s reload
// 第二次測試,登錄www.b.com,無法看到圖片
第一次測試
第二次測試
(10)ngx_http_proxy_module模塊
- 轉發請求至另一臺主機
- proxy_pass URL;
Context: location, if in location, limit_except
- proxy_pass后面路徑不帶uri時,會將location的uri傳遞(附加)給后端主機
server {
...
server_name HOSTNAME;
location /uri/ {
proxy_pass http://host[:port]; // 最后沒有/
}
...
}
上面示例:http://HOSTNAME/uri --> http://host/uri
- proxy_pass后面的路徑是一個uri時,其會將location的uri替換為proxy_pass的uri
server {
...
server_name HOSTNAME;
location /uri/ {
proxy_pass http://host/new_uri/;
}
...
}
上面示例:http://HOSTNAME/uri/ --> http://host/new_uri/
- 如果location定義其uri時使用了正則表達式的模式,則proxy_pass之后必須不能使用uri; 用戶請求時傳遞的uri將直接附加至后端服務器之后
server {
...
server_name HOSTNAME;
location ~|~* /uri/ {
proxy_pass http://host; // 不能加/
}
...
}
上面示例:http://HOSTNAME/uri/ --> http://host/uri/
-
實驗4-14:將訪問
www.a.com
網址下圖片的請求調度至一臺服務器,將訪問www.a.com
網址下txt目錄的請求調度至另一臺服務器- 實驗環境:
反向代理服務器:192.168.136.230,nginx服務器
圖片服務器:192.168.136.229,httpd服務器
txt目錄服務器:192.168.136.129,httpd服務器 - 實驗過程
// 192.168.136.230的配置 server { listen 80 ; server_name www.a.com ; root /app/website1; location ~ \.(jpg|gif|png)$ { proxy_pass http://192.168.136.229; } location /txt { proxy_pass http://192.168.136.129; } } nginx -s reload // 192.168.136.229的配置 cp /usr/share/pixmaps/faces/leaf.jpg /var/www/html/ cp /usr/share/pixmaps/faces/chess.jpg /var/www/html/images // 192.168.136.129的配置 echo /var/www/html/index.html > /var/www/html/index.html echo /var/www/html/txt/index.html > /var/www/html/txt/index.html
- 測試
訪問
www.a.com/images/chess.jpg
成功,是因為chess.jpg位于192.168.136.229網頁根目錄的images子目錄下。訪問www.a.com/images/leaf.jpg
失敗,是因為leaf.jpg位于192.168.136.229網頁根目錄下。訪問www.a.com/txt
成功,從返回結果看,實際訪問位置位于192.168.136.129網頁根目錄的txt子目錄下。- 修改配置文件如下:
// 192.168.136.230的配置 server { listen 80 ; server_name www.a.com ; root /app/website1; location ~ \.(jpg|gif|png)$ { proxy_pass http://192.168.136.229; } location /txt { proxy_pass http://192.168.136.129/; // 多了/ } } nginx -s reload
- 測試
從返回結果看,將192.168.136.230的轉發配置URL加
/
符號后,轉發位置被完全替換,直接轉發至192.168.136.129的網頁根目錄下。至于圖片轉發設置中location用到了正則表達式,則URL不能再增加/
符號了,否則語法檢查會報錯。 - 實驗環境:
- proxy_set_header field value;
設定發往后端主機的請求報文的請求首部的值
Context: http, server, location
proxy_set_header X-Real-IP $remote_addr;
- 實驗4-15:實驗環境繼承4-14,實現在后端主機的日志中顯示客戶端的ip地址
// 192.168.136.230配置
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80 ;
server_name www.a.com ;
root /app/website1;
proxy_set_header X-Real-IP $remote_addr;
location ~ \.(jpg|gif|png)$ {
proxy_pass http://192.168.136.229;
}
location /txt {
proxy_pass http://192.168.136.129/;
}
}
nginx -s reload
// 192.168.136.229配置
vim /etc/httpd/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{X-Real-IP}i\" \"%{User-Agent}i\"" combined
CustomLog logs/access_log combined
service httpd reload
左邊紅框對應%h,指代反向代理服務器IP;右邊紅框對應%{X-Real-IP}i,指代客戶端IP
proxy_cache_path;
定義可用于proxy功能的緩存;context: http
proxy_cache_path path [levels=levels] keys_zone=name:size [inactive=time] [max_size=size];proxy_cache zone | off; 默認off
指明調用的緩存,或關閉緩存機制;Context: http, server, locationproxy_cache_key string;
緩存中用于“鍵”的內容
默認值:proxy_cache_key $scheme$proxy_host$request_uri;proxy_cache_valid [code ...] time;
定義對特定響應碼的響應內容的緩存時長
定義在http{...}中
- 實驗4-16:實現反向代理服務器緩存
vim /etc/nginx/nginx.conf
proxy_cache_path /var/lib/nginx/tmp/proxy_cache levels=1:2:2 keys_zone=proxycache:20m inactive=120s max_size=1g;
// 定義緩存路徑和參數,位置只能在http上下文中
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80 ;
server_name www.a.com ;
root /app/website1;
proxy_set_header X-Real-IP $remote_addr;
location ~ \.(jpg|gif|png)$ {
proxy_pass http://192.168.136.229;
}
location /txt {
proxy_pass http://192.168.136.129;
}
proxy_cache proxycache; // 引用定義的緩存名稱
proxy_cache_key $request_uri; // 計算哈希表的key,此處為請求的uri
proxy_cache_valid 200 301 302 1h; // 指定不同響應結果緩存的生命期
proxy_cache_valid any 1m;
}
設置緩存前的ab測試結果
設置緩存后的ab測試結果
proxy_cache_use_stale;
proxy_cache_use_stale error | timeout | invalid_header| updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...
在被代理的后端服務器出現哪種情況下,可以真接使用過期的緩存響應客戶端proxy_cache_methods GET | HEAD | POST ...;
對哪些客戶端請求方法對應的響應進行緩存,GET和HEAD方法總是被緩存proxy_hide_header field;
默認nginx在響應報文不傳遞后端服務器的首部字段Date, Server, X-Pad, X-Accel-等,用于隱藏后端服務器特定的響應首部proxy_connect_timeout time;
定義與后端服務器建立連接的超時時長,如超時會出現502錯誤,默認為60s,一般不建議超出75s,proxy_send_timeout time;
將請求發送給后端服務器的超時時長;默認為60sproxy_read_timeout time;
等待后端服務器發送響應報文的超時時長,默認為60s
(11)ngx_http_headers_module模塊
- 向由代理服務器響應給客戶端的響應報文添加自定義首部,或修改指定首部的值
add_header name value [always];
添加自定義首部
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;add_trailer name value [always];
添加自定義響應信息的尾部
(12)ngx_http_fastcgi_module模塊
- 轉發請求到FastCGI服務器,不支持php模塊方式
fastcgi_pass address;
address為后端的fastcgi server的地址
可用位置:location, if in locationfastcgi_index name;
fastcgi默認的主頁資源fastcgi_param parameter value [if_not_empty];
設置傳遞給FastCGI服務器的參數值,可以是文本,變量或組合fastcgi_cache_path path [levels=levels] keys_zone=name:size[inactive=time] [max_size=size];
定義fastcgi的緩存;context: httpfastcgi_cache zone | off;
調用指定的緩存空間來緩存數據
可用位置:http, server, locationfastcgi_cache_key string;
定義用作緩存項的key的字符串fastcgi_cache_methods GET | HEAD | POST ...;
為哪些請求方法使用緩存fastcgi_cache_min_uses number;
緩存空間中的緩存項在inactive定義的非活動時間內至少要被訪問到此處所指定的次數方可被認作活動項fastcgi_keep_conn on | off;
收到后端服務器響應后,fastcgi服務器是否關閉連接,建議啟用長連接fastcgi_cache_valid [code ...] time;
不同的響應碼各自的緩存時長
- 實驗4-17:實現lnmp,并設置php fastcgi模式緩存
主機1:nginx服務器,ip: 192.168.136.230
主機2:php-fpm服務器,ip: 192.168.136.229
主機3:MariaDB服務器,ip: 192.168.136.130
// 主機2:安裝php-fpm, php-mysql
yum install php-fpm php-mysql
vim /etc/php-fpm.d/www.conf
listen = 9000
listen.allowed_clients = 127.0.0.1,192.168.136.230
pm.status_path = /status // 顯示php狀態,測試用
ping.path = /ping // 測試用
ping.response = pong
service php-fpm start
mkdir -p /app/php
vim /app/php/index.php // 建立php測試文件
<?php
phpinfo();
?>
// 主機1:配置fastcgi
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80 ;
server_name www.a.com ;
root /app/website1;
location ~ \.php$ {
fastcgi_pass 192.168.136.229:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ ^/(status|ping)$ {
fastcgi_pass 192.168.136.229:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
nginx -t
nginx -s reload
- 測試php是否成功連接
// 主機3:安裝mariadb
yum install mariadb-server
systemctl start mariadb
mysql_secure_installation
mysql -uroot -pmagedu
// 建立數據庫和用戶
MariaDB [(none)]> create database wpdb;
MariaDB [(none)]> grant all on wpdb.* to 'wpuser'@'192.168.136.229' identified by 'centos';
// 主機2:編輯php文件,測試是否成功連接mysql
vim /app/php/index.php
<?php
$mysqli=new mysqli("192.168.136.130","wpuser","centos");
if(mysqli_connect_errno()){
echo "Failed";
$mysqli=null;
exit;
}
echo "Success";
$mysqli->close();
?>
- 測試php是否成功連接數據庫
- 測試一下php的性能
- 設置php的fastcgi模式緩存
vim /etc/nginx/nginx.conf
fastcgi_cache_path /var/lib/nginx/tmp/fastcgi_cache levels=1:2:2 keys_zone=fastcgicache:20m inactive=120s max_size=1g;
// 定義緩存路徑和參數,位置只能在http上下文中
vim /etc/nginx/conf.d/vhost.conf
server {
listen 80 ;
server_name www.a.com ;
root /app/website1;
location ~ \.php$ {
fastcgi_pass 192.168.136.229:9000;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache fastcgicache;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
}
}
- 在fastcgi的緩存模式下測試性能,有明顯提高
(13)ngx_http_upstream_module模塊
- 用于將多個服務器定義成服務器組,而由proxy_pass, fastcgi_pass等指令進行引用
- upstream name { ... }
定義后端服務器組,會引入一個新的上下文;context: http
默認調度算法是wrr
- 格式:
upstream httpdsrvs{
server ...
server...
...
}
- server address [parameters];
在upstream上下文中server成員,以及相關的參數;context: upstream
- address的表示格式:
unix:/PATH/TO/SOME_SOCK_FILE
IP[:PORT]
HOSTNAME[:PORT] - parameters:
weight=number:權重,默認為1
max_conns:連接后端報務器最大并發活動連接數,1.11.5后支持
max_fails=number:失敗嘗試最大次數;超出此處指定的次數時,server將被標記為不可用,默認為1
fail_timeout=time:后端服務器標記為不可用狀態的連接超時時長,默認10s
backup:將服務器標記為“備用”,即所有服務器均不可用時才啟用
down:標記為“不可用”,配合ip_hash使用,實現灰度發布
ip_hash
源地址hash調度方法least_conn
最少連接調度算法,當server擁有不同的權重時其為wlc,當所有后端主機連接數相同時,則使用wrr,適用于長連接hash key [consistent]
基于指定的key的hash表來實現對請求的調度,此處的key可以直接文本、變量或二者組合
作用:將請求分類,同一類請求將發往同一個upstream server,使用consistent參數,將使用ketama一致性hash算法,適用于后端是cache服務器(如varnish)時使用
hash $request_uri consistent;
hash $remote_addr;keepalive連接數N;
為每個worker進程保留的空閑的長連接數量,可節約nginx端口,并減少連接管理的消耗
- 實驗4-18:實現負載均衡
主機1:nginx負載均衡服務器,ip: 192.168.136.230
主機2:httpd服務器,ip: 192.168.136.229
主機3:httpd服務器,ip: 192.168.136.129
// 主機1配置
vim /etc/nginx/nginx.conf // upstream定義必須在http上下文中
upstream websrvs {
server 192.168.136.229 weight=2; // wrr算法調度,權重為2
server 192.168.136.129 weight=1; // wrr算法調度,權重為1
server 127.0.0.1:8080 backup;
}
vim /etc/nginx/conf.d/vhost.conf // 設置proxy轉發
server {
listen 80 ;
server_name www.a.com ;
root /app/website1;
location / {
proxy_pass http://websrvs;
}
}
server {
listen 8080;
root /app/website1;
}
// 設置相關網頁文件
echo sorry page > /app/website1/index.html
nginx -t
nginx -s reload
// 主機2, 3的配置
service httpd start
echo homepage on 192.168.136.229 > /var/www/html/index.html // 設置主機2默認主頁
echo homepage on 192.168.136.129 > /var/www/html/index.html // 設置主機3默認主頁
- 測試調度結果
可以看到按照wrr算法的相應權重調度頁面
當關閉主機2的httpd服務后,自動進行健康性檢查,全部調度至主機3
當關閉主機3的httpd服務后,自動調度至本機的sorry page
- 在此基礎上,實現將相同請求url調度至同一臺服務器(模擬調度至緩存服務器)
// 修改主機1設置
vim /etc/nginx/nginx.conf
upstream websrvs {
server 192.168.136.229 weight=2;
server 192.168.136.129 weight=1;
server 127.0.0.1:8080 backup;
hash $request_uri consistent; // 添加此行
}
nginx -t
nginx -s reload
// 主機2, 3創建網頁文件
for i in {1..10}; do echo "page$i on 192.168.136.229" > /var/www/html/page$i.html;done // 主機2上創建文件
for i in {1..10}; do echo "page$i on 192.168.136.129" > /var/www/html/page$i.html;done // 主機3上創建文件
- 測試相同請求的調度綁定
不同的網頁請求可能被調度到不同的主機,但相同的請求調度的主機不會發生變化
(五)nginx的傳輸層調度/反向代理配置
(1)ngx_stream_core_module模塊
模擬基于tcp或udp服務連接的反向代理,即工作于傳輸層的反向代理或調度器
stream { ... }
定義stream相關的服務;context: mainlisten
listen address:port [ssl] [udp] [proxy_protocol];context: server
(2)ngx_stream_proxy_module模塊
可實現代理基于TCP, UDP (1.9.13), UNIX-domain sockets的數據流
proxy_pass address;
指定后端服務器地址proxy_timeout timeout;
無數據傳輸時,保持連接狀態的超時時長
默認為10mproxy_connect_timeout time;
設置nginx與被代理的服務器嘗試建立連接的超時時長
默認為60s
- 實驗5-1:實現傳輸層轉發
主機1:nginx負載均衡服務器,ip: 192.168.136.230
主機2:mysql服務器,ip: 192.168.136.130,CentOS 7
主機3:mysql服務器,ip: 192.168.136.129,CentOS 6
// 主機2, 3的mysql配置,為方便觀察調度結果,主機2安裝MariaDB 5.5,主機3安裝Mysql 5.1
// 主機2
yum install mariadb-server
systemctl start mariadb
mysql_secure_installation
mysql -uroot -pmagedu
MariaDB [(none)]> create user 'testuser'@'192.168.136.%' identified by 'centos';
mysql -utestuser -pcentos -h192.168.136.130 // 測試賬號testuser能否正常登陸
// 主機3
yum install mysql-server
service mysqld start
mysql_secure_installation
mysql -uroot -pmagedu
mysql> create user 'testuser'@'192.168.136.%' identified by 'centos';
mysql -utestuser -pcentos -h192.168.136.129 // 測試賬號testuser能否正常登陸
// 主機1配置,stream必須在main中,不能在http的括號中
vim /etc/nginx/nginx.conf
stream {
upstream mysqlsrvs {
server 192.168.136.130:3306;
server 192.168.136.129:3306;
least_conn; // 最少連接調度算法
}
server {
listen 192.168.136.230:3306;
proxy_pass mysqlsrvs;
}
}
nginx -t
nginx -s reload
- 在其他主機測試,根據登陸數據庫的版本號確定成功調度
五、Nginx的編譯安裝
- 編譯實驗:實現nginx的編譯安裝,并且通過修改源代碼實現
顯示服務器名稱與版本號時,顯示信息為"zengine/5.5.5"
只顯示服務器名稱時,顯示信息為"wanginx"
(一)安裝環境準備
yum groupinstall "development tools"
yum install pcre-devel openssl-devel
useradd -r nginx -s /sbin/nologin
(二)修改源代碼實現服務器名稱和版本信息的定制化
tar xvf nginx-1.12.2.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.12.2/
vim src/http/ngx_http_header_filter_module.c // 修改只顯示服務器名稱時的信息
static u_char ngx_http_server_string[] = "Server: wanginx" CRLF;
vim src/core/nginx.h // 修改服務器和版本號信息均顯示時的信息
#define NGINX_VERSION "5.5.5"
#define NGINX_VER "zengine/" NGINX_VERSION
(三)編譯安裝
./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
make && make install
(四)配置nginx,啟動服務
vim /etc/profile.d/nginx.sh
PATH=/usr/local/nginx/sbin:$PATH
source /etc/profile.d/nginx.sh
nginx
(五)關閉服務器版本信息顯示
vim /etc/nginx/nginx.conf
server_tokens off; // 在http括號中添加此行
nginx -t
nginx -s reload