nginx负载均衡

  负载均衡(load balance)方案很多,基于硬件最有名的F5,软件的有lvs,nginx等等,其中lvs是工作TCP/IP四层协议上,基于netfilter框架进行转发,性能据说能达到F550%
nginx工作在七层应用层协议上,所以性能上不如lvs,但是配置简单,不如lvs需要了解相应的网络知识。
这篇文章重要是介绍一下nginx,lvs如果有时间我也会续上。

 

目的:nginx实现负载均衡

环境:三台vm,分别是nginx(192.168.1.108),apache1(192.168.1.109),apache2(192.168.1.110)

 

Apache环境配置:

为了简单,采用yum方式安装;

配置ip,关闭防火墙,setenforce 0;

Yum -y install “Development Tools”

Yum -y httpd

vi /var/www/html/index.html

添加<h1>t1<h1>

启动apache服务,输入http://192.168.1.109/ 会出现t1页面

 

同理配置apache2输入http://192.168.1.110/ 会出现t2页面

 

至此后端测试服务器配置完成!

Nginx配置

下载地址http://nginx.org,下载稳定版

 

 

 

先安装几个依赖库和依赖包

# yum -y groupinstall "Development Tools

# yum -y install pcre-devel openssl-devel

添加运行nginx用户,最好不要用root

# groupadd -r nginx

# useradd -r -g nginx nginx

 

生成配置文件,这是网上搜的,可以根据需要./configuration --help 

# ./configure 

  --prefix=/usr 

  --sbin-path=/usr/sbin/nginx 

  --conf-path=/etc/nginx/nginx.conf 

  --error-log-path=/var/log/nginx/error.log 

  --http-log-path=/var/log/nginx/access.log 

  --pid-path=/var/run/nginx/nginx.pid  

  --lock-path=/var/lock/nginx.lock 

  --user=nginx 

  --group=nginx 

  --with-http_ssl_module 

  --with-http_flv_module 

  --with-http_stub_status_module 

  --with-http_gzip_static_module 

  --http-client-body-temp-path=/var/tmp/nginx/client/ 

  --http-proxy-temp-path=/var/tmp/nginx/proxy/ 

  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ 

  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi 

  --http-scgi-temp-path=/var/tmp/nginx/scgi 

  --with-pcre 

  --with-file-aio

#make && make install 

 

至此nginx,安装也完成了!

最好吧nginx做成服务,实现开机自启动。可以参考这老兄博客

http://blog.chinaunix.net/uid-20622737-id-3464285.html

配置就更简单了,参见官方文档

http://nginx.org/en/docs/http/load_balancing.html

我的配置如下:

 

然后重启nginx,连续刷新http://192.168.1.108/,是不是在t1t2之间切换.

最后关于策略参考,当然官方文档为首选

http://nginx.org/en/docs/http/load_balancing.html#nginx_load_balancing_methods

或者下面这篇http://www.cnblogs.com/dyllove98/archive/2013/07/13/3188450.html

原文地址:https://www.cnblogs.com/virgree/p/5207714.html