nginx负载均衡的相关配置

一台nginx的负载均衡服务器(172.25.254.131)

两台安装httpd作为web端

一、准备工作

1.1 安装nginx

yum -y install gcc openssl-devel pcre-devel    
useradd -s /sbin/nologin nginx 
cd ~/nginx-1.10.1/
./configure 
>--prefix=/usr/local/nginx 
>--user=nginx 
>--group=nginx 
>--with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module    #在安装pcre的时候,如果使用的是源码安装,则在这里需要只用pcre的位置
make && make install
ln -s /usr/local/nginx/sbin/nginx  /sbin/
nginx -t
nginx
nginx -s reload

1.2 安装appache

[root@web1 ~]# yum -y install httpd
[root@web1 ~]# systemctl restart httpd
[root@web1 ~]# netstat -ntlp |grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      80049/httpd 
[root@web1 ~]# echo 172.25.254.134>>/var/www/html/index.html
[root@web1 ~]# curl 172.25.254.134
172.25.254.134
[root@web1 ~]# firewall-cmd --add-port=80/tcp  --permanent
[root@web1 ~]# firewall-cmd --reload
[root@web1 ~]# firewall-cmd --list-all
ports: 80/tcp
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# systemctl restart httpd
[root@web2 ~]# netstat -ntlp |grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      80049/httpd 
[root@web1 ~]# echo 172.25.254.135>>/var/www/html/index.html
[root@web1 ~]# curl 172.25.254.135
172.25.254.135
[root@web1 ~]# firewall-cmd --add-port=80/tcp  --permanent
[root@web1 ~]# firewall-cmd --reload
[root@web1 ~]# firewall-cmd --list-all
ports: 80/tcp

二、配置

2.1 开始配置nginx的负载均衡

一般2000万pv以下的负载均衡都可以使用nginx实现,依赖于Nginx_http_upstream_module模块。所支持的代理方式有proxy_pass,fastcgi_pass,memcached_pass.

upstream模块应放于nginx.conf配置的http{}内。默认的算法是wrr权重轮询

参数:weight max_fails(根据应用场景,小的话,用户体验度高,但是要求节点足够多,同时在负载大于80%后,不会向该节点扔请求,大的话可以提高服务器的利用率 ),backup,fail_timeout,down

[root@lb01 ~]# cd /usr/local/nginx/conf/

[root@lb01 conf]# egrep -v "#|^$" nginx.conf.default 

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

[root@lb01 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf

[root@lb01 conf]# vim nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
upstream  web_pools{
#可以跟ip_hash;这样,backup不能使用,权重也没有效果,一个客户端永远访问一台 server
172.25.254.134:80 weight=5; server 172.25.254.135:80 weight=5; server 172.25.254.158:80 weight=5 backup; #nginx自带的一个高可用 }
#nginx自带判断功能,单节点故障,会自己剔除故障节点,节点故障修复,又会自己添加进来 server { listen
80; server_name www.lb01test.com; location / { root html; index index.html index.htm;
   proxy_set_header Host $host; #只带请求的主机名 proxy_pass http:
//web_pools; } } }

[root@lb01 conf]# nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@lb01 conf]# nginx -s reload

2.2 访问

[root@lb01 conf]# curl http://www.lbtest.com
172.25.254.135
[root@lb01 conf]# curl http://www.lbtest.com
172.25.254.134
[root@lb01 conf]# curl http://www.lbtest.com
172.25.254.135
[root@lb01 conf]# curl http://www.lbtest.com
172.25.254.134
[root@lb01 conf]# curl http://www.lbtest.com
172.25.254.135
[root@lb01 conf]# curl http://www.lbtest.com
172.25.254.134
[root@lb01 conf]# curl http://www.lbtest.com
172.25.254.135
[root@lb01 conf]# curl http://www.lbtest.com
172.25.254.134
[root@lb01 conf]# curl http://www.lbtest.com
172.25.254.135

三 、upstream核心模块和相关参数

3.1 算法

轮询:默认算法,每一个请求按顺序逐一分配到不同的后端服务器,如果后端服务器down掉了,则能自动剔除。

weight:权重,设置权重,用于后端服务器性能不均的情况,访问比率约等于权重之比

ip_hash:解决了session问题:每个请求按访问IP的hash结果分配,这样每个访客可以固定一个后端服务器。

fair(第三方)动态算法:

按照后端服务器的响应时间来分配请求,相应的时间短的有限分配,是比轮询和权重的更加智能的负载均衡算法,必须下载upstream_fair的模块

url_hash算法

按照访问的url的hash结果来分配请求,让每个url定向到同一个后端服务器,后端服务器Wie缓存服务器时的效果显著。在uupstream中加入hash语句,servevr语句中不能写入weight等其他参数,hash_method是使用的hash算法

url_hash安访问的url的hash结果来分配请求,是每个url定向到同一个后端服务器。可以进一步的提高后端缓存服务器的命中率,需要安装Nginx的hsah软件包

url_hash(web缓存节点)和Ip_hsah(回话保持类似)

least_conn:最少连接数,那个机器连接数少就分发

一致性Hash

max_conns=number:限制最大的并发连接,默认为0,没有限制

3.2 http_proxy_module

Proxy_pass指令属于ngx_http_proxy_module模块,此模块可以将请求转发到另一台服务器

proxy_set_header:设置后端的服务器获取用户的主机名或者真实的IP地址,以及代理着的真实IP

client_body_buffer_size:用于指定客户端请求主题缓冲区大小,可以理解为先保存到本地在传给用户

proxy_connect_timeout:表示与后端服务器连接的超时时间,及发起我吼等候的超时时间

proxy_send_timeout:表示后端服务器的数据回传时间,即在规定的时间之内后端服务器必须传完所有的数据,否则,Nginx将断开连接

proxy_read_timeout:是指Nginx从反向代理的后端服务器获取信息的时间,表示连接建立成功后,Nginx等后端服务器的响应时间,

Proxy_pass http://blog_server_pool:用于指定反向代理的服务器池

proxy_set_header Host $Host:当后端服务器上配置多个虚拟主机时,需要用到Header来区分反向代理哪一个主机

proxy_set_header X-Forwareded-For $remote_addr:如果侯丹Web服务器上的程序需要获取用户IP,从该Header头获取。 

原文地址:https://www.cnblogs.com/zyxnhr/p/10705726.html