Example one: Manually add an application:
Create a directory of WebApp
[Email protected] myweb]# mkdir WebApp
[Email protected] webapp]# mkdir logs
[Email protected] myweb]# CD webapp/
[[email protected] webapp]# Mkdir./{classes,lib,meta-inf,web-inf,root}
in the Create a custom index.jsp file under the root Directory
[email protected] webapp]# cat root/index.jsp
<%@ page language= "java"%>
<%@ page import= "java.util.*"%>
<title>jsptest page</title>
<body>
<%out.println ("Hello World"); %>
</body>
then configure the Tomcat configuration file
[Email protected] conf]# vim Server.xml
Add a Host Component
<contextpath= "WebApp" docbase= "Root" reloadable= "true"/>
<valveclassname= "Org.apache.catalina.valves.AccessLogValve" directory= "/myweb/logs"
prefix= "Myweb_access_log" suffix= ". txt"
pattern= "%h%l%u%t"%r" %s%b "/>
</Host>
and modify the default engine component for the custom Host
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/75/6D/wKioL1Y43kvjK7ZLAAHSQ67_Mxg457.jpg "title=" 11.png "alt=" Wkiol1y43kvjk7zlaahsq67_mxg457.jpg "/>
then restart Tomcat
[[email protected] bin]# catalina.sh start
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/75/6F/wKiom1Y43h2xsyyeAAPBKIdK4b8762.jpg "title=" 12.png "alt=" Wkiom1y43h2xsyyeaapbkidk4b8762.jpg "/>
Browse among them to view:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/75/6D/wKioL1Y43miRxTNqAACi6jsm1Ro530.jpg "title=" 13.png "alt=" Wkiol1y43mirxtnqaaci6jsm1ro530.jpg "/>
You can also customize The Value component implements access control. such as:
<valveclassname= "Org.apache.catalina.valves.RemoteAddrValve" deny= "172\.16\.100\.100"/>
Note:
AppBase: Specifies the root directory when Tomcat reads a program
DocBase: Specifies the root directory of an application, which can be specified as rootand put program files directly in this directory
Path : Specifies the URL address at which to access . and docBase are relative paths, similar to the paths defined in httpd . Path Given cannot end with "/";
example two: Using nginx to do reverse proxy back-end Tomcat server:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/75/6F/wKiom1Y43kPTezOOAACRMv2VAB0509.jpg "title=" 14.png "alt=" Wkiom1y43kptezooaacrmv2vab0509.jpg "/>
using the example above as a Tomcat service, configure the nginx Direction agent on another host .
modifying Nginx 's master configuration file
Add to
server {
Listen 80;
location~* \. (Jsp|do) $ {
Proxy_pass http://tomcat1.com:8080;
}
}
Can be tested in the browser:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/75/6F/wKiom1Y43lGDDRN2AAC6tRPRCNU626.jpg "title=" 15.png "alt=" Wkiom1y43lgddrn2aac6trprcnu626.jpg "/>
example three: Using httpd to do reverse proxy to implement back-end Tomcat
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/75/6D/wKioL1Y43qyAdZpPAAB3D0n5f3k123.jpg "title=" 16.png "alt=" Wkiol1y43qyadzppaab3d0n5f3k123.jpg "/>
you can use yum to install httpd, which uses centos7 installed httpd-2.4
The first step : First confirm whether the httpd supports the proxy module:
Proxy_module//httpd The most important module for using this module for proxy functions
Proxy_http_module//HTTPD Implementation of sub-modules based on HTTP protocol proxy function
Proxy_balancer_module//httpd module for load Balancing
Proxy_ajp_module//HTTPD implements a module based on the AJP protocol and back-end Tomcat links
The above modules must be enabled before you can use httpd to do reverse proxy or load balancer with back-end Tomcat links
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/75/6F/wKiom1Y43n6gHHDaAARvh-Raz28067.jpg "title=" 17.png "alt=" Wkiom1y43n6ghhdaaarvh-raz28067.jpg "/>
Note: In addition , httpd can also be implemented based on the JK module (using the JK module to implement the back-up agent can only be based on AJP protocol with back-end tomcat ), this module is not installed by default and requires the httpd from Apache download and compile using the website.
Step Two : Modify The httpd configuration file:
Modify The httpd configuration file to disable the hub host
#DocumentRoot "/var/www/html"
Define a virtual host
[Email protected] conf.d]# vim vhost.conf
<virtualhost *:80>
ServerName tomcat1.com
Proxyvia on// whether to enable the addition of headers in response messages to the client to indicate that they are represented by themselves.
Proxyrequests off// turn off the forward proxy function,module_proxy By default can do both forward proxy, but also can do reverse proxy, but can only use a proxy mode,off means to turn off the forward proxy function, Enable the reverse proxy feature.
Proxypreservehost on// sets whether the Host header in the client request message is sent to the back-end server.
<proxy *>
Require all granted// set all requests back to proxy
</proxy>
proxypass/http://172.16.249.204:8080/# Sets the address and port of the backend proxy service and those resources to proxy.
proxypassreverse/http://172.16.249.204:8080/
<location/>
Require all granted
</Location>
</VirtualHost>
start the httpd service
[Email protected] conf.d]# systemctl start Httpd.service
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/75/6D/wKioL1Y43sniqdKWAACoO3mT8QM648.jpg "title=" 18.png "alt=" Wkiol1y43sniqdkwaacoo3mt8qm648.jpg "/>
Test in the browser:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/75/6F/wKiom1Y43zzAGggCAACdfoAlqNQ477.jpg "title=" 19.png "alt=" Wkiom1y43zzagggcaacdfoalqnq477.jpg "/>
Note : It is difficult to use httpd for static and dynamic separation, but can be implemented using Nginx , generally in production environment using Lnamt: linux,nginx ,Apache,MySQL,Tomcat
httpd uses AJP protocol back-end Tomcat to do reverse proxy: (only need to modify the reverse proxy protocol used for AJP ,httpd service will automatically call MODULE_AJP Module for reverse-generation function)
<virtualhost *:80>
ServerName tomcat1.com
Proxyvia on
Proxyrequests OFF
Proxypreservehost on
<proxy *>
Require all granted
</proxy>
proxypass/ajp://172.16.249.204:8009/
proxypassreverse/ajp://172.16.249.204:8009/
<location/>
Require all granted
</Location>
</VirtualHost>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/75/6D/wKioL1Y434iS_K5DAACoBlWAC_Y016.jpg "title=" 20.png "alt=" Wkiol1y434is_k5daacoblwac_y016.jpg "/>
Note: for The status page of the httpd service , you should review the status page of the Proxy service , just add one of the following to the virtual host and add the status page Location can be achieved.
Proxypass/staus!
See Blog for Follow-up examples: httpd load Balancingtomcat and nginx load balancing based on three different ways Tomcat
This article is from the "Seven Stars" blog, please be sure to keep this source http://qikexing.blog.51cto.com/7948843/1709441
Nginx or httpd implements reverse proxy Tomcat and implements session hold (II)