Nginx+Tomcat负载均衡群集

mac2022-06-30  74

1:实施准备

关闭防火墙

[root@localhost ~]# systemctl stop firewalld

 

2:查看JDK是否安装

[root@localhost ~]# java -version

openjdk version "1.8.0_102"

OpenJDK Runtime Environment (build 1.8.0_102-b14)

OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)

 

3:安装配置Tomcat

[root@localhost ~]# tar zxvf apache-tomcat-8.5.30.tar.gz

[root@localhost ~]# mv apache-tomcat-8.5.30 /usr/local/tomcat8

[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh

[root@localhost ~]# netstat -anpt | grep 8080

tcp6       0      0 :::8080                 :::*                    LISTEN      45388/java   

 

浏览器测试Tomcat网站:127.0.0.1:8080

7:建立javaweb站点

1)建立web目录

[root@localhost ~]# mkdir -p /web/webapp1

 

2)建立java测试页面

[root@localhost ~]# vi /web/webapp1/index.jsp

test web

 

3)修改server.xml文件

[root@localhost ~]# vi /usr/local/tomcat8/conf/server.xml

148--151

<Host name="localhost"  appBase="webapps"

            unpackWARs="true" autoDeploy="true">

        <Context docBase="/web/webapp1" path="" reloadable="false">

        </Context>

 

4)重启tomcat服务

[root@localhost ~]# /usr/local/tomcat8/bin/shutdown.sh

[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh

 

5)访问网站测试

http://127.0.0.1:8080

 

Nginx+Tomcat负载均衡群集

案例实施

1Tomcat server的部署

1)关闭防火墙

2)确认是否安装JDK

3)安装Tomcat

4)创建/web/webapp1目录,并修改Tomcat配置文件server.xml

5)在/web/webapp1目录下简历测试页面文件index.jsp,两个Tomcat服务器的web测试页面的内容可以设置的不一样,便于观察效果。

6)启动Tomcat,并测试访问两个Tomcat服务器

 

2Nginx服务器配置

1)安装Nginx

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc*

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

[root@localhost ~]# tar zxvf nginx-1.12.0.tar.gz

[root@localhost ~]# cd nginx-1.12.0/

[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

[root@localhost nginx-1.12.0]# make && make install

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

 

2)配置nginx

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

upstream tomcat_server {

server 172.16.16.172:8080 weight=1;

server 172.16.16.173:8080 weight=1;

}

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

            proxy_pass http://tomcat_server;

 }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}

 

 

3)测试nginx配置文件

[root@localhost ~]# nginx -t

 

4)启动nginx服务

[root@localhost ~]# nginx

 

5)查看nginx服务进程

[root@localhost ~]# ps aux | grep nginx

root       8716  0.0  0.0  20480   620 ?        Ss   08:50   0:00 nginx: master process nginx

nginx      8718  0.0  0.0  23008  1384 ?        S    08:50   0:00 nginx: worker process

root       8746  0.0  0.0 112664   972 pts/0    R+   08:51   0:00 grep --color=auto nginx

 

6)查看nginx端口号及PID进程号

[root@localhost ~]# netstat -anpt | grep nginx

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8716/nginx: master  

 

 

 

3:测试负载均衡效果

1)打开浏览器访问:

http://172.16.16.170

 

2)不断刷新页面,观察页面变化

 

3)使用脚本查看效果

 

转载于:https://www.cnblogs.com/kkan/p/11110337.html

相关资源:Linux—Nginx-tomcat负载均衡群集
最新回复(0)