nginx之反向代理 负载均衡

负载均衡策略:
1.轮循
2.加权轮循
3.ip hash

[root@nginx conf]# vim /usr/local/lnmp/nginx/conf/nginx.conf

    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        #tcp_nopush     on;
        keepalive_timeout  65;
        #gzip  on;

        upstream uplook {
            server 192.168.10.11:443;
            server 192.168.10.12;
            server 192.168.10.13;
        }
        server {
            listen       80;
            server_name  www.nginx.com;
            index        index.html index.htm;
            location / {
                  proxy_pass http://uplook;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
       }

如果真实WEB服务的性能不同,为了保证每个nginx-server都能够满足最大的访问.
可以加入权重,权重值越高,分到的请求越多.

[root@nginx conf]# vim /usr/local/lnmp/nginx/conf/nginx.conf
    
    upstream uplook {
         server 192.168.10.11 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.10.12 weight=2;
         server 192.168.10.13 backup;            \\backup:备份,其他服务器宕机后启用
    }

为了保证在短时间内,同一个客户端的请求不被分配到其他的nginx-server上?

[root@nginx conf]# vim nginx.conf
   
    upstream uplook {
        ip_hash;
        server 172.16.0.10;
        server 172.16.5.100;
        server 172.16.16.100;
    }

客户端测试:
一直得到同一个页面.

nginx+(apache|nginx) ,WEB服务器获取客户端真实IP
代理服务器:

location / {
          #root   html;
          #index  index.html index.htm;
          proxy_pass http://webs;
          proxy_set_header X-Real-ip $remote_addr;
      }

WEB服务器:
A. 后端apache:
LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{X-Real-ip}i” “%{User-Agent}i”” combined

B.后端nginx:

  1. 安装real-ip模块
    1-1 查看原来安装的编译参数:
    # nginx -V
         nginx version: nginx/1.8.1
         built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
         built with OpenSSL 1.0.1e-fips 11 Feb 2013
         TLS SNI support enabled
         configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
    
    1-2 在原编译参数的基础上加上real-ip模块
        # cd /package/nginx-1.8.1/ 
        # ./configure   --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
        
    1-3 编译(不要安装)
        # make       \\make之后会在objs目录下生成一个新的nginx
        
    1-4 将新生成的nginx命令覆盖掉原命令
        # rm -rf  /usr/local/nginx/sbin/nginx
        # cp  nginx  /usr/local/nginx/sbin/            
            
  2.修改主配置文件        
    location / {
            root   html;
            index  index.html index.htm;
            set_real_ip_from 10.10.10.11;
            real_ip_header X-Real-ip;
        }
   
   3.重启服务

猜你喜欢

转载自blog.csdn.net/qq_43377292/article/details/86493491