Nginx是一款輕量級的Web和反向代理服務器,它的誕生主要是爲了解決C10K的問題,它具有較多的特性;
Nginx的特性:
1、模塊化設計,具有較好的擴展性
2、具有高可靠性
3、支持熱部署:可以在不影響用戶使用的情況下,升級老版本;可以不停機更新配置文件、更換日誌文件、更換服務器程序版本
4、低消耗內存:10K個keepalive連接模式下的非活動連接僅消耗2.5M內存
5、是event-driven事件驅動模型:具有一個主進程,多個子進程,每個子進程響應多個請求
Nginx的基本功能:
1、靜態資源的web服務器
2、可以作爲http協議的正反向代理服務器
正向代理:Nginx作爲服務器,可直接響應資源給客戶端
反向代理:Nginx作爲代理器,客戶端不直接和服務器交流,而是通過和代理服務器的代理器來交流;通過代理器來讀取服務器中的資源,從而響應給客戶端
3、支持pop3/imap4協議反向代理服務器
4、支持FastCGI、UWSGI等
5、支持模塊化:例如ssl、zip
Nginx的程序架構圖:
Master:Master爲主進程,主要作用是加載配置文件、管理worker進程、可進行平滑升級
Worker:worker爲子進程,由master主進程生成,一個worker進程可以處理多個客戶端請求;主要作用是接受客戶端請求、http代理、FastCGI代理、Memcache代理、在磁盤內緩存以及管理磁盤內的緩存
準備環境:
虛擬機1:nginx | IP:172.18.42.200 |
虛擬機2:httpd(後端主機) | IP:172.18.42.201 |
虛擬機3:httpd(後端主機) | IP:172.18.42.202 |
一、實現“ngx_http_auth_basic_module”認證機制
1、虛擬機1安裝nginx服務
[root@node0 ~]# yum install nginx
2、編輯nginx主配置文件
[root@node0 ~]# vim /etc/nginx/nginx.conf ##nginx的主配置文件 server { listen 80; ##監聽的端口 server_name localhost; root /www/lweim/; ##指定讀取資源路徑 location /nginx { auth_basic "Show your Information"; ##指定提示頁 auth_basic_user_file /etc/nginx/.ngxpasswd; ##指定密碼文件路徑 } } [root@node0 nginx]# htpasswd -c -m /etc/nginx/.ngxpasswd lweim ##創建虛擬用戶“lweim” -c:當.nginxpasswd這個文件不存在時需使用 -m:使用md5算法 [root@node0 ~]# echo "172.18.42.200 Nginx 1" > /www/lweim/nginx/index.html
3、訪問web頁面
二、實現“ngx_http_stub_status_module”基本狀態信息
1、編輯配置文件
[root@node0 ~]# vim /etc/nginx/nginx.conf server { listen 80; server_name localhost; root /www/lweim/; location /status { ##指明狀態頁面目錄 stub_status on; ##開啓狀態頁 } } [root@node0 ~]# mkdir /www/lweim/status/ ##創建狀態頁目錄
2、訪問web
Active connections:處於活動狀態的客戶端連接數量
Accepts:已經接受客戶端的總請求數量
Handled:已經處理客戶單的總請求數量
Request:客戶端發出的總請求數量
Reading:正在讀取客戶端請求報文首部的連接數量
Writing:正在向客戶端發送響應報文的連接數量
Waiting:正在等待客戶端發出請求的空閒進程數
三、實現“ngx_http_ssl_module”https請求
1、在未修改配置文件之前嘗試訪問“https://172.18.42.200”
2、修改配置文件
server { listen 80; server_name localhost; root /www/lweim/; } server { listen 443 ssl; ##https監聽在tcp的443端口 server_name localhost; ssl_certificate /etc/nginx/ssl/ngx.crt; ##指明ngx.crt的文件路徑 ssl_certificate_key /etc/nginx/ssl/ngx.key; ##指明ngx.key的文件路徑 ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location /nginx { } }
Ssl on | off:是否啓用當前虛擬主機的ssl功能
Ssl_ certificate file:當前虛擬主機使用的PEM格式的證書文件
Ssl_certificate_key file:當前虛擬機使用的證書文件中的公鑰配對的私鑰文件路徑,依然是PEM格式
Ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2]:表示ssl協議的版本;摩恩爲後3個
Ssl_session_cache off | none | [ builtin [ : size ] ] [ shard : name : size ]:指明ssl會話緩存的機制
builtin:使用openssl內建的緩存機制,對此機制爲各worker獨有
shared:在各個worker進程共享的緩存
name:緩存空間的名稱
size:緩存空間的大小以字節爲單位,每1MB內存可緩存4000個會話
Ssl_session_time timeout:ssl會話時長,指ssl session cache中緩存條目時長
3、再次訪問web
四、實現“ngx_http_rewrite_module”重寫url
1、修改配置文件
location / { rewrite (.*)\.html$ $1.txt last; rewrite (.*)\.txt$ $1.jpg break; } ##當用戶第一次請求uri中以html結尾時,會把uri重寫爲以txt結尾並重新開始循環; 隨後匹配到第二條rewrite,把uri中的txt更改爲以jpg結尾的文件並響應給客戶端
rewrite regex replacement [flag]
regex:基於正則表達式,用於匹配用戶請求的url
replacement:爲重寫的結果
Flag:
Last:重寫完成後停止對當前uri在location中的後續其他操作,而後更改爲對新的uri做出新一輪處理
Break:重寫完成後停止對當前uri在當前location中的後續其他操作,直接返回給客戶端
Redirect:重寫完成後,臨時返回給客戶端一個新的URL,隨後瀏覽器在根據新的URL請求新的資源;響應碼爲302
Premanent:重寫完成後,永久返回給客戶端一個新的URL,隨後瀏覽器在根據新的URL請求信的資源;響應碼爲301
2、訪問web服務
五、實現“ngx_http_gizp_module”壓縮功能:只能用在http上下文
1、修改nginx配置文件
http { gzip on; gzip_comp_level 6; gzip_disable msie6; gzip_http_version 1.0; gzip_types text/plain; }
Gzip on | off:啓用或禁用gzip壓縮響應報文
Gzip_comp_level level:指定壓縮比,1-9;默認爲1
Gzip_disable regex:regex是匹配客戶端瀏覽器類型的模式,表示對所有匹配到的瀏覽器下不執行壓縮響應
Gzip_min_length length:觸發啓用先壓縮功能的響應報文的最小長度
Gzip_http_version:設定啓用壓縮響應功能時,協議的最小版本
Gzip_types:指定僅執行壓縮的資源內容類型;默認爲text/html
gzip_types text/plain、text/css、text/xml、application/x-javascript 、application/xml、application/json、application/java-script;
2、訪問web
六、實現“ngx_http_fastcgi_module”
1、虛擬機2安裝php-fpm服務
[root@node1 ~]# yum install php-fpm -y ##安裝php-fpm服務 [root@node1 ~]# vim /etc/php-fpm.d/www.conf ##修改php-fpm的配置文件 listen = 172.18.42.201:9000 ##監聽本地能與外部通信的IP地址 listen.allowed_clients = 172.18.42.200 ##監聽具有httpd服務的IP [root@node1 ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 172.18.42.201:9000 *:*
Nginx和php結合的方式只有fpm;php-fpm的工作方式類似於httpd的prefork模塊
Listen = :指明本地能與外部通信的地址
Listen.allow_clients = :指明具有httpd服務的IP
Pm = dynamic | static
Pm.start_servers:啓動fpm進程時啓動的工作進程數量
Pm.min_spare_servers:最少空閒進程數
Pm.max_spare_servers:最大空閒進程數
Pm.max_children:最大工作進程數
2、修改nginx的配置文件
location ~ \.php$ { fastcgi_pass 172.18.42.201:9000; ##指明fpm-php服務的IP fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /web/lweim/$fastcgi_script_name; include fastcgi_params; }
Fastcgi_pass:用來指明代理那個服務器(裝了php-fpm的服務器能與外部監聽的地址)
Fastcgi_index:fastcgi應用的主頁面名稱
Fastcgi_param:傳遞給fpm服務器參數
3、訪問web
七、實現“fastcgi_cache_path”緩存:只能用在http上下文
1、修改nginx的配置文件
[root@node0 ~]# vim /etc/nginx/nginx.conf http { fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=ngxcache:10m; ##定義緩存 } location ~ \.php$ { fastcgi_pass 172.18.42.201:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /web/lweim/$fastcgi_script_name; include fastcgi_params; fastcgi_cache ngxcache; ##指明緩存名稱 fastcgi_cache_key $request_uri; ##定義緩存鍵 fastcgi_cache_valid 200 302 10m; ##狀態碼200、302緩存10分鐘 fastcgi_cache_valid 301 1h; ##狀態碼301緩存1個小時 fastcgi_cache_valid 404 2h; ##狀態碼404緩存2個小時 }
Fastcgi_cache_path path:指明緩存文件的路徑
Fastcgi_cache zone | off:是否啓用緩存,如果啓用,需要指明緩存與那個cache文件中
Fastcgi_cache_key string:定義要使用的緩存鍵
Fastcgi_cache_methods GET | HEAD | POST:緩存那些類型的請求的相關數據
Fastcgi_cache_vaild [code..] time:對不同響應碼設定其可緩存時間
2、查看緩存目錄
[root@node0 ~]# tree /var/cache/nginx/fastcgi/ /var/cache/nginx/fastcgi/ ├── 1 │ └── af │ └── e251273eb74a8ee3f661a7af00915af1 ├── b │ └── fe │ └── c86156f7dcfecf44876ca30d1bac7feb └── e └── 39 └── f46b8508aa08a6f8670fb088b8a9739e
八、實現“ngx_http_proxy_module”
1、修改nginx配置文件
[root@node0 ]# vim nginx.conf location ~ .*\.txt$ { ##txt結尾的文件由虛擬機2來響應 proxy_pass } location ~ .*\.jpg$ { proxy_pass ##jpg結尾的文件由虛擬機3來響應 }
(1)當proxy_pass後面的路徑不帶uri時,會將其location中的uri傳遞給後端主機
location /bbs { proxy_pass http://172.18.42.200; } ##最終請求的路徑爲http://172.18.42.200/bbs
(2)當proxy_pass後面路徑是一個uri時,它會將location中的uri替換爲proxy_pass的後端主機
location /bbs { proxy_pass http://172.18.42.200/; } ##最終請求的路徑爲http://172.18.42.200;把“/bbs/”當做成“/”
(3)如果location定義其uri時使用了正則表達式模式匹配機制,則proxy_pass後的路徑一定不能帶uri
location ~* \.php$ { proxy_pass }
2、配置虛擬機2
[root@node1 ~]# yum install httpd -y [root@node1 ~]# echo "172.18.42.201 txt" > /var/www/html/wtc.txt [root@node1 ~]# systemctl start httpd.service
3、配置虛擬級3
[root@node1 ~]# yum install httpd -y [root@node2 ~]# mv /root/wawa.jpg /var/www/html/wtc.jpg [root@node1 ~]# systemctl start httpd.service
4、訪問web
九、實現“proxy_set_header”,讓後端主機記錄源IP
1、在未修改配置文件之前查看後端主機的記錄日誌
[root@node1 ~]# tail /var/log/httpd/access_log 172.18.42.200 - - [25/May/2016:19:20:34 +0800] "GET /wtc.txt HTTP/1.0" 200 18 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0" 172.18.42.200 - - [25/May/2016:19:20:34 +0800] "GET /wtc.txt HTTP/1.0" 200 18 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0" ##後端主機記錄的訪問IP都是nginx自身的的IP
2、修改nginx的配置文件
[root@node0 nginx]# vim nginx.conf location ~ .*\.txt$ { proxy_pass http://172.18.42.201; proxy_set_header X-Real_IP $remote_addr; }
3、修改虛擬機2httpd的配置文件
[root@node1 ~]# vim /etc/httpd/conf/httpd.conf #LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%{X-Real_IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
4、訪問web之後查看後端主機的記錄日誌
[root@node1 ~]# tail /var/log/httpd/access_log 172.18.250.14 - - [25/May/2016:19:27:41 +0800] "GET /wtc.txt HTTP/1.0" 304 - "-" "Mozilla/5.0 (Wi 172.18.250.14 - - [25/May/2016:19:27:41 +0800] "GET /wtc.txt HTTP/1.0" 304 - "-" "Mozilla/5.0 (Wi 172.18.250.14 - - [25/May/2016:19:27:41 +0800] "GET /wtc.txt HTTP/1.0" 304 - "-" "Mozilla/5.0 (Wi ##記錄的訪問IP“172.18.250.14”爲物理機的IP,做日誌分析纔有意義
十、實現實現“proxy_cache”緩存
1、修改nginx配置文件
http { proxy_cache_path /var/cache/nginx/proxy levels=1:2 keys_zone=ngxcache:10m; } location ~ .*\.txt$ { proxy_pass http://172.18.42.201; proxy_set_header X-Real_IP $remote_addr; proxy_cache ngxcache; proxy_cache_key $request_uri; proxy_cache_valid 200 302 404 1h; }
2、訪問web之後查看緩存目錄
root@node0 ~]# tree /var/cache/nginx/proxy/ /var/cache/nginx/proxy/ └── 5 └── fd └── 19cbf2faabf255f790ebd83de4a42fd5
十一、實現“ngx_http_upstream_module”負載均衡
1、修改nginx配置文件
http { upstream web { ##定義一個服務器組 server 172.18.42.201 weight=1; server 172.18.42.202 weight=2; } } location / { proxy_pass ##引用服務器組 } } [root@node1 ~]# curl http://172.18.42.200 <h1> 172.18.42.201 Web Server 1 </h1> [root@node1 ~]# curl http://172.18.42.200 <h1> 172.18.42.202 Web Server 2 . </h1> [root@node1 ~]# curl http://172.18.42.200 <h1> 172.18.42.202 Web Server 2 . </h1> [root@node1 ~]# curl http://172.18.42.200 <h1> 172.18.42.201 Web Server 1 </h1> ##實現了負載均衡
Upstream name {……}
定義後端服務器組,可直接使用字符串
Server address {parameters}:指明服務器的地址和相關參數
address:
IP[ : PORT ]:給定IP地址
HOSTNAME[ : PORT ]:當後端有多個虛擬主機時,應該使用hostname定義
Unix:/path/to/some_sock_file
parameters:
Weight=number:後端服務器權重
Max_fails=number:設定最大失敗重試次數
Fail_timeout time:設置服務器不可用的超時時長
Backup:備用主機;定義後端主機是否爲一個sorry_server
Down:手動標記後自動下面,不在處理任何請求
2、實現源地址哈希調度算法
http { upstream web { server 172.18.42.201 weight=1; server 172.18.42.202 weight=2; ip_hash; ##源地址哈希算法;將來自同一用戶的請求始終發往同一主機上 } } location / { proxy_pass http://web; } [root@node1 ~]# curl http://172.18.42.200 <h1> 172.18.42.201 Web Server 1 </h1> [root@node1 ~]# curl http://172.18.42.200 <h1> 172.18.42.201 Web Server 1 </h1> [root@node1 ~]# curl http://172.18.42.200 <h1> 172.18.42.201 Web Server 1 </h1> [root@node1 ~]# curl http://172.18.42.200 <h1> 172.18.42.201 Web Server 1 </h1> ##響應請求的始終是一臺後端主機
Ip_hash:源地址哈希算法,只能用在upstream上下文;指定調度算法
Least_conn:最少連接調度方法,只能用在upstream上下文
Keepalive:指明嘗試連接後端主機次數,只能用在upstream上下文中
3、“Health_check”對後端主機做健康檢測:只能用在location上下文
Interval=number:檢測的頻度,默認爲5s
Fails=number:判定爲失敗的檢測次數
Passes=number:判定爲檢測成功的次數
Uri=uri:執行健康狀態監測時請求的uri
Match=name:基於哪個match做檢測結果爲“成功”或者“失敗”的判斷
Port=port:向服務器的那個端口發起健康狀態檢測請求
4、“match name”對後端主機做健康狀態監測時,定義其結果判斷標準的標準指令:只能用於http上下文
Status:期望的響應碼
Status CODE
Status !CODE
Status CODE~CODE
Header:基於響應首部進行判斷
Header HEADER=VALUE
Header HEADER!=VALUE
Header [!]HEADER
Header Header ~ VALUE
Body:期望的響應碼報文的主體部分應該有的內容
Body ~ “CONTENT”
Body !~ “CONNTENT”
5、“hash key”:定義調度方法,可自定義基於何種信息(key)進行綁定:只能用在upstream上下文
Hash $remote_addr:根據後端主機的IP地址進行綁定
Hash $request_uri:綁定第一次訪問的服務器,以後每次的訪問都指定元的的那臺服務器
Hasg $cookie_username:根據客戶端的cookie