Use a single nginx to directly schedule tomcat as follows:
Http {
Upstream tomcat_server {
Server 10.0.0.20: 8080;
Server 10.0.0.30: 8080;
# Server 10.0.0.100: 8080 backup;
}
Server {
Location ~ * \. (Jsp | do) $ {
Proxy_pass http: // tomcat_server;
}
}
}
Generally, if tomcat does not directly interact with the front-end user, nginx_proxy performs the operation, as shown in the following figure.
LAMT:
Configure apache to connect to Tomcat through The mod_proxy module
To connect to the Tomcat instance using mod_proxy, apache has loaded mod_proxy, mod_proxy_http, mod_proxy_ajp, proxy_balancer_module (used for Tomcat cluster implementation) and other modules:
#/Usr/local/apache/bin/httpd-D DUMP_MODULES | grep proxy
Proxy_module (shared)
Proxy_connect_module (shared)
Proxy_ftp_module (shared)
Proxy_http_module (shared)
Proxy_fcgi_module (shared)
Proxy_scgi_module (shared)
Proxy_ajp_module (shared)
Proxy_balancer_module (shared)
Proxy_express_module (shared)
Httpd installed by rpm
[Root @ nginx-proxy2 ~] # Rpm-qa httpd
Httpd-2.2.15-39.el6.centos.x86_64
Modules to be started:
[Root @ nginx-proxy2 ~] # Ls/usr/lib64/httpd/modules/
Proxy_module
Proxy_http_module
Proxy_ajp_module
Proxy_balancer_module
[Root @ nginx-proxy2 ~] # Httpd-M
Proxy_http_module (shared)
Proxy_ajp_module (shared)
Proxy_module (shared)
Proxy_balancer_module (shared)
*************** ********
Tar xf httpd-2.4.2.tar.gz
Cd httpd-2.4.2
./Configure \
-- Prefix =/usr/local/apache,
-- Sysconfdir =/etc/httpd \
-- Enable-so -- enable-ssl \
-- Enable-cgi \
-- Enable-rewrite \
-- With-zlib \
-- With-pcre \
-- With-apr =/usr/local/apr \
-- With-apr-util =/usr/local/apr-util \
-- Enable-proxy-http \
-- Enable-proxy-ajp
Make & make install
Compile and install: manual start is required. By default, compilation is not started.
2. Add the following content to the global configuration section of httpd. conf or the VM:
ProxyVia Off disable forward proxy
ProxyRequests Off
ProxyPreserveHost Off
<Proxy *>
Require all granted
</Proxy>
ProxyPass/ajp: // 172.16.100.1: 8009/
ProxyPassReverse/ajp: // 172.16.100.1: 8009/
<Location/>
Require all granted
</Location>
Instructions on the preceding apache commands:
ProxyPreserveHost {On | Off}: If this function is enabled, the proxy will send the Host: line in the user request message to the backend server, instead of using the server address specified by ProxyPass. To support virtual hosts in reverse proxy, you must enable this option. Otherwise, you do not need to enable this feature.
ProxyVia {On | Off | Full | Block}: used to control whether to use Via: in the http header. It is mainly used to control the flow of proxy requests in multi-level proxies. The default value is Off, indicating that this function is not enabled. On indicates that all request and response packets are added Via:; Full indicates that the version number of the current apache server is added for each Via: line; block indicates that the Via: in each proxy request message will be removed.
ProxyRequests {On | Off}: whether to enable apache forward proxy. When this option is enabled, the mod_proxy_http module must be enabled for the proxy http protocol. If ProxyPass is set for apache, you must set ProxyRequests to Off.
ProxyPass [path]! | Url [key = value...]: associate a URL of the backend server with a virtual path of the current server as the path to provide services. The path is a virtual path on the current server, and the url is a URL path on the backend server. When using this command, the value of ProxyRequests must be set to Off. Note that if the path ends with "/", the corresponding url must end with "/", and vice versa.
In addition, the mod_proxy module supports the connection pool function with the backend server after the httpd 2.1 version. The connection is created on demand and can be saved to the connection pool for further use. The connection pool size or other settings can be defined by using key = value in ProxyPass. Common keys are as follows:
◇ Min: minimum capacity of the connection pool. This value is irrelevant to the actual number of connections. It only indicates the minimum size of space to be initialized in the connection pool.
◇ Max: The maximum capacity of the connection pool. Each MPM has its own independent capacity. The value is related to the MPM itself. For example, the Prefork value is always 1, others depend on the value of the ThreadsPerChild command.
◇ Loadfactor: defines the weight of the corresponding backend server in the server load balancer cluster configuration. The value range is 1-100.
◇ Retry: How long will it take for apache to send requests to the backend server for an error response and try again. The unit is seconds.
If the Proxy is specified to start with balancer: //, that is, when used in the server load balancer cluster, it can also accept some special parameters, as shown below:
◇ Lbmethod: apache implements load balancing scheduling. The default value is byrequests, that is, the number of requests is scheduled based on the weight, and bytraffic performs weight-based traffic flow scheduling, bybusyness is scheduled by considering the current load of each backend server.
◇ Maxattempts: Number of failovers before a request is abandoned. The default value is 1, and the maximum value is not greater than the total number of nodes.
◇ Nofailover: The value is On or Off. If it is set to On, the user's session will be damaged when the backend server fails. Therefore, when the backend server does not support session replication, you can set it to On.
◇ Stickysession: name of the sticky session of the scheduler. The value is JSESSIONID or PHPSESSIONID depending on the web programming language.
In addition to setting the command in banlancer: // or ProxyPass, you can also use the ProxySet command to directly set the command, for example:
<Proxy balancer: // hotcluster>
BalancerMember http://www1.linuxea.com: 8080 loadfactor = 1
BalancerMember http://www2.linuxea.com: 8080 loadfactor = 2
ProxySet lbmethod = bytraffic
ProxyPassReverse: used to allow apache to adjust the Location, Content-Location, and URL corresponding to the URI tag in the HTTP redirect response packet. This command must be used in the reverse proxy environment to prevent Redirect packets from bypassing the proxy server.
Example:
[Root @ nginx-proxy2 conf. d] # pwd
/Etc/httpd/conf. d
[Root @ nginx-proxy2 conf. d] # cat mod_proxy.conf
ProxyVia on
ProxyRequests off
ProxyPreserveHost on
ProxyPass/http: // 10.0.0.53: 8080/
ProxyPassReverse/http: // 10.0.0.53: 8080/
<Location/>
Order Allow, Deny
Allow from all
</Location>
[Root @ nginx-proxy2 conf. d] #
Mode_jk.png
The biggest advantage of apache is that it can forward data back based on ajp without using the http protocol.
[Root @ nginx-proxy2 conf. d] # pwd
/Etc/httpd/conf. d
[Root @ nginx-proxy2 conf. d] # cat mod_proxy.conf
ProxyVia on
ProxyRequests off
ProxyPreserveHost on
ProxyPass/ajp: // 10.0.0.53: 8009/
ProxyPassReverse/ajp: // 10.0.0.53: 8009/
<Location/>
Order Allow, Deny
Allow from all
</Location>
[Root @ nginx-proxy2 conf. d] #
Ajp.png
Admin-gui definition
[Root @ nginx-proxy2 conf. d] # vim tomcat-user.xml.
<Role rolename = "manager-gui"/>
<Role rolename = "admin-gui"/>
<User username = "tomcat" password = "tomcat" roles = "tomcat, manager-gui, admin-gui"/>
<! --
<User username = "both" password = "tomcat" roles = "tomcat, role1"/>
<User username = "role1" password = "tomcat" roles = "role1"/>
-->
</Tomcat-users>