nginx搭建负载均衡

负载均衡:针对web负载均衡简单的说就是将请求通过负债均衡软件或者负载均衡器将流量分摊到其它服务器。

负载均衡的分类如下图:

今天分享一下nginx实现负载均衡的实现,操作很简单就是利用了nginx的反向代理和upstream实现:

服务器名称 地址 作用
A服务器 192.168.0.212 负载均衡服务器
B服务器 192.168.0.213 后端服务器
C服务器 192.168.0.215 后端服务器

A服务器nginx配置如下:

 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服务器配置如下:

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日志进行查看请求是否在轮询服务器。

思考:负载均衡搭建是搭建成功了,但是也有问题

1.这样的架构会出现session无法共享的问题?

2.如果其中有一台后端服务器宕机了怎么处理?

这些问题后面会有文章进行说明

.

猜你喜欢

转载自www.cnblogs.com/lisqiong/p/9953034.html