Tomcat virtual host configuration and tomcat Virtual Host Configuration
1. What is a VM?
Multiple web sites are built on one physical machine. Each web site runs independently and does not interfere with each other. These sites are "virtual hosts ".
How to implement a VM:
1. Host Name-based VM: resolve multiple domain names to the same IP address, add multiple sites to the WEB server, and set a host name for each site. The HTTP request contains the host name information. When the WEB server receives an access request, it can access different websites based on different host names.
Steps:
A. Configure the ing management between domain names and Ip addresses (for local LAN, we can add them to the host file; for large networks or Internet networks, we need to configure the ing between Ip addresses and domain names on the DNS server)
Add the following content at the end of the C: \ Windows \ System32 \ drivers \ etc \ hosts file:
127.0.0.1 www.jalja1.org127.0.0.1 www.jalja2.org
B. configure server. xml
<Host name = "www.jalja1.org" appBase = "E:/learning/activeMq/app1" unpackWARs = "true" autoDeploy = "true"> <Valve className = "org. apache. catalina. valves. accessLogValve "directory =" logs "prefix =" localhost_access_log. "suffix = ". txt "pattern =" % h % l % u % t & quot; % r & quot; % s % B "/> </Host> <Host name =" www.jalja2.org "appBase =" E: /learning/activeMq/app2 "unpackWARs =" true "autoDeploy =" true "> <Valve className =" org. apache. catalina. valves. accessLogValve "directory =" logs "prefix =" localhost_access_log. "suffix = ". txt "pattern =" % h % l % u % t & quot; % r & quot; % s % B "/> </Host>
C. Test
Http://www.jalja1.org: 8080/
Http://www.jalja2.org: 8080/
2. Port-based VM: an IP address that enables access to different websites through different ports.
Step: set two service components in server. xml
<Service name = "Catalina"> <Connector port = "8080" protocol = "HTTP/1.1" connectionTimeout = "20000" redirectPort = "8443"/> <Connector port = "8009 "protocol =" AJP/1.3 "redirectPort =" 8443 "/> <Engine name =" Catalina "defaultHost =" localhost "> <Realm className =" org. apache. catalina. realm. lockOutRealm "> <Realm className =" org. apache. catalina. realm. userDatabaseRealm "resourceName =" UserDatabase "/> </Realm> <Host name =" www.jalja1.org "appBase =" E: /learning/activeMq/app1 "unpackWARs =" true "autoDeploy =" true "> <Valve className =" org. apache. catalina. valves. accessLogValve "directory =" logs "prefix =" localhost_access_log. "suffix = ". txt "pattern =" % h % l % u % t & quot; % r & quot; % s % B "/> </Host> </Engine> </Service> <Service name =" Catalina2 "> <Connector port =" 8888 "protocol =" HTTP/1.1 "connectionTimeout =" 20000 "redirectPort =" 8443 "/> <Connector port =" 8010 "protocol =" AJP/1.3 "redirectPort =" 8443 "/> <Engine name =" Catalina2 "defaultHost =" localhost "> <Realm className =" org. apache. catalina. realm. lockOutRealm "> <Realm className =" org. apache. catalina. realm. userDatabaseRealm "resourceName =" UserDatabase "/> </Realm> <Host name =" www.jalja1.org "appBase =" E: /learning/activeMq/app2 "unpackWARs =" true "autoDeploy =" true "> <Valve className =" org. apache. catalina. valves. accessLogValve "directory =" logs "prefix =" localhost_access_log. "suffix = ". txt "pattern =" % h % l % u % t & quot; % r & quot; % s % B "/> </Host> </Engine> </Service>
Http://www.jalja1.org: 8888/
Http://www.jalja1.org: 8080/
3. IP address-based VM: the server uses multiple NICs to configure multiple IP addresses, then configures the WEB server, and binds multiple websites to different IP addresses. (This method wastes Ip resources. tomcat does not support this method)
Ii. tomcat directory structure:
Bin: tomcat starts and closes the script file.
Conf: tomcat configuration files. Its core configuration file:
1. server. xml configuration and server-related information
2. web. xml web application (a web application is equivalent to a site)
3, tomcat-users.xml configuration tomcat related information (tomcat user name and password permissions)
<?xml version='1.0' encoding='utf-8'?><tomcat-users> <role rolename="admin-gui"/> <user username="tomcat" password="tomcat" roles="admin-gui"/></tomcat-users>
Lib: tomcat starts dependent jar packages
Logs: tomcat logs
Temp: tomcat running is a temporary file generated
Webapps: the directory where the web application is located, that is, the directory where external web resources are stored.
Work: tomcat working directory, mainly used to store servlet files and class files generated after jsp is accessed.
Iii. Process of tomcat processing HTTP requests
1. Relationship between tomcat components (tomcat7)
<?xml version='1.0' encoding='utf-8'?><Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JasperListener" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service></Server>
-ServerThe server element is the JVM entry point. There is only one configuration file. Because the server is not a container, child components cannot be nested. server listens for the shutdown command on a specified port. server can contain one or more service instances.
-ServiceA service is composed of one or more ors that share the same iner. Generally, a Service is an Engine, but there is no clear requirement for this. because the Service is not a iner, sub-components (such as Loggers/Valves) cannot be nested in it ).
-Connector Connector is a connection between Tomcat and the client. Tomcat has two typical Connector: http and JK2.http connector listen to connections from the Browser (usually in the familiar port 8080 ), JK2. requests from other webservers (listening on port 8009 by default ). Connector will send the obtained request to the Engine for processing.
-Engine Multiple Virtual hosts can be configured under the Engine. Each Virtual Host has a domain name. When the Engine receives a request, it matches the request to a Host, then, the request is sent to the Host for processing.The Engine has a default virtual Host. When the request cannot match any Host, the default Host is used for processing.
-Host Host represents a virtual host. The default value is localhost. Multiple web applications can be deployed under the host. In our actual application, the problem object is generally host.
-Context context indicates a WEB application.
2. Request Processing Process
Processing process: the user sends a request to the web server. The request will be received by the ctor Connector being monitored, and the request will be sent to the Engine under the Service for processing, and wait for the Engine to process the result. After the Engine obtains the request, it matches the Host based on the requested Host information. The Host matches the corresponding Context based on the requested path, after the Context web application matches, construct the request and response request objects and call the specified Servlet to process the requests. After the request is processed, the response object is returned to the Host, the Host returns the response object to the Engine, and the Engine then returns the response object to the Connector linker, finally, the Connector returns response to the browser.