nginx+tomcat to configure load balancing

Keywords: Tomcat Nginx JSP xml

The first step is to download tomcat and configure the multi tomcat running environment;

① download tomcat, extract two copies to D:\tomcats directory, and rename them:

tomcat_8080

tomcat_8090

② replace the Catalina home in startup.bat and catalina.bat under the bin directory of tomcat_withcatalina home

③ replace the Catalina home in the startup.bat and catalina.bat under the bin directory of tomcat_withcatalina home

④ configure system environment variables

Variable name Catalina home variable value: D: \ Tomcats \ Tomcat

Variable name Catalina home variable value: D: \ Tomcats \ Tomcat 8090

⑤ modify three places of server.xml under the conf directory of tomcat_

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

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

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

⑥ modify three places of server.xml under the conf directory of tomcat_

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

<Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

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

Step 2 download nginx and configure the nginx running environment

① download the window version of nginx

② configure the nginx.conf file under the conf of nginx

#Running user
#user  nobody;
#Number of open processes < = number of CPUs
worker_processes  1;

#Error log save location
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#Waiting events
events {
    #Open under Linux to improve performance
    #use epoll;
    #Maximum connections per process (maximum connections = connections x processes)
    worker_connections  1024;
}

http {
    #File extension and file type mapping table
    include       mime.types;
    #Default file type
    default_type  application/octet-stream;

    #The output format of log file is similar to the global setting
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #Request log save location
    #access_log  logs/access.log  main;
    
    #Open send file
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #Open gzip compression
    #gzip  on;
	
	#Configure a load balancing group
	upstream mytomcats { 
		  server 192.168.0.119:8080;  
		  server 192.168.0.119:8090;
	}  	

    #First virtual host
    server {
        listen       80;
        server_name  192.168.0.119;
        charset utf-8;
		location / {  
			root test;
			index index.jsp;  #Set default page 
			proxy_pass  http://mytomcats;	
			deny 127.0.0.1;  #Rejected ip
			allow 192.168.0.119; #Permissible ip
		} 
        error_page   500 502 503 504  /50x.html;
    }
}

Third, create a Dynamic Web Project and deploy it to Tomcat and Tomcat. The index.jsp page is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Load balancing test-8080</title>
</head>
<body>
	<p>8080 service</p>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Load balancing test-8090</title>
</head>
<body>
	<p>8090 service</p>
</body>
</html>

Fourth, start Tomcat and tomcat, input 192.168.0.119/test/index.jsp in the browser, and refresh to see the output results:

For "8080 service" and "8090 service".

Summary: the simulation application load balancing is completed.

 

 

Posted by dcooper on Wed, 27 Nov 2019 09:54:04 -0800