您的位置:首页 > 运维架构 > Tomcat

初识Nginx,简单配置实现负载均衡(ubuntu + Nginx + tomcat)

2015-07-13 16:23 585 查看
  工作需要,研究了一下Nginx的反向代理实现负载均衡,网上搜了一下教程,大多含糊不清,所以写下这个,权当总结,方便日后查看,如果能恰好帮到一些需要的人,那就更好了

  先说需求,域名指向搭建了Nginx的服务器A,然后由A负载均衡到装有tomcat的服务器B和服务器C(不知道“由A负载均衡到B、C”这种说法对不对)

          


  先说环境:

      服务器ABC均为优麒麟(ubuntukylin)14.04

      服务器A装有Nginx1.6.3

      服务器B、C使用tomcat6

  准备工作:

      1.设置服务器A的IP为192.168.1.109,服务器B的IP为192.168.1.110, 服务器B的IP为192.168.1.111

      2.查看系统是否安装G++编译器  

g++ --version


      如果显示版本信息,则不需要安装。如果提示没有找到,则首先安装g++编译器

       

sudo apt-get install g++


      3.安装PCRE,PCRE库是实现Perl式正则表达式的基础

cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz tar -zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make
make install


      4.安装zlib库

cd /usr/local/src
wget http://zlib.net/zlib-1.2.8.tar.gz tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install


      5.安装Nginx

  Nginx有稳定版和主线版,我选用的是稳定版1.6.3,(http://nginx.org/en/download.html

 

cd /usr/local/src
wget http://nginx.org/download/nginx-1.6.3.tar.gz tar -zxvf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure --prefix=/usr/local/nginx  //这里表示将Nginx安装在/usr/local/nginx目录下
make
make install


  至此,Nginx安装完成,下面命令为启动nginx:

cd /usr/local/nginx/sbin
./nginx


启动之后,浏览器输入服务器A的IP:192.168.1.109,如果出现一下画面,则表示nginx安装成功


  

下面要配置反向代理负载均衡

配置服务器B和C,使得访问服务器B提示“this is page A”,访问服务器C提示“this is page B”

然后编辑nginx的配置文件,保存

user  comdev comdev;

worker_processes 10;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#最大文件描述符
worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 51200;
}

http
{
include       mime.types;
default_type  application/octet-stream;
keepalive_timeout 120;
tcp_nodelay on;
upstream  192.168.1.109  {
server   192.168.1.110:8080;
server   192.168.1.111:8080;
}

server
{
listen  80;
server_name  192.168.1.109;

location / {
proxy_pass       http://192.168.1.109; proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}
}
}


重启nginx

方法参考:http://www.cnblogs.com/derekchen/archive/2011/02/17/1957209.html

然后再访问192.168.1.109,则会显示“this is page A”,刷新,则会显示“this is page B”

至此,nginx反向代理实现负载均衡的简单实例就完成了

当然,nginx还可配置N多内容,待我一一摸索然后写上来

整理自:
http://www.nginx.cn/install http://blog.csdn.net/user_longling/article/details/44646465 http://developer.51cto.com/art/201407/446626.htm http://zyan.cc/post/306/ http://www.php100.com/html/program/nginx/2013/0905/5525.html### http://ari.iteye.com/blog/833153 http://www.cnblogs.com/derekchen/archive/2011/02/17/1957209.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: