Nginx + Tomcat load balancing cluster instance, can now do! ! !

Typically the next stage Tomcat site due to possible single point of failure and can not cope with too many complex and diverse customer requests and other issues, can not be used alone in a production environment. So we need a more reliable solution to improve Web site architecture.

Nginx is a very good HTTP server software, which supports the response up to 50,000 concurrent connections, has a strong ability to deal with static resources, stable, and memory, CPU and other system resource consumption is very low. Currently, many large sites use Nginx server as a backend web application of a reverse proxy and load balancers to improve concurrency load the entire site.

Case environment

Nginx + Tomcat load balancing cluster instance, can now do!  !  !

Case implementation

(1) Installation Services to deploy Tomcat 1

[root@localhost ~]#  tar zxf apache-tomcat-8.5.16.tar.gz -C /usr/src
[root@localhost ~]# mv /usr/src/apache-tomcat-8.5.16/ /usr/local/tomcat8
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml 
                          ……………………          //省略部分内容
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
//host定义一个虚拟主机,域名为localhost,appBase定义应用程序基准目录,unpackWARs定义是否自动解压,autoDeploy定义是否自动部署
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />
        <Context docBase="/web/webapp" path="" reloadable="false">
        </Context>
//context定义一个web应用(虚拟目录),path指定访问目录,docBase定义网页目录,当应用程序发生变化时候,自动装载,不用重启tomcat(手动添加 <Context  ……></Context>内容)

      </Host>
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh
//启动Tomcat服务
[root@localhost ~]# mkdir -p /web/webapp
[root@localhost ~]# vim /web/webapp/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
        <head>
                <title>JSP test1 page</title>
</head>
<body>
<% out.println("welcome to test");%>
</body>
</html>
[root@localhost ~]# netstat -anpt | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      18026/java   
[root@localhost ~]# firewall-cmd --add-port=8080/tcp
success
//开启相应的端口,不建议关闭防火墙

Client Access test:
Nginx + Tomcat load balancing cluster instance, can now do!  !  !

For more information and to build Tomcat server package on the network disk Download reference Bowen: the deployment of Tomcat (Web) services Comments

(2) Installation Services to deploy Tomcat 2

The first method to build structures with different Tomcat server (the actual production environment, the home must be consistent, however, in order to see the experiment results, we recommend not the same as the contents of the home page)

Client access effect:
Nginx + Tomcat load balancing cluster instance, can now do!  !  !

(3) Installation Services to deploy Nginx

[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel
//pcre为nginx提供rewrite所需的正则表达式语法,zlib:提供压缩,openssl-devel:提供ssl加密
[root@localhost ~]# groupadd www
[root@localhost ~]# useradd -g www www -s /bin/false
//创建所需的程序用户及组(/bin/false)这个shell非常严格,无论通过何种方式登录系统都不允许
[root@localhost ~]# tar zxf nginx-1.12.0.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/nginx-1.12.0/
[root@localhost nginx-1.12.0]#  ./configure --prefix=/usr/local/nginx \
 --user=www --group=www  --with-file-aio \
 --with-http_stub_status_module  \
 --with-http_gzip_static_module --with-http_flv_module  \
 --with-http_ssl_module 
 [root@localhost nginx-1.12.0]# make && make install

Nginx service details and related packages, please refer Bowen: the relevant configuration in detail based on Nginx server and build Web Hosting

The meaning of each configuration item:
// --prefix =: Specify the installation directory;
// - the User =, - = Group: specified users and groups to run;
// - File-with-aio: Enable file modification support;
// - with-http_stub_status_module: enabled statistics;
// --with-http_gzip_static_module: enable gzip static compression;
// - with-http_flv_module: flv enabled module, seek to provide memory-based file offset time;
/ / --with-http_ssl_module: enable SSL module;

[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
//该Nginx服务配置文件
http {
                                         ………………         //省略部分内容

        upstream  tomcat_server {
        server 192.168.1.2:8080 weight=1;
        server 192.168.1.3:8080 weight=1;
        }
                 //设定负载均衡的服务器列表,weight参数表示权限,权值越高被分到的概率越大
                 //Nginx使用upstream模块实现代理
    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_pass http://tomcat_server;
                         //把nginx的默认站点通过proxy_pass方法代理到设定好的tomcat_server负载均衡服务器组 
        }
                                           ………………………………    //省略部分内容                                                                 
}                                                                       
[root@localhost nginx-1.12.0]# /usr/local/nginx/sbin/nginx -t
//检查Nginx服务配置文件是否有语法错误
[root@localhost nginx-1.12.0]# /usr/local/nginx/sbin/nginx 
//启动服务,由于没有优化路径,所以应打绝对路径
[root@localhost nginx-1.12.0]# netstat -anpt | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      46627/nginx: master 
[root@localhost nginx-1.12.0]# firewall-cmd --add-service=http
success

Test load balance:
Nginx + Tomcat load balancing cluster instance, can now do!  !  !
Nginx + Tomcat load balancing cluster instance, can now do!  !  !
load balance has been achieved! ! !

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14157628/2435687