In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to configure Nginx+Tomcat clusters and load balancers". In daily operation, I believe many people have doubts about how to configure Nginx+Tomcat clusters and load balancers. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to configure Nginx+Tomcat clusters and load balancers". Next, please follow the editor to study!
Tomcat cluster is an effective means to improve the overall system performance through horizontal expansion when a single server reaches the performance bottleneck. Nginx is a high-performance HTTP and reverse proxy web server, which can be easily configured to achieve load balancing of Tomcat clusters.
1. Overview
For Web applications, the problem of cluster * * is the sharing of Session information. There are generally the following solutions:
Use sticky sessions, such as IP Hash's load balancing strategy, to centralize current user requests on a single server; the disadvantage is a single point of failure and session loss
Use Session replication and Session replication strategy that comes with Tomcat to synchronize session information to each node of the cluster. The disadvantage is that it consumes more memory and bandwidth, which is suitable for small clusters.
Using third-party caching middleware to cache entire cluster session information, such as Redis cache, can be controlled by the application to associate with Session, or can be adapted to Tomcat
Of course, you can also save session information to a shared file system or database
During the configuration of Nginx, you may encounter the following problems:
You cannot use an underscore when configuring a upstream name, such as tomcat_ha, otherwise Tomcat will throw an exception for The character [_] is never valid in a domain name
Kill all nginx.exe processes on windows, taskkill / fi "imagename eq nginx.exe" / f
There is a system process with pid 4 on windows that takes up port 80, so change the nginx to 8000.
In the process of configuring a Tomcat cluster, you need to pay attention to:
Make sure web.xml is configured
element
Make sure that the Manager of Context is replaced with the standard session manager
Receiver.address should not be configured as auto, because 127.0.0.1 candidate Receiver.port may or may not be bound by default. Tomcat will automatically detect the available ports in the range of 4000-4100 and automatically handle conflicts.
If you need to turn off the firewall or open ports on different servers, there is also time synchronization
2. Nginx core configuration
Nginx uses the default configuration, and the core configurations added and modified are as follows:
Http {... # gzip on; # set the server list and weight of load balancer upstream tomcat-ha {# ip_hash; server 172.31.1.41upstream tomcat-ha 8080 weight=1; server 172.31.1.42upstream tomcat-ha 8080 weight=1;} server {listen 8000; server_name localhost; # charset koi8-r; # access_log logs/host.access.log main Location / {root html; index index.html index.htm; # forward request proxy_pass http://tomcat-ha$request_uri; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for Proxy_set_header X-Forwarded-Proto $scheme;}...}}
3. Tomcat cluster configuration
Enable the cluster configuration and add the following configuration to the element:
Briefly describe how it works:
Nginx forwards the request to Tomcat1, requests login authentication, creates a session, generates a Cookie, and copies the Session information to Tomcat2 before the response is returned
When requesting again, nginx forwards the request with session Cookie to the Session object that Tomcat2,Tomcat2 discovers that there is an associated authenticated successful Session in the internal Session pool, and returns the requested resource without authentication.
4. Verify load balancing and Session replication
4.1 Test environment
Use two PC to deploy Tomcat, corresponding to: 172.31.1.41-Tomcat1172.31.1.42-Tomcat2
Deployment is based on the use of the SessionExample program that comes with Tomcat, and a web application of tomcat-benchmark is written.
View the internal Session pool of the deployed application combined with the Manager application that comes with Tomcat
4.2 load balancing
Change the context-param in the tomcat-benchmark deployment descriptor file to "Ichimm Tomcat 1P2" to distinguish between the two Tomcat, launch Nginx and Tomcat, and visit 172.31.1.42 Tomcat 8000 in the browser to see the request switching between the two servers:
4.3 Session replication
For ease of understanding, set the load balancing policy of Nginx to ip_hash:
Suppose Nginx always locates the request to Tomcat1, and then creates a session on Tomcat1, adding some attributes to the session
Turn off Tomcat1 simulation failure, and Nginx will forward the request to Tomcat2 with the previous session Cookie.
Check whether the Session information associated with Cookie (JSESSIONID) exists on Tomcat2. If so, replication is successful.
The whole process is as follows:
Contrary to the description above, you can see that the Session information is copied from Tomcat2 to Tomcat1.
At this point, the study on "how to configure Nginx+Tomcat clusters and load balancing" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.