centos 安装nginx + 多个tomcat负载均衡

今天在centos上安装了两个tomcat和nginx,进行配置。今天记录的只是最基本的实现测试。(不包含使用redis进行session共享)

Nginx 是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。  其特点是占有内存少,并发能力强。

直接开始主题:

1,首先jdk应该是配好了我就不写了,安装nginx(我使用的rmp安装) ,安装 pcre 让nginx支持rewrite,我使用的是pcre2-10.00.tar.gz;

PCRE下载地址:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/  

  tar zxvf  pcre-8.01.tar.gz  

  cd pcre-8.01  

  ./configure  

  make 

  make install 

ps:(网上说没有安装openssl还要安装openssl。)

2,安装nginx:

 rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm 

 yum install nginx   (我是一路的[y/n]  选择y,最后出现complete!)

nginx的几个默认目录:

 whereis nginx
 nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx

 其中

 配置所在目录:/etc/nginx/

 错误日志:/var/log/nginx/error.log

 默认站点目录:/usr/share/nginx/html

3.可能CentOS的防火墙把80端口拦住了,打开80端口

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

/etc/init.d/iptables status 查看

 

 

出现这个就哦了。

4,配置nginx:

配置文件在/etc/nginx/

#Nginx所用用户和组 
user  root;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue  
    use epoll;  
      
    #允许最大连接数 
    worker_connections  2048;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    upstream localhost {  
         #ip_hash   
          server localhost:8081;  
          server localhost:8080;  
         } 

    #这里还包含另外的cong文件
    include /etc/nginx/conf.d/*.conf;

    




}

主要是upstream 所以一会将要把两个tomcat端口设置成8080和8081.

另一个配置文件就是那个包含的/etc/nginx/conf.d/*.conf; (也就是default.conf):

#charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    #修改就是这一段代理
    location / {
        proxy_connect_timeout   3;  
        proxy_send_timeout      30;  
        proxy_read_timeout      30;  
        proxy_pass http://localhost;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #

5.启动nginx(我是先配置好两个tomcat,然后再测试的,继续往下)

 nginx

 测试nginx配置:nginx -t

6,开始配置tomcat

  tomcat都很熟悉,这次只需要修改server.xml配置文件即可。修改3处:

a: 

<!-- 修改port端口:18080 两个tomcat不能重复-->
<Server port="18080" shutdown="SHUTDOWN">

b:

<!-- port="8080" tomcat监听端口 -->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />

c:  Engine元素增加jvmRoute属性:(我是 tomcat1 和 tomcat2 )

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

 

另一个也是改这三处,但是端口要和nginx一致。我的是8080和8081

7.测试:启动两个tomcat和nginx。

验证配置负载均衡设置,http://localhost/   多次访问测试:

我为了方便看。这两个tomcat的首页我添加了一个小标识。说明访问了不同的tomcat。

 

 

8.后续还需要加上redis缓存实现session共享,根据自己情况进行测试。

 

 


posted @ 2015-01-30 20:07  付建超  阅读(3412)  评论(2编辑  收藏  举报