一、nginx實現反向代理負載均衡
1、什么是反向代理呢?
反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的連接請求,然后將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現為一個反向代理服務器。如下圖的www.baidu.com可以想象成一個代理服務器,當客戶端internet向www.baidu.com發起連接請求,www.baidu.com這個代理服務器會把請求轉發給后端的服務器server1、server2、server3,這時代理服務器就表現為一個反向代理服務器。
2、nginx實現反向代理(介紹)
nginx代理基於ngx_http_proxy_module模塊的功能,該模塊有很多屬性配置選項,如:
proxy_pass:指定將請求代理至server的URL路徑;
proxy_set_header:將發送至server的報文的某首部進行重寫;
proxy_send_timeout:在連接斷開之前兩次發送到server的最大間隔時長;過了這么長時間后端還是沒有收到數據,連接會被關閉
proxy_read_timeout:是從后端讀取數據的超時時間,兩次讀取操作的時間間隔如果大於這個值,和后端的連接會被關閉
proxy_connect_timeout: 是和后端建立連接的超時時間
3、nginx實現反向代理負載均衡(介紹)
nginx負載均衡是ngx_http_upstream_module模塊的功能,需要在配置文件http塊上下文中定義upstream塊,指定一組負載均衡的后端服務器,然后在proxy_pass中引用,就可以反向代理時實現負載均衡了
語法:server address [parameters];
paramerters:
weight:負載均衡策略權重,默認為1;
max_fails:在一定時間內(這個時間在fail_timeout參數中設置)檢查這個服務器是否可用時產生的最多失敗請求數
fail_timeout:在經歷了max_fails次失敗后,暫停服務的時間。max_fails可以和fail_timeout一起使用,進行對后端服務器的健康狀態檢查;
backup:當所有后端服務器都宕機時,可以指定代理服務器自身作為備份,對外提供維護提示頁面
down:永久不可用
4、專業健康檢測模塊 nginx_upstream_check_module-master(官方地址介紹)
5、如何使用tengine(實驗)
①下載源碼並編譯安裝
lftp 172.17.0.1:/pub/Sources/sources/nginx> get tengine-2.1.1.tar.gz 2062650 bytes transferred 下載源碼 [root@centos7 ~]# tar -xvf tengine-2.1.1.tar.gz 解壓 [root@centos7 ~]# cd tengine-2.1.1/ [root@centos7 ~/tengine-2.1.1]# yum groupinstall "Development tools" 安裝開發包組 [root@centos7 ~/tengine-2.1.1]# yum install openssl-devel pcre-devel 安裝所需工具 [root@centos7 ~/tengine-2.1.1]# ./configure --prefix=/usr/local/tengine 安裝到指定路徑 [root@centos7 ~/tengine-2.1.1]# make && make install 編譯 [root@centos7 ~/tengine-2.1.1]# cd /usr/local/tengine/
②編輯配置文件
[root@centos7 ~/tengine-2.1.1]# cd /usr/local/tengine/ 進入自己指定的目錄 [root@centos7 /usr/local/tengine]# ls conf html include logs modules sbin [root@centos7 /usr/local/tengine]# cd conf/ [root@centos7 /usr/local/tengine/conf]# vim nginx.conf 修改配置文件,寫在http段 #gzip on; upstream server-cluster { 定義upstream塊,指定一組負載均衡的后端服務器 server 172.17.250.117:80; server 172.17.250.107:80; 后端服務器IP check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_http_send "HEAD / HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; } upstream static-cluster { server 172.17.251.226:80; server 172.17.251.227:80; check interval=3000 rise=2 fall=5 timeout=1000 type=http; # check_http_send "HEAD / HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; } server { listen 80; server_name localhost; proxy_set_header Host $host; 將發送至server的報文的首部進行重寫;常用於nginx做負載均衡時,獲取客戶端IP使,需要添加forward頭部 proxy_set_header X-REMOTE-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 原有請求報文中如果存在X-Forward-For首部,則將client_addr以逗號分隔原有值后,否則則直接添加此首部; #charset koi8-r; #access_log logs/host.access.log main; location /stats { check_status; } location ~* .jpg|.png|.gif|.jpeg$ { proxy_pass http://static-cluster; 指定將請求代理至server的URL路徑 } location ~* .css|.js|.html|.xml$ { proxy_pass http://static-cluster; } location / { # root html; index index.php index.html index.htm; }
③查看健康測試狀態頁面
二、nginx實現緩存功能(實驗)
nginx實現緩存是通過代理緩存proxy-cache,這也是ngx_http_proxy_module模塊提供的功能,配置較多,常用的選項有;proxy_cache_path proxy_cache和proxy_cache_valid
1、proxy_cache_path
proxy_cache_path定義一個完整的緩存空間,指定緩存數據的磁盤路徑、索引存放的內存空間以及一些其他參數。如緩存策略。該選項只能定義在http塊上下文中
例如:procxy_cache_path /data/cache levels=1:2 keys_zone=web:10m max_size=1G inactive=10;
2、proxy_cache
proxy_cache用來引用上面proxy_cache_path定義的緩存空間,現時打開緩存功能
例如:proxy_cache web; #引用上面定義上的緩存空間,同一緩存空間可以在幾個地方使用
3、proxy_cache_valid
proxy_cache_valid設置不同響應代碼的緩存時間
例如:proxy_cache_valid 200 302 10m;
實現緩存功能,配置文件編輯示例:
56 proxy_cache_path /data/cache levels=1:2 keys_zone=web:10m max_size=1G inactive=10m; 57 定義一個完整的緩存空間;緩存數據存儲在/data/cache目錄中,配置在該目錄下再分兩層目錄,名稱為web,10m內存空間大小,最大緩存數據磁盤空間的大小;10分鍾未被訪問的緩存數據將從緩存中刪除 58 server { 59 listen 80; 60 server_name qianzhihe; 61 proxy_set_header Host $host; 62 proxy_set_header X-REMOTE-IP $remote_addr; 63 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 64 65 add_header Qianzhihe-Cache "$upstream_cache_status from $server_addr"; 66 給請求響應增加一個頭部信息,表示從服務器上返回的cache狀態怎么樣(有沒有命中HIT) #charset koi8-r; 67 68 #access_log logs/host.access.log main; 69 location /stats { 70 check_status; 71 } 72 location ~* .jpg|.png|.gif|.jpeg$ { 74 proxy_cache web; 緩存圖片,引用上面定義的緩存空間,同一緩存空間可以在幾個地方使用 75 proxy_cache_valid 200 302 301 20m; 對代碼200 302 301的響應設置10分鍾的緩存 76 proxy_pass http://server-cluster; 引用上面定義的upstream負載均衡組
測試:訪問IP地址后F12鍵查看header頭部,第一次是MISS字樣(丟失圖片),再次刷新就會顯示HIT(命中)字樣,證明圖片緩存成功
介紹結束,多謝瀏覽~~