负载均衡:针对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.若是其中有一台后端服务器宕机了怎么处理?负载均衡
这些问题后面会有文章进行说明
.