Nginx+Tomcat搭建集群环境

单机部署多个Tomcat实例(Linux)

由于考虑到在学习时,可能没有足够的机器资源去用多台机器部署多个Tomcat实例,所以本节将简单介绍一下如何在Linux系统环境下,单机部署多个Tomcat实例。多机就不介绍了,因为多机就是一台机器安装一个Tomcat就行了,不需要做额外的更改。

1.解压tomcat·到相应的目录下:

[root@localhost src]# tar -zxvf apache-tomcat-8.5.32.tar.gz  -C /usr/local/

2.安装好Tomcat后,将Tomcat目录拷贝多份出来。并更改一下目录名称:

[root@localhost local]# cp -r apache-tomcat-8.5.32 ./tomcat8-01
[root@localhost local]# cp -r apache-tomcat-8.5.32 ./tomcat8-02

3.配置环境变量:

[root@localhost local]# vim /etc/profile  # 在文件末尾增加如下内容
export CATALINA_BASE=/usr/local/tomcat8-01
export CATALINA_HOME=/usr/local/tomcat8-01
export TOMCAT_HOME=/usr/local/tomcat8-01

export CATALINA_2_BASE=/usr/local/tomcat8-02
export CATALINA_2_HOME=/usr/local/tomcat8-02
export TOMCAT_2_HOME=/usr/local/tomcat8-02
[root@localhost local]# source /etc/profile  # 使配置文件生效

4.第一个Tomcat不需要动,只需要修改第二个Tomcat的相关配置,首先编辑第二个Tomcat安装目录中bin目录下的catalina.sh文件:

[root@localhost local]# cd /usr/local/tomcat8-02/bin/
[root@localhost /usr/local/tomcat8-02/bin]# vim catalina.sh  # 找到如下那行注释,在该注释下,增加两行配置
# OS specific support.  $var _must_ be set to either true or false.
export CATALINA_BASE=$CATALINA_2_BASE
export CATALINA_HOME=$CATALINA_2_HOME

5.然后编辑第二个Tomcat安装目录中conf目录下的server.xml文件,在该文件中需要修改三个端口:

[root@localhost /usr/local/tomcat8-02/conf]# vim server.xml
# 第一个端口,Server port节点端口
"9005" shutdown="SHUTDOWN">

# 第二个端口,Connector port节点端口,也即是Tomcat访问端口
"9080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8" />

# 第三个端口,Connector port节点端口
"9009" protocol="AJP/1.3" redirectPort="8443" />        
[root@localhost /usr/local/tomcat8-02/conf]#     

Nginx+Tomcat搭建集群

1.安装依赖(有了就不用装了)

yum install -y gcc gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

2.到nginx官网上获取下载链接,然后到Linux上下载并解压编译nginx:

[root@localhost ~]# cd /usr/local/src/
[root@localhost /usr/local/src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@localhost /usr/local/src]# tar -zxvf nginx-1.14.0.tar.gz
[root@localhost /usr/local/src]# cd nginx-1.14.0
[root@localhost /usr/local/src/nginx-1.14.0]# ./configure --prefix=/usr/local/nginx

3.创建nginx的主配置文件,因为我们不使用nginx自带的配置文件:

[root@localhost /usr/local/nginx/conf]# mv nginx.conf nginx.conf.bak
[root@localhost /usr/local/nginx/conf]# vim nginx.conf  # 内容如下
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    add_header Access-Control-Allow-Origin *;
    include vhost/*.conf;
}
[root@localhost /usr/local/nginx/conf]# mkdir ./vhost  # 创建虚拟主机配置文件的存放目录
[root@localhost /usr/local/nginx/conf]# cd vhost/
[root@localhost /usr/local/nginx/conf/vhost]# vim 192.168.88.134  # 创建虚拟主机配置文件,内容如下:
upstream 192.168.88.134 {
        # 需要负载的server列表,可以直接使用ip
        server 192.168.88.134:8080 weight=1;
        server 192.168.88.134:9080 weight=2;
}

server{
  listen 80;
  autoindex on;
  server_name 192.168.88.134;
  access_log /usr/local/nginx/logs/access.log combined;
  index index.html index.htm index.jsp;

  location / {
        proxy_pass http://192.168.88.134;
        add_header Access-Control-Allow-Origin *;
  }
}

4.启动两个Tomcat实例:
5.修改第二个Tomcat实例index.jsp文件内容,以作为两个Tomcat实例的区别,方便一会验证负载均衡是否已成功生效:

[root@localhost ~]# vim /usr/local/tomcat8-02/webapps/ROOT/index.jsp 
<div id="congrats" class="curved container">
    

I'm Tomcat 2

div>

6.设置防火墙规则,开放80端口:

[root@study-01 ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@study-01 ~]# firewall-cmd --reload
success

7.使用浏览器进行访问,验证nginx的负载均衡是否已成功生效:
Nginx+Tomcat搭建集群环境_第1张图片
Nginx+Tomcat搭建集群环境_第2张图片

你可能感兴趣的:(集群,nginx,tomcat)