Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Build Nginx and Tomcat load balancing clusters in Linux environment

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "building Nginx and Tomcat load balancing clusters in Linux environment". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Tomcat server is a free and open source web application server, which is a lightweight application server and the first choice for developing and debugging JSP programs. Because Tomcat's ability to handle static HTML is not as good as Apache or Nginx, Tomcat usually runs on the back end as a Servlet and JSP container.

The Nginx server can support responses of up to 50000 concurrent connections, has strong static resource processing capacity, runs stably, and consumes very low system resources such as memory and CPU.

In general, a Tomcat site may have a single point of failure and can not cope with too many customers' complex requests, so it can not be used alone in the production environment. At present, many large websites use Nginx server as the reverse proxy and load balancer of back-end website programs. To put it simply, Nginx is used as a load balancer to respond to client requests. As a load cluster of application servers, Tomcat handles client requests, that is, Nginx forwards access requests to servers in the backend server pool for processing. The topology diagram of the website is shown in the figure:

Case environment:

Host operating system IP address main software Nginx server RedHat7.3 x86_64192.168.113.151nginx-1.12.0.tar.gzTomcat server 1CentOS7.4 x86_64192.168.113.129jdk-8u91-linux-x64.tar.gz / apache-tomcat-8.5.16.tar.gzTomcat server 2CentOS7.4 x86_64192.168.113.150jdk-8u91-linux-x64.tar.gz / apache-tomcat-8.5.16.tar.gz

Tomcat1 server configuration

1. Turn off the firewall after starting the host

Systemctl disable firewalld.service / / disable firewall service systemctl stop firewalld.service / / disable firewall service

two。 Install JDK and configure the Java environment

(1)。 Extract the jdk software to the / opt directory, and move the extracted jdk folder to / usr/local/, renaming it to java.

Tar zxvf jdk-7u65-linux-x64.gz-C / opt mv jdk1.7.0_65/ / usr/local/java

(2)。 Create a java.sh script under / etc/profile.d/.

[root@centos7-1 ~] # vim / etc/profile.d/java.sh export JAVA_HOME=/usr/local/java / / set the Java root directory export PATH=$PATH:$JAVA_HOME/bin / / add the bin/ subdirectory under the Java root directory to the PATH environment variable

(3)。 Import the java.sh script into the environment variable to make it effective

Source / etc/profile.d/java.sh

(4)。 Run the java-version command to see if the java version is the same as previously installed.

[root@centos7-1 ~] # java-version openjdk version "1.8.0x131" OpenJDK Runtime Environment (build 1.8.0_131-b12) OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)

3. Install and configure Tomcat

(1)。 Extract the apache-tomcat-8.5.16.tar.gz package to the / opt directory, and move the folder generated by the extract to / usr/local/, renaming it to tomcat8.

Tar zxvf apache-tomcat-8.5.16.tar.gz-C / opt/ mv apache-tomcat-8.5.16/ / usr/local/tomcat8

(2)。 Start Tomcat

[root@centos7-1] # / usr/local/tomcat8/bin/startup.sh Using CATALINA_BASE: / usr/local/tomcat8 Using CATALINA_HOME: / usr/local/tomcat8 Using CATALINA_TMPDIR: / usr/local/tomcat8/temp Using JRE_HOME: / usr/local/java Using CLASSPATH: / usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar Tomcat started.

Tomcat runs on port 8080 by default. Run the netstat command to view the listening information on port 8080.

[root@centos7-1 ~] # netstat-ntap | grep 8080 tcp6 0 0:: 8080:: * LISTEN 1583/java

(3)。 Open the browser to access the test http://192.168.113.129:8080/,. If the interface below x appears, it means that the Tomcat has been configured and started successfully.

4. Set up a web site for Java

(1)。 Create a web directory under the root directory, and create a webapp1 directory in it to store the website files.

[root@centos7-1 ~] # mkdir-pv / web/webapp1 mkdir: created directory "/ web" mkdir: created directory "/ web/webapp1"

(2)。 Create a test page for index.jsp in the webapp1 directory.

[root@centos7-1 ~] # vim / web/webapp1/index.jsp / / comment JSP test1 page / / Test Page title / / Test Page body content

(3)。 Modify the server.xml file of Tomcat.

Define a virtual host and point the path to the website file to the established / web/webapp1, adding a context segment to the host section.

[root@centos7-1 ~] # vim / usr/local/tomcat8/conf/server.xml

Document base directory for docBase:web applications

Reloadable: set to monitor whether the "class" has changed

Path= ": set default" class

(4)。 Shut down Tomcat and restart.

/ usr/local/tomcat8/bin/shutdown.sh / / close Tomcat / usr/local/tomcat7/bin/startup.sh / / restart Tomcat

(5)。 Access http://192.168.113.129:8080/ through the browser, and the following interface appears, indicating that the Tomcat site is configured successfully and is able to run JSP.

Tomcat2 server configuration

The Tomcat2 server configuration method is basically the same as Tomcat1, which includes:

1. Turn off the firewall after starting the host.

two。 Install JDK, configure the Java environment, and the version is the same as Tomcat1 server.

3. Install and configure Tomcat, the version is the same as Tomcat1 server.

4. Create the / web/webapp1 directory, modify the Tomcat configuration file server.xml file, and change the website file directory to the / web/webapp1/ path.

5. Set up index.jsp under the / web/webapp1/ path, and change the contents of the test page index.jsp to distinguish as follows:

[root@localhost ~] # vim / web/webapp1/index.jsp / / comment JSP test2 page / / Test Page title / / Test Page body content

6. Launch tomcat, the browser accesses http://192.168.113.150:8080/, and the following interface appears, indicating that the Tomcat site is configured successfully and can run JSP.

Nginx server configuration

Install Nginx on Nginx server 192.168.113.151, reverse proxy two Tomcat sites, and achieve load balancing.

1. Turn off the firewall

Systemctl disable firewalld.service systemctl stop firewalld.service

two。 Install support softwar

Yum install gcc gcc-c++ make pcre pcre-devel zlib-devel openssl-devel-y

3. Extract and install Nginx

Useradd-s / bin/false www / / create and run user www Forcibly disable login to the system shell environment tar zxvf nginx-1.12.0.tar.gz-C / opt/ / extract the nginx software to / opt directory cd / opt/nginx-1.12.0/ switch to the extracted nginx folder directory. / configure\ / / specific options for configuring nginx-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

-- prefix=/usr/l,ocal/nginx: specify the nginx installation path

-- user=www,--group=www: specify the users and groups to run

-- with-file-aio: enable file modification support

-- with-http_stub_status_module: enable status statistics

-- with-http_gzip_static_module: enable gzip static compression

-- with-http_flv_module: enables the flv module to provide time-based offset files for seeking memory usage

-- with-http_ssl_module: enable the ssl module

Add Q group: 834962734, you can get a copy of Java architecture advanced technology learning resources. (high concurrency + Spring source code + JVM principle analysis + distributed architecture + micro-service architecture + multi-thread concurrency principle and so on. These become essential content for architects) and the Java advanced learning roadmap.

4. Configure nginx.conf

Vim / usr/local/nginx/conf/nginx.conf

(1)。 Add the following code to http {...} to set the list of servers for load balancer. The weight parameter represents the weight. The higher the weight, the greater the probability of being assigned. In order to make the test effect obvious, set the weight to the same.

Upstream tomcat_server {server 192.168.113.129 weight=1; server 192.168.113.150 weight=1;} / / configure the backend service pool tomcat_server to provide response data

(2)。 Add the line "proxy_pass http://tomcat_server;"" in http {...}-server {...}-location / {...}.

Location / {root html; index index.html index.htm; proxy_pass http://tomcat_server; / / configure server processing that forwards access requests to the backend server pool}

Using the above method, the default site of Nginx is proxied to the set tomcat_server load balancing server group through the proxy_pass method.

5. Test that the nginx configuration file is correct.

[root@RedHat7-1] # / usr/local/nginx/sbin/nginx-t nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful

6. Start the Nginx service and view its port

[root@RedHat7-1 ~] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ create a soft link to the nginx main program. It is convenient to call the nginx command [root@RedHat7-1 ~] # nginx/ / start the nginx service [root@RedHat7-1 ~] # netstat-ntap | grep nginx tcp 000.0.0.0usr/local/nginx/sbin/nginx 80 0.0.0.0usr/local/nginx/sbin/nginx * LISTEN 17819/nginx: master [root@RedHat7-1 ~] # killall-1 nginx/ / restart the nginx service

Test the effect of load balancing

1. Open a browser to access http://192.168.113.151/.

two。 Constantly refresh the browser test, you can see that because the weight is the same, the page will repeatedly switch between the following two pages.

On the first visit, the test page of test1 appears. After refreshing, the test page of test2 appears on the second visit, which indicates that the load balancer set has been successfully built and can be switched between the two tomcat server sites.

In the real environment, the content of the server cluster site is the same, here is to test the effect of the load balancer set and set up two different test pages.

This is the end of the content of "Building Nginx and Tomcat load balance clusters in Linux Environment". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report