Apache reverse proxy combined with Tomcat cluster for Load Balancing (4) and Apache reverse proxy
This article mainly introduces the tomcat cluster mode. Next we will talk about apache's reverse proxy. The reverse proxy can be understood in this way. We usually think that the proxy (Forward proxy) is an internal LAN machine that cannot access the Internet, the gateway can access the Internet, and everyone accesses the Internet through the gateway in the LAN, this gateway is a proxy machine. The reverse proxy means the opposite. Now, the gateway does not act as a proxy for the internal LAN, but requests an external LAN to act as a proxy. For example, currently, an application system is deployed on machine A in the LAN. The external network cannot directly access machine A, but accesses machine A through the gateway. Here, the gateway is A reverse proxy.
Conclusion: Forward proxy-provides Internet channels for LAN clients in the firewall, and reverse proxy-provides the servers behind the firewall to Internet users for access.
Apache Server Load balancer is implemented by using the reverse proxy of Apache, coupled with the load balancing policy. Next I will explain how apache performs load balancing.
Enable the reverse proxy function of apache
The mod_proxy (reverse proxy) module has been added after Apache2.2x, but is not enabled by default. Open httpd under the Apache directory on your machine. conf file, remove the comment before the mod_proxy module:
# Load mod_proxyLoadModule proxy_module modules/mod_proxy.soLoadModule proxy_ajp_module modules/mod_proxy_ajp.soLoadModule proxy_balancer_module les/mod_proxy_connect.soLoadModule proxy_module modules/mod_proxy_connect.soLoadModule proxy_ftp_module modules/mod_proxy_ftp.soLoadModule proxy_http_module modules/modules
In this way, the mod_proxy module of Apache is enabled.
Add an Apache listening port
Add the following configuration at the end of httpd. conf:
ServerAdmin [email]****.com[/email] ServerName localhost ServerAlias localhost ProxyPass / balancer://cluster/ stickysession=JSESSIONID nofailover=Off ProxyPassReverse / balancer://cluster/ ErrorLog "logs/error.log" CustomLog "logs/access.log" common
VirtualHost *: 80 represents the http server port of the local machine. Generally, projects are monitored on the default port 80 of the HTTP service.
ProxyPass/balancer: // cluster/indicates that all requests will be redirected to balancer: // cluster/for processing. Balancer is a built-in load. The cluster is the name of the load in the following configuration (the name must be random but consistent between the top and bottom)
ProxyPassReverse/balancer: // cluster/is a reverse proxy, that is, reverse proxy of all requests to the application url path after Server Load balancer.
Stickysession = JSESSIONID nofailover = Off is used for Session replication. (Sticky session) adds Server Load balancer Processing Configuration
Add the following configuration at the end of the httpd. conf file:
ProxyRequests Off
BalancerMember ajp: // 127.0.0.1: 18009 loadfactor = 1 route = tomcat7_node1BalancerMember ajp: // 127.0.0.1: 28009 loadfactor = 1 route = tomcat7_node2 # status = + H is configured for hot backup, when all machines are over, the request will be sent to the machine # BalancerMember http: // 192.168.1.218: 8009 status = + H # balanced by the number of requests (default) # ProxySet lbmethod = byrequests # by weight # ProxySet lbmethod = bytraffic # By load, that is, distributing new requests to less load # ProxySet lbmethod = bybusynessProxySet lbmethod = bybusyness
Among them, ProxyRequests Off is to disable the forward proxy, because the forward proxy allows the client to access any website and hide the client itself, so you must take security measures to ensure that only authorized clients provide services. The reverse proxy of Apache is used here, so the forward proxy is disabled first.
BalancerMember ajp://127.0.0.1:18009 loadfactor=1 route=tomcat7_node1
Which are the members of the server load (The cluster members who forward external requests) and 18009 are tomcat's ajp ports. Note that the two tomcat ports on the same machine are inconsistent.
Route is the route ID and the jvmRoute in tomcat above. Note that it must be consistent with the tomcat name in the text.
ProxySetlbmethod is the load balancing algorithm policy. Here, you need to be careful when the throughput is less Node Based on the load, so you can allocate more tasks to you. The byrequests strategy focuses more on the number of times.
The Apache Server Load balancer configuration is complete. Start the test below. Note that you need to start the web Project in tomat as the default Project (that is, enter http: // localhost: 8080.
BalancerMember ajp://127.0.0.1:18009 loadfactor=1 route=tomcat7_node1
You do not need to add a project name after the port number. If it is not a default project of tomcat, you need to change Apache configuration to ajp: // 127.0.0.1: 18009/WebProject (that is, add the project name ), in short, the project paths of tomcat and apache are saved in the same way. Otherwise, session information cannot be shared.
Test procedure
1. Start Apache and two tomcat servers (start them successively, only start them all)
2. Access the local http: // localhost (or http: // localhost/WebProject, based on your project path)
3. apache forwards requests to different servers.