Nginx+tomcat配置集群负载均衡

12 篇文章 0 订阅

1.下载安装Windows版本的Nginx,官网:http://nginx.org/en/download.html下载稳定版本的1.8.0(偶数为稳定版本,奇数为开发版本)

下载到zip包,解压就可以了,如我解压到d:/server,文档结构如下图

主要的配置文件在conf目录下,稍后介绍

解压完成后,就可以启动Nginx了,通过cmd进入命令行cd到目录:D:\server\nginx-1.8.0下,输入nginx就可以启动了。如下图

nginx -s stop可以停止Nginx服务器。nginx -t 检查配置

2.nginx.conf 配置(以下代码是Nginx服务器conf目录下的 nginx.conf 配置文件)

#Nginx所用用户和组,window下不指定
#user  nobody;

#工作的子进程数量(通常等于CPU数量)
worker_processes  4;

#错误日志存放路径
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#指定pid存放文件
#pid        logs/nginx.pid;


events {
    #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。  
    #use epoll;  
      
    #允许最大连接数
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    #引入Gzip.conf配置文件,此文件需要与 nginx.conf 放置在同一个目录下
    include    Gzip.conf;    
    #引入Proxy.conf配置文件,此文件需要与 nginx.conf 放置在同一个目录下
    include    Proxy.conf;
    upstream localhost {  
      #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。  
      #同一机器在多网情况下,路由切换,ip可能不同  
      #ip_hash;   
     
      #不同tomcat对应的端口,必须保证每个tomcat端口的不同,避免引起端口冲突
      server localhost:18081;  
      server localhost:18080;  
     }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
			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
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


 3.Gzip.conf 配置 

gzip              on;  
gzip_min_length      1000;  
gzip_types         text/plain text/css application/x-javascript;

 

4.Proxy.conf 配置

proxy_redirect          off;  
proxy_set_header        Host $host;  
proxy_set_header        X-Real-IP $remote_addr;  
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;  
client_max_body_size    10m;  
client_body_buffer_size 128k;  
proxy_connect_timeout   300;  
proxy_send_timeout      300;  
proxy_read_timeout      300;  
proxy_buffer_size       4k;  
proxy_buffers           4 32k;  
proxy_busy_buffers_size 64k;  
proxy_temp_file_write_size 64k;

5.tomcat 配置

tomcat 的配置都是在server.xml 文件中,修改几处端口,避免端口的冲突就可以了。

本例中集群两台tomcat服务器,第一台tomcat的配置如下,第二台tomcat的配置对应的端口分别为:18006,18081,8010

修改第一处端口:

         <!--  修改port端口:18005 俩个tomcat不能重复,端口随意,别太小--> 

         <Server port="18005" shutdown="SHUTDOWN">

修改第二处端口:


<Connector port="18080" protocol="HTTP/1.1"   
               connectionTimeout="20000"   
               redirectPort="8443" />
修改第三处端口:

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


6.验证Nginx配置:nginx -t,如下图则表示配置成功:

启动两个tomcat,端口对应分别为18080,18081,然后通过http://localhost访问Nginx,nginx会自动将请求分发到tomcat中。


可以通过修改tomcat默认的index.jsp页面来标示访问的是哪一个tomcat

tomcat-1

tomcat-2













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值