Nginx服務搭建負載均衡,反向代理,快取加速,訪問分散式檔案系統高可用
阿新 • • 發佈:2019-01-02
主配置檔案如下:
[[email protected]~]#vim /usr/local/nginx/conf/nginx.conf
server{
listen 8099 //埠號
location / {
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
root /mnt/icfs; //共享叢集目錄
auth_basic "AS13000 Restricted Site ";
auth_basic_user_file /usr/local/nginx/nginx_passwd; //金鑰檔案
}
}
建立nginx_passwd檔案,包括使用者名稱、密碼:
使用htpasswd命令建立使用者,在第一次建立時需要加-c可建立檔案。
[ [email protected] ~]# touch /usr/local/nginx/nginx_passwd
[[email protected] ~]# htpasswd –c –m /usr/local/nginx/nginx_passwd zang 密碼
[[email protected] ~]# htpasswd –m /usr/local/nginx/nginx_passwd lin 密碼
[[email protected] ~]# htpasswd –m /usr/local/nginx/nginx_passwd tom 密碼
1.2 Nginx啟動
在介面服務節點執行以下操作:
[[email protected] ~]#cd /usr/local/nginx/sbin
[[email protected] sbin]#./nginx //啟動
[[email protected] sbin]#./nginx -t //檢查語法錯誤
[[email protected] sbin]#./nginx -s reload //重啟服務
[[email protected] sbin]#./nginx -s stop //停止服務
1.3 客戶端訪問
客戶端可通過瀏覽器直接訪問:瀏覽器輸入http://ip:port
二,Nginx快取
2.1 proxy_cache快取
在nginx.conf配置檔案中
http塊加入以下程式碼
#proxy_temp_path和proxy_cache_path指定的路徑必須在同一分割槽
proxy_temp_path /tmp/proxy_temp_dir;
#設定名稱為nginx_cache,記憶體快取空間大小為500MB,30天沒有被訪問的內容自動清除,硬碟快取空間大小為30GB。
proxy_cache_path /tmp/proxy_cache_dir levels=1:2 keys_zone=nginx_cache:200m inactive=3d max_size=30g;
在server加入以下程式碼
location / {
proxy_cache nginx_cache;
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
expires 30d;
}
重啟Ngnix
/usr/local/nginx/sbin/nginx -s reload
2.2 開啟檔案快取
預設: open_file_cache off;
配置塊: http、 server、 location
檔案快取會在記憶體中儲存以下3種資訊:
❑檔案控制代碼、 檔案大小和上次修改時間。
❑已經開啟過的目錄結構。
❑沒有找到的或者沒有許可權操作的檔案資訊。
這樣,通過讀取快取就減少了對磁碟的操作。
該配置項後面跟3種引數。
❑max:表示在記憶體中儲存元素的最大個數。當達到最大限制數量後,將採用LRU(Least
Recently Used)演算法從快取中淘汰最近最少使用的元素。
❑inactive :表示在 inactive 指定的時間段內沒有被訪問過的元素將會被淘汰。 預設時間
為60秒。
❑off:關閉快取功能。
例如:
open_file_cache max=1000 inactive=20s;
[[email protected]~]#vim /usr/local/nginx/conf/nginx.conf
server{
listen 8099 //埠號
location / {
open_file_cache max=1000 inactive=20s; //開啟檔案快取
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
root /mnt/icfs; //共享叢集目錄
auth_basic "AS13000 Restricted Site ";
auth_basic_user_file /usr/local/nginx/nginx_passwd; //金鑰檔案
}
}
三、Nginx負載均衡
Nginx的upstream預設是以輪詢的方式實現負載均衡,這種方式中,每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。
另外一種方式是ip_hash:每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題。
測試環境:ICFS V3.6.3.1版本服務,三節點服務叢集,33osd,2+1糾刪,3mons;
說明: jinggao注意
AS13000系統只在主mon節點部署了Nginx服務,其他節點需要以下步驟:
非主mon節點:
cp /icfs/plugins/monitor/Agent/nginx/ /usr/loacl/ -r
三節點叢集IP,分別為
A伺服器IP :100.7.42.73 (主)
B伺服器IP :100.7.42.173
C伺服器IP :100.7.42.176
部署思路:
A伺服器100.7.42.73做為主伺服器,由A伺服器負載均衡到B伺服器(100.7.42.173)與C伺服器(100.7.42.176)上。
1、A伺服器nginx.conf設定,如下
在http段加入以下程式碼
upstream inspur.com { //inspur.com 為server引用
server 100.7.42.173:8099;
server 100.7.42.176:8099;
}
server{
listen 8099;
server_name inspur.com;
location / {
#反向代理的地址
proxy_pass http:// inspur.com;
#設定主機頭和客戶端真實地址,以便伺服器獲取客戶端真實IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
儲存重啟nginx
2,B、C伺服器nginx.conf設定
開啟nginx.conf,在http段加入以下程式碼
server{
listen 8099;
server_name inspur.com;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
root /mnt/icfs; //共享叢集目錄
}
儲存重啟nginx
四,Nginx與Apache實現反向代理
反向代理(reverse proxy)方式是指用代理伺服器來接受 Internet 上的連線請求,然後將
請求轉發給內部網路中的上游伺服器, 並將從上游伺服器上得到的結果返回給 Internet 上請
求連線的客戶端, 此時代理伺服器對外的表現就是一個 Web 伺服器。
測試環境:ICFS V3.6.3.1版本服務,三節點服務叢集,33osd,2+1糾刪,3mons;
三節點叢集IP,分別為
A伺服器IP :100.7.42.73 (主) 0
B伺服器IP :100.7.42.173 1
C伺服器IP :100.7.42.176 2
對應虛擬ip為:
100.7.39.100 2
100.7.39.101 1
100.7.39.102 0
1、100.7.42.73節點Apache配置(其他節點配置相同,ServerName與實際節點相對應):
[[email protected]~]# vim /etc/httpd/conf/httpd.conf
/*在配置檔案最下方編輯*/
IncludeOptional conf.d/autoindex.conf //優化增加訪問共享目錄列表資訊
<Directory "/mnt/icfs/httptest">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted //允許匿名訪問共享目錄
</Directory>
<VirtualHost *:910>
ServerAdmin [email protected]
DocumentRoot /mnt/icfs/httptest/ //共享目錄
Options Indexes FollowSymLinks
<Location />
Options Indexes MultiViews
Dav On
</Location>
</VirtualHost>
2、100.7.42.73節點Nginx配置(叢集各個節點配置一樣):
在http段加入以下程式碼
upstream inspur.com { //inspur.com 為server引用
server 100.7.42.73:910;
server 100.7.42.173:910;
server 100.7.42.176:910;
}
server{
listen 8099;
server_name inspur.com;
location / {
#反向代理的地址
proxy_pass http://inspur.com;
#設定主機頭和客戶端真實地址,以便伺服器獲取客戶端真實IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
3,客戶端訪問
瀏覽器輸入:
http://虛擬IP:8099 /*通過Nginx反向代理訪問共享服務*/
http://虛擬IP:910 /*通過Apache訪問共享服務*/
五,Nginx與Apache效能對比
AB測試工具:
Nginx併發使用者數為1000,請求總數為1000,
Server Software: Nginx/1.9.2
Requests per second: 12267.02 [#/sec] (mean) #每秒請求數,伺服器的吞吐量
Time per request: 81.442 [ms] (mean) #使用者平均請求等待時間 時延
每個請求的平均處理時間為0.081ms,80%的請求是在38ms之內完成
http併發使用者數為1000,請求總數為1000,
Server Software: Apache/2.4.6
Requests per second: 1807.12 [#/sec] (mean)
Time per request: 553.367 [ms] (mean)
每個請求的平均處理時間為0.553ms,80%的請求是在227ms之內完成
[[email protected]~]#vim /usr/local/nginx/conf/nginx.conf
server{
listen 8099 //埠號
location / {
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
root /mnt/icfs; //共享叢集目錄
auth_basic "AS13000 Restricted Site ";
auth_basic_user_file /usr/local/nginx/nginx_passwd; //金鑰檔案
}
}
建立nginx_passwd檔案,包括使用者名稱、密碼:
使用htpasswd命令建立使用者,在第一次建立時需要加-c可建立檔案。
[
[[email protected] ~]# htpasswd –c –m /usr/local/nginx/nginx_passwd zang 密碼
[[email protected] ~]# htpasswd –m /usr/local/nginx/nginx_passwd lin 密碼
[[email protected] ~]# htpasswd –m /usr/local/nginx/nginx_passwd tom 密碼
1.2 Nginx啟動
在介面服務節點執行以下操作:
[[email protected] ~]#cd /usr/local/nginx/sbin
[[email protected] sbin]#./nginx //啟動
[[email protected] sbin]#./nginx -t //檢查語法錯誤
[[email protected] sbin]#./nginx -s reload //重啟服務
[[email protected] sbin]#./nginx -s stop //停止服務
1.3 客戶端訪問
客戶端可通過瀏覽器直接訪問:瀏覽器輸入http://ip:port
二,Nginx快取
2.1 proxy_cache快取
在nginx.conf配置檔案中
http塊加入以下程式碼
#proxy_temp_path和proxy_cache_path指定的路徑必須在同一分割槽
proxy_temp_path /tmp/proxy_temp_dir;
#設定名稱為nginx_cache,記憶體快取空間大小為500MB,30天沒有被訪問的內容自動清除,硬碟快取空間大小為30GB。
proxy_cache_path /tmp/proxy_cache_dir levels=1:2 keys_zone=nginx_cache:200m inactive=3d max_size=30g;
在server加入以下程式碼
location / {
proxy_cache nginx_cache;
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
expires 30d;
}
重啟Ngnix
/usr/local/nginx/sbin/nginx -s reload
2.2 開啟檔案快取
預設: open_file_cache off;
配置塊: http、 server、 location
檔案快取會在記憶體中儲存以下3種資訊:
❑檔案控制代碼、 檔案大小和上次修改時間。
❑已經開啟過的目錄結構。
❑沒有找到的或者沒有許可權操作的檔案資訊。
這樣,通過讀取快取就減少了對磁碟的操作。
該配置項後面跟3種引數。
❑max:表示在記憶體中儲存元素的最大個數。當達到最大限制數量後,將採用LRU(Least
Recently Used)演算法從快取中淘汰最近最少使用的元素。
❑inactive :表示在 inactive 指定的時間段內沒有被訪問過的元素將會被淘汰。 預設時間
為60秒。
❑off:關閉快取功能。
例如:
open_file_cache max=1000 inactive=20s;
[[email protected]~]#vim /usr/local/nginx/conf/nginx.conf
server{
listen 8099 //埠號
location / {
open_file_cache max=1000 inactive=20s; //開啟檔案快取
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
root /mnt/icfs; //共享叢集目錄
auth_basic "AS13000 Restricted Site ";
auth_basic_user_file /usr/local/nginx/nginx_passwd; //金鑰檔案
}
}
三、Nginx負載均衡
Nginx的upstream預設是以輪詢的方式實現負載均衡,這種方式中,每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。
另外一種方式是ip_hash:每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題。
測試環境:ICFS V3.6.3.1版本服務,三節點服務叢集,33osd,2+1糾刪,3mons;
說明: jinggao注意
AS13000系統只在主mon節點部署了Nginx服務,其他節點需要以下步驟:
非主mon節點:
cp /icfs/plugins/monitor/Agent/nginx/ /usr/loacl/ -r
三節點叢集IP,分別為
A伺服器IP :100.7.42.73 (主)
B伺服器IP :100.7.42.173
C伺服器IP :100.7.42.176
部署思路:
A伺服器100.7.42.73做為主伺服器,由A伺服器負載均衡到B伺服器(100.7.42.173)與C伺服器(100.7.42.176)上。
1、A伺服器nginx.conf設定,如下
在http段加入以下程式碼
upstream inspur.com { //inspur.com 為server引用
server 100.7.42.173:8099;
server 100.7.42.176:8099;
}
server{
listen 8099;
server_name inspur.com;
location / {
#反向代理的地址
proxy_pass http:// inspur.com;
#設定主機頭和客戶端真實地址,以便伺服器獲取客戶端真實IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
儲存重啟nginx
2,B、C伺服器nginx.conf設定
開啟nginx.conf,在http段加入以下程式碼
server{
listen 8099;
server_name inspur.com;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
root /mnt/icfs; //共享叢集目錄
}
儲存重啟nginx
四,Nginx與Apache實現反向代理
反向代理(reverse proxy)方式是指用代理伺服器來接受 Internet 上的連線請求,然後將
請求轉發給內部網路中的上游伺服器, 並將從上游伺服器上得到的結果返回給 Internet 上請
求連線的客戶端, 此時代理伺服器對外的表現就是一個 Web 伺服器。
測試環境:ICFS V3.6.3.1版本服務,三節點服務叢集,33osd,2+1糾刪,3mons;
三節點叢集IP,分別為
A伺服器IP :100.7.42.73 (主) 0
B伺服器IP :100.7.42.173 1
C伺服器IP :100.7.42.176 2
對應虛擬ip為:
100.7.39.100 2
100.7.39.101 1
100.7.39.102 0
1、100.7.42.73節點Apache配置(其他節點配置相同,ServerName與實際節點相對應):
[[email protected]~]# vim /etc/httpd/conf/httpd.conf
/*在配置檔案最下方編輯*/
IncludeOptional conf.d/autoindex.conf //優化增加訪問共享目錄列表資訊
<Directory "/mnt/icfs/httptest">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted //允許匿名訪問共享目錄
</Directory>
<VirtualHost *:910>
ServerAdmin [email protected]
DocumentRoot /mnt/icfs/httptest/ //共享目錄
Options Indexes FollowSymLinks
<Location />
Options Indexes MultiViews
Dav On
</Location>
</VirtualHost>
2、100.7.42.73節點Nginx配置(叢集各個節點配置一樣):
在http段加入以下程式碼
upstream inspur.com { //inspur.com 為server引用
server 100.7.42.73:910;
server 100.7.42.173:910;
server 100.7.42.176:910;
}
server{
listen 8099;
server_name inspur.com;
location / {
#反向代理的地址
proxy_pass http://inspur.com;
#設定主機頭和客戶端真實地址,以便伺服器獲取客戶端真實IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
3,客戶端訪問
瀏覽器輸入:
http://虛擬IP:8099 /*通過Nginx反向代理訪問共享服務*/
http://虛擬IP:910 /*通過Apache訪問共享服務*/
五,Nginx與Apache效能對比
AB測試工具:
Nginx併發使用者數為1000,請求總數為1000,
Server Software: Nginx/1.9.2
Requests per second: 12267.02 [#/sec] (mean) #每秒請求數,伺服器的吞吐量
Time per request: 81.442 [ms] (mean) #使用者平均請求等待時間 時延
每個請求的平均處理時間為0.081ms,80%的請求是在38ms之內完成
http併發使用者數為1000,請求總數為1000,
Server Software: Apache/2.4.6
Requests per second: 1807.12 [#/sec] (mean)
Time per request: 553.367 [ms] (mean)
每個請求的平均處理時間為0.553ms,80%的請求是在227ms之內完成