Nginx負載均衡
阿新 • • 發佈:2020-09-07
一、Nginx負載均衡
1.為什麼做負載均衡
當我們的Web伺服器直接面向用戶,往往要承載大量併發請求,單臺伺服器難以負荷,我使用多臺Web伺服器組成叢集,前端使用Nginx負載均衡,將請求分散的打到我們的後端伺服器叢集中,實現負載的分發。那麼會大大提升系統的吞吐率、請求效能、高容災
往往我們接觸的最多的是SLB(Server Load Balance)負載均衡,實現最多的也是SLB、那麼SLB它的排程節點和服務節點通常是在一個地域裡面。那麼它在這個小的邏輯地域裡面決定了他對部分服務的實時性、響應性是非常好的。
所以說當海量使用者請求過來以後,它同樣是請求排程節點,排程節點將使用者的請求轉發給後端對應的服務節點,服務節點處理完請求後在轉發給排程節點,排程節點最後響應給使用者節點。這樣也能實現一個均衡的作用,那麼Nginx則是一個典型的SLB
2.負載均衡的叫法
負載均衡
負載
Load Balance
LB
3.公有云中叫法
1.SLB 阿里雲產品
2.LB 青雲產品
3.CLB 騰訊雲產品
4.ULB ucloud產品
4.常見的負載均衡軟體
Nginx
Haproxy
LVS
#LVS是最快的負載均衡軟體
5.負載均衡型別
1.四層負載均衡
所謂四層負載均衡指的是OSI七層模型中的傳輸層,那麼傳輸層Nginx已經能支援TCP/IP的控制,所以只需要對客戶端的請求進行TCP/IP協議的包轉發就可以實現負載均衡,那麼它的好處是效能非常快、只需要底層進行應用處理,而不需要進行一些複雜的邏輯。
2.七層負載均衡
七層負載均衡它是在應用層,那麼它可以完成很多應用方面的協議請求,比如我們說的http應用的負載均衡,它可以實現http資訊的改寫、頭資訊的改寫、安全應用規則控制、URL匹配規則控制、以及轉發、rewrite等等的規則,所以在應用層的服務裡面,我們可以做的內容就更多,那麼Nginx則是一個典型的七層負載均衡SLB
二、Nginx負載均衡配置
Nginx要實現負載均衡需要用到proxy_pass代理模組配置.
Nginx負載均衡與Nginx代理不同地方在於,Nginx的一個location僅能代理一臺伺服器,而Nginx負載均衡則是將客戶端請求代理轉發至一組upstream虛擬服務池.
1.語法模組 ngx_http_upstream_module
Syntax: upstream name { ... } Default: — Context: http upstream backend { server backend1.example.com weight=5; server backend2.example.com:8080; server backup1.example.com:8080 backup; server backup2.example.com:8080 backup; } server { location / { proxy_pass http://backend; } }
2.環境準備
主機 | 外網ip | 身份 |
---|---|---|
lb01 | 10.0.0.4,172.16.1.4 | 負載均衡 |
web01 | 172.16.1.7 | web |
web03 | 172.16.1.9 | web |
3.操作web01
1)配置nginx
[root@web01 conf.d]# vim linux.node.com.conf server { listen 80; server_name linux.node.com; charset utf-8; location / { root /code/node; inde index.html; } }
2)配置站點
[root@web01 conf.d]# mkdir /code/node [root@web01 conf.d]# echo "我是web01......" > /code/node/index.html
3)配置hosts
10.0.0.7 linux.node.com #重啟訪問 [root@web01 conf.d]# systemctl restart nginx
4.操作web03
[root@web03 conf.d]# vim linux.node.com.conf server { listen 80; server_name linux.node.com; charset utf-8; location / { root /code/node; inde index.html; } }
2)配置站點
[root@web03 conf.d]# mkdir /code/node [root@web03 conf.d]# echo "我是web03......" > /code/node/index.html
3)配置hosts
10.0.0.9 linux.node.com #重啟訪問 [root@web03 conf.d]# systemctl restart nginx
5.配置負載均衡配置檔案
[root@lb01 conf.d]# vim node_proxy.conf upstream web { server 172.16.1.7:80; server 172.16.1.8:80; } server { listen 80; server_name linux.node.com; location / { proxy_pass http://web; include proxy_params; } }
6.配置優化檔案
[root@Nginx ~]# vim /etc/nginx/proxy_params proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k;
7.訪問頁面測試
三、nginx 排程演算法
排程演算法 | 概述 |
---|---|
輪詢 | 按時間順序逐一分配到不同的後端伺服器(預設) |
weight | 加權輪詢,weight值越大,分配到的訪問機率越高 |
ip_hash | 每個請求按訪問IP的hash結果分配,這樣來自同一IP的固定訪問一個後端伺服器 |
url_hash | 按照訪問URL的hash結果來分配請求,是每個URL定向到同一個後端伺服器 |
least_conn | 最少連結數,那個機器連結數少就分發 |
1.輪詢配置方法
upstream node { server 172.16.1.7:80; server 172.16.1.8:80; }
2.加權輪詢配置方法
#訪問根據配置的權重比例進行分配 upstream node { server 172.16.1.7:80 weight=5; server 172.16.1.8:80 weight=1; }
3.ip_hash的配置方法
#根據訪問的來源IP分配至同一臺伺服器 upstream node { server 172.16.1.7:80; server 172.16.1.8:80; ip_hash; } #經常使用這種方式進行會話保持
四、負載均衡後端狀態
狀態 | 概述 |
---|---|
down | 當前的server暫時不參與負載均衡 |
backup | 預留的備份伺服器 |
max_fails | 允許請求失敗的次數 |
fail_timeout | 經過max_fails失敗後, 服務暫停時間 |
max_conns | 限制最大的接收連線數 |
1.down狀態配置測試
[root@lb01 ~]# vim /etc/nginx/conf.d/proxy.conf upstream web { server 172.16.1.7:80 down; server 172.16.1.9:80; } #一般在程式碼上線或維護伺服器時使用該狀態
2.backup狀態測試
[root@lb01 ~]# vim /etc/nginx/conf.d/proxy.conf upstream web { server 172.16.1.7:80; server 172.16.1.9:80; server 172.16.1.10:80 backup; server 172.16.1.11:80 backup; }
3.max_fails配置
[root@lb01 ~]# vim /etc/nginx/conf.d/proxy.conf upstream web { server 172.16.1.7:80 max_fails=3 fail_timeout=10s; server 172.16.1.9:80; }
4.測試max_conns最大TCP連線數
[root@lb01 ~]# vim /etc/nginx/conf.d/proxy.conf upstream web { server 172.16.1.7:80 max_conns=10; server 172.16.1.9:80; }
五、負載均衡結合專案
1.配置blog的負載均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/blog.conf upstream blog { server 172.16.1.7:80; server 172.16.1.9:80; } server { listen 80; server_name linux.blog.com; location / { proxy_pass http://blog; include /etc/nginx/proxy_params; } } [root@lb01 ~]# systemctl restart nginx #配置hosts,訪問測試
2.配置wecenter的負載均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/zh.conf upstream zh { server 172.16.1.7:80; server 172.16.1.9:80; } server { listen 80; server_name linux.zh.com; location / { proxy_pass http://zh; include /etc/nginx/proxy_params; } }
3.負載均衡常見錯誤
1)錯誤
如果後端伺服器返回報錯,負載均衡仍然會將請求分配到出錯的web伺服器,因為負載均衡只會根據排程演算法將請求分配到後端,不會進行判斷後端是否正常
2)解決錯誤的模組語法
Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
Default: proxy_next_upstream error timeout;
Context: http, server, location
3)配置方法
[root@lb01 ~]# vim /etc/nginx/conf.d/zh.conf upstream zh { server 172.16.1.7:80; server 172.16.1.9:80; } server { listen 80; server_name linux.zh.com; location / { proxy_pass http://zh; include /etc/nginx/proxy_params; proxy_next_upstream http_502 error timeout; } }
六、Nginx負載均衡健康檢查
在Nginx官方模組提供的模組中,沒有對負載均衡後端節點的健康檢查模組,但可以使用第三方模組。
`nginx_upstream_check_module`來檢測後端服務的健康狀態。
1.安裝依賴包
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
2.下載nginx原始碼包以及nginx_upstream_check模組第三方模組
[root@lb02 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz [root@lb02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
3.解壓nginx原始碼包以及第三方模組
[root@lb02 ~]# tar xf nginx-1.14.2.tar.gz [root@lb02 ~]# unzip master.zip
4.進入nginx目錄,打補丁(nginx的版本是1.14補丁就選擇1.14的,p1代表在nginx目錄,p0是不在nginx目錄)
[root@lb02 ~]# cd nginx-1.14.2/ [root@lb02 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch [root@lb02 nginx-1.14.2]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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 --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' [root@lb02 nginx-1.14.2]# make && make install
5.在已有的負載均衡上增加健康檢查的功能
[root@lb02 /etc/nginx]# vim /etc/nginx/nginx.conf http { include conf.d/*.conf; ... ... } [root@lb02 /etc/nginx]# mkdir /etc/nginx/conf.d [root@lb01 conf.d]# cat proxy_web.conf upstream web { server 172.16.1.7:80 max_fails=2 fail_timeout=10s; server 172.16.1.8:80 max_fails=2 fail_timeout=10s; check interval=3000 rise=2 fall=3 timeout=1000 type=tcp; #interval 檢測間隔時間,單位為毫秒 #rise 表示請求2次正常,標記此後端的狀態為up #fall 表示請求3次失敗,標記此後端的狀態為down #type 型別為tcp #timeout 超時時間,單位為毫秒 } server { listen 80; server_name linux.web.com; location / { proxy_pass http://web; include proxy_params; } location /upstream_check { check_status; } } #編輯優化檔案 [root@lb02 /etc/nginx]# vim /etc/nginx/proxy_params proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 60s; proxy_read_timeout 60s; proxy_send_timeout 60s; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 8 128k;
6.建立使用者和目錄
[root@lb02 /etc/nginx]# groupadd nginx -g 666 [root@lb02 /etc/nginx]# useradd nginx -u 666 -g 666 [root@lb02 /etc/nginx]# mkdir /var/cache/nginx/
7.啟動並訪問
[root@lb02 /etc/nginx]# /usr/sbin/nginx #配置hosts 10.0.0.5 linux.web.com
七、Nginx負載均衡會話保持
在使用負載均衡的時候會遇到會話保持的問題,可通過如下方式進行解決。
1.使用nginx的ip_hash,根據客戶端的IP,將請求分配到對應的IP上
2.基於服務端的session會話共享(file+NFS,MySQL,redis)
1.session共享的方法
1.把多臺機器的session檔案掛載到NFS
2.通過程式將session儲存到MySQL資料庫
3.通過程式將session儲存到redis
2.搭建第一臺phpmyadmin
1)上傳包
[root@web01 ~]# cd /code/ [root@web01 /code]# rz phpMyAdmin-4.9.0.1-all-languages.zip
2)解壓
[root@web01 /code]# unzip phpMyAdmin-4.9.0.1-all-languages.zip [root@web01 /code]# mv phpMyAdmin-4.9.0.1-all-languages php
3)配置程式碼
[root@web01 /code]# cp php/config.sample.inc.php php/config.inc.php [root@web01 /code]# vim php/config.inc.php $cfg['Servers'][$i]['host'] = '172.16.1.51';
4)配置nginx
[root@web01 /code]# vim /etc/nginx/conf.d/linux.php.com.conf server { listen 80; server_name linux.php.com; root /code/php; location / { index index.php; } location ~* \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
5)重啟訪問
[root@web01 /code]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 /code]# systemctl restart nginx #配置hosts 10.0.0.7 linux.php.com
6)訪問頁面錯誤
#報錯 session_start(): open(SESSION_FILE, O_RDWR) failed: Permission denied (13) session_start(): Failed to read session data: files (path: /var/lib/php/session) #解決: [root@web01 /code]# chown -R www.www /var/lib/php/session
7)再次訪問測試
#如果忘記資料庫遠端連線密碼 [root@db01 ~]# mysql -uroot -pLinhd@123 MariaDB [(none)]> grant all on *.* to root@'172.16.1.%' identified by '123'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> select user,host,password from mysql.user;
3.同步配置到第二臺伺服器
1)推送配置和站點
#推送站點目錄 [root@web01 /code]# scp -r /code/php 172.16.1.9:/code/ #推送nginx配置 [root@web01 /code]# scp /etc/nginx/conf.d/linux.php.com.conf 172.16.1.9:/etc/nginx/conf.d/
2)重啟訪問
[root@web03 ~]# systemctl restart nginx #配置hosts 10.0.0.9 linux.php.com
3)授權目錄
#報錯 session_start(): open(SESSION_FILE, O_RDWR) failed: Permission denied (13) session_start(): Failed to read session data: files (path: /var/lib/php/session) #解決: [root@web03 /code]# chown -R www.www /var/lib/php/session
4.配置負載均衡
1)配置
[root@lb01 ~]# cp /etc/nginx/conf.d/blog.conf /etc/nginx/conf.d/php.conf [root@lb01 ~]# vim /etc/nginx/conf.d/php.conf upstream php { server 172.16.1.7:80; server 172.16.1.9:80; } server { listen 80; server_name linux.php.com; location / { proxy_pass http://php; include /etc/nginx/proxy_params; } }
2)重啟並訪問
[root@lb01 ~]# systemctl restart nginx #配置hosts 10.0.0.4 linux.php.com
5.使用redis實現session共享
1)安裝redis
[root@db01 ~]# yum install -y redis
2)配置redis
[root@db01 ~]# vim /etc/redis.conf bind 127.0.0.1 172.16.1.51
3)啟動redis
[root@db01 ~]# systemctl start redis #檢查啟動 [root@db01 ~]# netstat -lntp tcp 0 0 172.16.1.51:6379 0.0.0.0:* LISTEN 29104/redis-server tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 29104/redis-server
4)配置PHP服務將session儲存到redis
[root@web01 /code]# vim /etc/php.ini #原配置 session.save_handler = files session.save_handler = redis session.save_path = "tcp://172.16.1.51:6379" [root@web01 /code]# vim /etc/php-fpm.d/www.conf #最下面幾行註釋 ;php_value[session.save_handler] = files ;php_value[session.save_path] = /var/lib/php/session
5)重啟php
[root@web01 /code]# systemctl restart php-fpm [root@web03 /code]# systemctl restart php-fpm
6)訪問測試
7)redis檢視seesion
[root@db01 ~]# redis-cli 127.0.0.1:6379> keys * 1) "PHPREDIS_SESSION:8b8721df0b5736149ea0c716f05773e9" 2) "PHPREDIS_SESSION:b59336d7a1a053c6d26c2550032c1609 127.0.0.1:6379> TTL PHPREDIS_SESSION:b59336d7a1a053c6d26c2550032c1609 (integer) 1199