負載均衡:針對web負載均衡簡單的說就是將請求經過負債均衡軟件或者負載均衡器將流量分攤到其它服務器。php
負載均衡的分類以下圖:html
今天分享一下nginx實現負載均衡的實現,操做很簡單就是利用了nginx的反向代理和upstream實現:nginx
服務器名稱 | 地址 | 做用 |
A服務器 | 192.168.0.212 | 負載均衡服務器 |
B服務器 | 192.168.0.213 | 後端服務器 |
C服務器 | 192.168.0.215 | 後端服務器 |
A服務器nginx配置以下:web
1 upstream apiserver { 2 server 192.168.0.213:8081 weight=1 max_fails=2 fail_timeout=3; 3 server 192.168.0.215:8082 weight=1 max_fails=2 fail_timeout=3; 4 } 5 6 server { 7 listen 80; 8 server_name api.test.com; 9 10 location / { 11 proxy_pass http://apiserver; 12 13 } 14 15 location ~ /\.ht { 16 deny all; 17 } 18 }
B服務器配置以下:後端
1 server { 2 listen 8081; 3 server_name 192.168.0.213; 4 set $root_path '/data/wwwroot/Api/public/'; 5 root $root_path; 6 index index.php index.html index.htm; 7 access_log /data/wwwlogs/access_log/api.8081.log; 8 try_files $uri $uri/ @rewrite; 9 location @rewrite { 10 rewrite ^/(.*)$ /index.php?_url=/$1; 11 } 12 13 location ~ \.php { 14 fastcgi_pass 127.0.0.1:9000; 15 fastcgi_index index.php; 16 include /usr/local/nginx/conf/fastcgi_params; 17 fastcgi_param PHALCON_ENV dev; 18 fastcgi_split_path_info ^(.+\.php)(/.+)$; 19 fastcgi_param PATH_INFO $fastcgi_path_info; 20 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 21 } 22 }
C服務器配置以下:api
server { listen 8082; server_name 192.168.0.215; set $root_path '/data/wwwroot/Api/public/'; root $root_path; index index.php index.html index.htm; access_log /data/wwwlogs/access_log/api.8081.log; try_files $uri $uri/ @rewrite; location @rewrite { rewrite ^/(.*)$ /index.php?_url=/$1; } location ~ \.php { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include /usr/local/nginx/conf/fastcgi_params; fastcgi_param PHALCON_ENV dev; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
到期負載均衡搭建完成,測試的能夠訪問搭建的域名地址,而後在對應的後端服務器打印access的log日誌進行查看請求是否在輪詢服務器。服務器
思考:負載均衡搭建是搭建成功了,可是也有問題session
1.這樣的架構會出現session沒法共享的問題?架構
2.若是其中有一臺後端服務器宕機了怎麼處理?負載均衡
這些問題後面會有文章進行說明
.