在之前的文章中,我们通过服务代理的方式已经看到了Nginx有作为负载均衡服务的功能了,在这篇文章中,我会讲解Nginx的基本的负载均衡的使用、backup状态演示、轮询策略和加权轮询、负载均衡策略ip_hash的方式、负载均衡策略url_hash策略。
通过在Nginx服务器中配置proxy_pass和upstream server组可以实现最基本的负载均衡。Nginx收到客户端的请求后,将请求代理路由到upstream server中配置的各个服务中,默认会使用轮询的策略依次访问各个服务。

upstream name {...},而且只能配置在http中
server1.conf的配置文件:
server {
    listen       8001;
    server_name  localhost;
    #charset koi8-r;
    access_log  /var/log/nginx/log/server1.access.log  main;
    location / {
        root   /opt/app/code1;
        index  index.html index.htm;
    }
端口8001,访问的是/opt/app/code1路径下的index.html文件
server2.conf配置文件
####################
#Author:jeson@imoocc.com
####################
server {
    listen       8002;
    server_name  localhost;
    #charset koi8-r;
    access_log  /var/log/nginx/log/server2.access.log  main;
    location / {
        root   /opt/app/code2;
        index  index.html index.htm;
    }
端口8002,访问的是/opt/app/code2路径下的index.html文件
server3.conf配置文件
server {
    listen       8003;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/server2.access.log  main;
    location / {
        root   /opt/app/code3;
        index  index.html index.htm;
    }
端口8003,访问的是/opt/app/code2路径下的index.html文件
####################
#Author:zzm
####################
    upstream zzm {
        server localhost:8001;
        server localhost:8002;
        server localhost:8003;
    }
server {
    listen       80;
    server_name  localhost ;
    #charset koi8-r;
    access_log  /var/log/nginx/test_proxy.access.log  main;
    resolver  8.8.8.8;
    location / {
       proxy_set_header Host   $host;       proxy_pass http://zzm; 
    } 这里的配置中如果我们监听80端口,如果用户直接访问IP:80/,那么就会负载均衡到8001和8002和8003中的一个上面去。



实战一完成,说明我们最最基本的负载均衡目的已经达成。
    upstream zzm {
        server localhost:8001 down;
        server localhost:8002 backup;
        server localhost:8003 max_fails=1 fail_timeout=10s;
    }
访问结果只有server3,因为server1已经down了,server2是备份

我们用iptables命令来限制对8003的访问:iptables -I INPUT -p tcp --dport 8003 -j DROP
此刻再访问localhost:80的结果是:

说明backup生效了。

    upstream zzm {
        server localhost:8001;
        server localhost:8002 weight=5;
        server localhost:8003;
    }
这里我们就只把配置贴出来,具体的演示结果就不展示了
upstream zzm {
        ip_hash;
        server localhost:8001;
        server localhost:8002;
        server localhost:8003;
    }
这里就多配置了ip_hash这个关键字,是根据客户端的ip来进行负载均衡的,但是如果Nginx的前置是路由器的话,它会根据路由器IP来做负载均衡,所以用的不多,我们这里也不做演示。
有时候我们希望用户访问某一些关键路径的时候只能路由到其中的一台服务器上,而不是随机落到一台服务器上,这个时候我们的配置就是这样的:
    upstream zzm {
        hash $request_uri;
        server localhost:8001;
        server localhost:8002;
        server localhost:8003;
    }
无论如何刷新url1.html,都只会到server3上面去访问,而不会到其他两台server中去访问,如下图所示:

Nginx作为负载均衡服务就讲到这里~~Nginx的场景实践篇就讲到这里,接下来我们将深度学习Nginx
作者:没有色彩的狼人
链接:https://www.jianshu.com/p/14c73c5d7e1d
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
location / {
proxy_pass http://taishan;      #请求转向taishan定义的服务器列表
proxy_set_header Host $host;    #将请求头转发给后端服务器
proxy_set_header X-Forward-For $remote_addr;    #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
                          #其他相关配置入下,可以根据需要添加配置
#client_max_body_size 10m;             #允许客户端请求的最大单文件字节数
#client_body_buffer_size 128k;            #缓冲区代理缓冲用户端请求的最大字节数
#proxy_connect_timeout 90;             #nginx跟后端服务器连接超时时间(代理连接超时)
#proxy_send_timeout 90;              #后端服务器数据回传时间(代理发送超时)
#proxy_read_timeout 90;              #连接成功后,后端服务器响应时间(代理接收超时)
#proxy_buffer_size 4k;                #设置代理服务器(nginx)保存用户头信息的缓冲区大小
#proxy_buffers 4 32k;                #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
#proxy_busy_buffers_size 64k;            #高负荷下缓冲大小(proxy_buffers*2)
#proxy_temp_file_write_size 64k;          #设定缓存文件夹大小,大于这个值,将从upstream服务器传
# root html;
# index index.php index.html index.htm;
upstream apachephp  {
    server ip:8080; #Apache
}
## Start www.redis.com.cn ##
server {
    listen 80;
    server_name  www.redis.com.cn;
    access_log  logs/redis.access.log  main;
    error_log  logs/redis.error.log;
    root   html;
    index  index.html index.htm index.php;
    ## send request back to apache ##
    location / {
        proxy_pass  http://apachephp;
        #Proxy Settings
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}
原文:https://www.cnblogs.com/lgj8/p/13253639.html