3臺VM虛擬機,1臺作負載均衡,2臺作RS。html
HOSTNAME | IP | 說明 |
lb01 | 192.168.5.210 | 主負載均衡器 |
web01 | 192.168.5.212 | web01服務器 |
web02 | 192.168.5.213 | web02服務器 |
系統:CentOS 6.9 x86_64nginx
軟件:nginx-1.15.2.tar.gz(http://nginx.org/download/nginx-1.15.2.tar.gz)web
3臺服務器均安裝Nginx。算法
yum -y install openssl openssl-devel pcre pcre-devel
useradd nginx -s /sbin/nologin -M tar zxvf nginx-1.15.2.tar.gz ./configure --user=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make make install
在兩臺web服務器上操做,配置以下:windows
[root@web01 nginx-1.15.2]# cat /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.test.com; location / { root html/test; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
mkdir /usr/local/nginx/html/test echo "www.test.com 212" > /usr/local/nginx/html/test/index.html
在windows客戶端測試,需先開通服務器防火牆的80端口,或關閉防火牆。後端
在lb01服務器上操做,配置以下:瀏覽器
[root@lb01 nginx-1.15.2]# cat /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; keepalive_timeout 65; upstream test_server_pools { server 192.168.5.212:80 weight=1; server 192.168.5.213:80 weight=1; } server { listen 80; server_name www.test.com; location / { proxy_pass http://test_server_pools;
} error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
官方示例:緩存
upstream backend {
server backend1.example.com weight=5;
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
server backup1.example.com backup;
}
server的參數說明:服務器
upstream 模塊內參數 | 參數說明 |
server 127.0.0.1:8080 | 能夠是IP或域名,默認端口80,高併發場景下,IP可換成域名,經過DNS作負載均衡 |
weight=1 | 表明服務器權重,默認值是1,數字越大表示接受的請求比例越大 |
max_fails=1 | nginx嘗試鏈接後端主機的次數,默認值是1,連續嘗試失敗後,會將請求轉發給後端正常的服務器 |
backup | 標誌此服務器做爲備份服務器,主服務器所有宕機時,向備份服務器轉發請求;負載調度算法使用ip_hash時不可用 |
fail_timeout=10s | 定義鏈接後端服務器失敗以後,距離下次檢查的時間,默認值是10s |
down | 標誌服務器永遠不可用併發 |
Nginx 反向代理重要參數 | 參數說明 |
proxy_pass http://server_pools; | 把請求轉發至定義好的服務器池 |
proxy_set_header Host $Host | 向後端服務器發送請求時加入 host 字段信息,可識別代理的哪一個虛擬主機 |
proxy_set_header X-Forwarded-For $remote_addr | 向後端服務器發送請求時加入 X-Forwarded-For 字段信息,用於後端服務器接收記錄真實的用戶IP,而非代理服務器IP |
可把參數寫成一個文件,使用 include 包含,看起來更規範。
location / { proxy_pass http://test_server_pools; include proxy.conf; }
[root@lb01 conf]# cat proxy.conf proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
一、修改hosts地址進行測試,Windows系統hosts文件路徑:C:\Windows\System32\drivers\etc
192.168.5.210 www.test.com
二、瀏覽器端採用無緩存刷新頁面,請求配均勻的分配到後端服務器。