码迷,mamicode.com
首页 > 其他好文 > 详细

nginx负载均衡

时间:2018-04-13 21:25:20      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:nginx ip_hash sticky

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。
Nginx作为负载均衡服务器优点:
1.Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP2.代理服务器对外进行服务。
2.理静态文件,索引文件以及自动索引;打开文件描述符缓冲。
3.无缓存的反向代理加速,简单的负载均衡和容错。
4.FastCGI,简单的负载均衡和容错。
5.模块化的结构。
配置环境: 操作环境:rhel6.5
调度器:172.25.40.5 server5.example.com
服务器:172.25.40.2 server2.example.com
172.25.40.3 server3.example.com
客户端本地解析:
vim /etc/hosts
172.25.40.5 www.westos.org westos.org bbs.westos.org

Ngin 源码安装

tar zxf nginx-1.12.0.tar.gz
cd nginx-1.12.0
隐藏版本号:
cd nginx-1.12.0/src/core/
vim nginx.h
#define NGINX_VER          "nginx"
取消debug
cd /root/nginx-1.12.0/auto/cc
vim gcc
#debug
#CFLAGS="$CFLAGS -g"
cd nginx-1.12.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

技术分享图片

Nginx 四种方式的负载均衡配置:
1.基于轮询(Round-Robin)方式的负载均衡:

cd /usr/local/nginx/conf
vim nginx.conf
user  nginx nginx;     
worker_processes  2;
worker_cpu_affinity 01 10;   绑定cpu
events {
    worker_connections  65535;       #并发连接数
}
http {
upstream westos{
        server 172.25.40.2:80;
        server 172.25.40.3:80;
        }
 server{
                listen 80;
                server_name www.westos.org;
                location / {
                        proxy_pass http://westos;
                }

        }
}

在配置并发连接数时,修改/etc/security/limits.conf文件后才会生效。

 vim /etc/security/limits.conf
nginx           -       nofile          65535

测试页面:
技术分享图片

技术分享图片

关闭server2 server3端的httpd服务,浏览器页面自动调转到调度器本地nginx服务端。

http {
        upstream westos{
        server 172.25.40.2:80;
        server 172.25.40.3:80;
        Server172.25.40.5:80 backup;
        }
}

技术分享图片

vim /usr/local/nginx/conf/nginx.conf
http {
        upstream westos{
        server 172.25.40.2:80;
        server 172.25.40.3:80;
        server localhost:8080 backup;
        }
        server{
                listen 8080;
                server_name localhost:8080;
                charset utf-8;
                location / {
                        root /backup;
                        index index.html;
                }

        }
}

技术分享图片

2.基于权重方式的负载均衡
配置权重:

vim /usr/local/nginx/conf/nginx.conf
http {
        upstream westos{
        server 172.25.40.2:80 weight=2;
        server 172.25.40.3:80;
        server localhost:8080 backup;
        }
}

技术分享图片
3.基于ip_hash方式的负载均衡:使用长连接,确保一个客户只和一台服务器通信

vim /usr/local/nginx/conf/nginx.conf
http {
        upstream westos{
        server 172.25.40.2:80;
        server 172.25.40.3:80;
        ip_hash;
        }
}

测试结果:
技术分享图片

4.使用nginx sticky实现基于cookie的负载均衡
Sticky 模块
cp /usr/local/nginx/conf/nginx.conf /opt/
重新安装nginx,因当前版本过高,不能添加sticky模块,现在安装nginx-1.10.1版本。
执行前面安装的步骤,这里不再说明。需要注意的是在编译时要添加--add-module=PATH参数,模块路径是解压的nginx-sticky-module-ng.tar.gz的解压路径。

cd /root/nginx-1.10.1
 ./configure --help --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-sticky-module-ng
make && make install
cp /opt/nginx.conf /usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf
http {
        upstream westos{
        sticky;
        server 172.25.40.2:80;
        server 172.25.40.3:80;
        }
}

nginx -t #检测语法
nginx -s reload #重新加载服务
注意:
1.nginx和apache不同,nginx每次安装一个新的模块都需要重新编译一次,编译完成之后将nginx这一个文件拷贝到sbin下面即可。
2.如果在reload时出现下面报错时,解决办法是:
技术分享图片

nginx负载均衡

标签:nginx ip_hash sticky

原文地址:http://blog.51cto.com/12183531/2103249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!