Tomcat learning Summary

Source: Internet
Author: User
Tags http redirect haproxy

About java

Tomcat is a Servlet container developed by the Jakarta project under the Apache Software Foundation. It supports Servlet and JavaServerPageJSP according to the technical specifications provided by SunMicrosystems and provides Web Server

Some special functions of the server, such as Tomcat Management and Control Platform, security domain management, and Tomcat valve. Because Tomcat itself contains an HTTP server, it can also be considered as a separate Web server.


Used for mod_proxy status information output
<Location/balancer-manager>
SetHandlerbalancer-manager
OrderDeny, Allow
Allowfromall
</Location>

Installation:
Tomcat installation is simple. You only need to decompress the package and pay attention to the dependency of tomcat on the java development environment.
# Rpm-ivhjdk-7u9-linux-x64.rpm
Java will be installed under/usr/java/jdk1.7.0 _ 09, and two links default and latest will be created at the same time.
Configure Environment Variables
Cat>/etc/profile. d/jdk. sh <EOF
JAVA_HOME =/usr/java/latest
PATH = $ JAVA_HOME/bin: $ PATH
EOF
#Tarxfapache-tomcat-7.0.42.tar.gz-C/usr/local
# Cd/usr/local
# Ln-svapache-tomcat-7.0.42tomcat
Configure Environment Variables
Cat>/etc/profile. d/tomcat. sh <EOF
CATALINA_HOME =/usr/local/tomcat
PATH = $ CATALINA_HOME/bin: $ PATH
EOF
Tomcat script
#! /Bin/sh
# TomcatinitscriptforLinux.
#
# Chkconfig: 23459614
# Description: TheApacheTomcatservlet/JSPcontainer.
JAVA_OPTS = '-Xms64m-Xmx128m'
JAVA_HOME =/usr/java/default
CATALINA_HOME =/usr/local/tomcat
ExportJAVA_HOMECATALINA_HOME
Exec $ CATALINA_HOME/bin/catalina. sh "$ @"

Configuration:
The tomcat configuration file is in $ CATALINA_HOME/conf.
Server. xml is used to configure the tomcat instance.
A tomcat instance is called server. The server has ctor, which sets the tomcat listening port and the communication mode with the browser or proxy. The Engine is an instance of the Servlet processor, that is, the servlet Engine.

When the Engine is associated with connect, a component called service is required. The host component is a host or virtual host that tomcat uses to receive requests and process them accordingly. The context component is placed under the host component

Context corresponds to a web application deployed under the host.
Other components include
Realm component for authentication
Valve, used for filtering, can implement access control and logging

Engine-related Configuration:
Name = "Catalina" # identifies an engine
DefaultHost = "www.kiki.com" # default host
JvmRoute = "TomcatA" # differentiate different engines when the front-end apache uses tomcat as a proxy

Configurations related to the host component:
Name = "localhost" identifies a host, which must be unique
AuotDeploy = "true" automatic deployment. The default value is false.
AppBase = "webapps" defines the path of the application on this host. The relative path is relative to the tomcat installation path, that is, "/usr/local/tomcat". You can also use the absolute path.
UnpackWARs = "true" enables Tomcat to automatically decompress the war File

Configuration related to context:
1) docBase: The storage location of the corresponding Web application. You can also use the relative path. The starting path is the path defined by appBase in the Host to which the Context belongs. Remember, the path name of docBase cannot be the same as the path defined in appBase of the corresponding Host.

For example, if appBase is deploy, and docBase cannot be the name of the deploy-bbs class;
2) path: URI relative to the virtual) root path of the host. If it is null, it indicates the virtual) root path of the host.

After tomcat is installed, several applications are deployed. In the $ CATALINA_HOME/webapps directory, you can use a browser to access the host localhost (default Host) through port 8080, but access ServerStatus, ManagerApp, host

The Manager needs authentication, which can be configured in $ CATALINA_HOME/conf/tomcat-users.xml as follows
<Rolerolename = "manager-gui"/>
<Rolerolename = "admin-gui"/>
<Userusername = "gentoo" password = "gentoo" roles = "manager-gui, admin-gui"/>
You can use gentoo: gentoo for access. Note that role in tomcat is equivalent to group.

Deploy applications:
Edit $ CATALINA_HOME/conf/server. xml, add the following configuration to the Engine container, and add a host and context
<Hostname = "www.kiki.com" appBase = "/tomcat/app1"
UnpackWARs = "true" autoDeploy = "true">
<Contextpath = "" docBase = "/tomcat/app1"/>
<ValveclassName = "org. apache. catalina. valves. AccessLogValve" directory = "logs"
Prefix = "www.kiki.com _ access_log." suffix = ". txt"
Pattern = "% h % l % u % t & quot; % r & quot; % s % B"/>
</Host>
Create the/tomcat/app1 directory in
# Mkidr-p/tomcat/app1
# Cd/tomcat/app1
Place the web applications developed using java in/tomcat/app1,


Proxy for tomcat:
Tomcat Web server connector supports two Protocols: AJP and HTTP
Nginx and haproxy are used as proxies for tomcat. Only http/https protocols can be used for communication. tomcat is a fully functional HTTP server. The front-end configuration uses the common proxy_pass (nginx) and

Default_backend (haproxy.

Apache is used as a proxy for tomcat, and http and ajp protocols are supported at the same time. Currently, ajp13 is a commonly used binary-based protocol for data transmission between web servers and tomcat, which is more efficient than http, therefore, if the frontend uses

Apache acts as a proxy for tomcat. We recommend that you use a connector of the ajp type.
Apache can be integrated with Tomcat through the mod_jk and mod_proxy modules, but mod_proxy is provided directly only in the apache2.2.x series, but it can provide more functions and security. For apache1.3.x and 2.0.x

Mod_jk is more applicable.

1. To use mod_proxy to connect to the Tomcat instance, apache must have loaded mod_proxy, mod_proxy_http, mod_proxy_ajp, and proxy_balancer_module for Tomcat cluster implementation.), mod_slotmem_shm (

Depends on proxy_balancer_module) and other modules:

# Httpd-M | grepproxy
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)

2. Add the following content to the global configuration section of httpd. conf or the VM:
ProxyViaOn
ProxyRequestsOff
ProxyPreserveHostOff

<Proxy *>
Requireallgranted
</Proxy>
ProxyPass/http: // 10.0.9.1: 8080/
ProxyPassReverse/http: /// 10.0.9.1: 8080/
# ProxyPass/ajp: // 10.0.9.1: 8009/
# ProxyPassReverse/ajp: // 10.0.9.1: 8009/
<Location/>
Requireallgranted
</Location>

You can choose to use the ajp protocol or http
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. If you want to support virtual hosts in reverse proxy

You need to 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 :;

Full indicates that the version number of the current apache server is added to each Via: line. Block indicates that the Via: will be removed from each proxy request message.

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

Set to Off.

ProxyPass [path]! | Url [key = valuekey = 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 the backend server.

A URL path on the 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 httpd2.1. 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 used in ProxyPass

Key = value. 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 considers

The current load is scheduled.
◇ 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 stickysession 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:
<Proxybalancer: // hotcluster>
BalancerMemberhttp: // www1.magedu.com: 8080 loadfactor = 1
BalancerMemberhttp: // www2.magedu.com: 8080 loadfactor = 2
ProxySetlbmethod = bytraffic
</Proxy>

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.

Load Balancing Based on mod_proxy

Configure the following content in the global configuration of httpd. conf:

ProxyViaOn
ProxyRequestsOff
ProxyPreserveHostOff

<Proxybalancer: // lbcluster1>
BalancerMemberajp: // 10.0.9.1: 8009 loadfactor = 10 route = TomcatA
BalancerMemberajp: // 10.0.9.11: 8009 loadfactor = 10 route = TomcatBP

RoxySetlbmethod = bytraffic </proxy>

ProxyPass/balancer: // lbcluster1
ProxyPassReverse/balancer: // lbcluster1/

Note: The default load balancing algorithm is byrequests. The mod_lbmethod_byrequests module must be loaded to make httpd work properly.

Used for mod_proxy status information output
<Location/balancer-manager>
SetHandlerbalancer-manager
OrderDeny, Allow
Allowfromall
</Location>


Use mod_jk to allow apache to act as a proxy for tomcat
Mod_jk is a project of ASF. It is a connector used by apache to communicate with Tomcat Based on the AJP protocol. It is a module of apache, the client server of the AJP protocol is the AJP Connector of Tomcat ).
Install the mod_jk connector. if the system does not have apxs, run yuminstallhttpd-devel first.
#Tarxftomcat-connectors-1.2.37-src.tar.gz
# Cdtomcat-connectors-1.2.37-src/native
#./Configure -- with-apxs =/usr/local/apache/bin/apxs
# Make & makeinstall

Add an apache sub-configuration file. The content is as follows:
LoadModulejk_modulemodules/mod_jk.so
JkWorkersFile/etc/httpd/workers. properties
JkLogFilelogs/mod_jk.log
JkLogLeveldebug
JkMount/* TomcatA
Jkmount/status/stat1

JkWorkersFile is used to specify the configuration file that stores the worker-related work attribute definitions, JkLogFile is used to specify the log file of the mod_jk module, and JkLogLevel can be used to specify the log level info, error, debug ), in addition

You can use JkRequestLogFormat to customize the log information format. JkMount format: JkMount <URLtomatch> <Tomcatworkername>)
Create the workers. properties file under/etc/httpd. The content is as follows:
Worker. list = TomcatA, stat1
Worker. TomcatA. host = 10.0.9.1
Worker. TomcatA. port = 8009
Worker. TomcatA. type = ajp13
Worker. TomcatA. lbfactor = 1
Worker. stat1.type = status

The workers. properties file is generally composed of two types of commands: one is the list of worker names that mod_jk can connect to, and the other is the attribute configuration information of each worker. They follow the following syntax respectively.

Worker. list = <acommaseparatedlistofworkernames>
Worker. <workername>. <property >=< propertyvalue>

The worker. list command can be specified multiple times, while the workername is the value of the jvmRoute parameter of the engine component in Tomcat. For example:
Worker. TomcatA. host = 172.16.100.1

Worker has different types according to its working mechanism. This is an attribute woker. <workname>. type that needs to be defined for each worker. Common types are as follows:
◇ Ajp13: This type indicates that the current worker is a running Tomcat instance.
◇ Lb: lb is loadbalancing, which is used for woker in Server Load balancer scenarios. This worker is not really responsible for processing user requests, but scheduling user requests to worker of another type ajp13.
◇ Status: A special worker that displays the working status of each actual worker in a distributed environment. It does not process any requests, nor is it associated with any actually working worker instance. For specific examples, see the configuration below.

Other common attributes of worker:
◇ Host: host where the worker instance of Tomcat 7 is located;
◇ Port: the port of the AJP1.3 connector on the Tomcat7 instance;
◇ Connection_pool_minsize: the minimum number of connections to be stored in the connection pool. The default value is pool_size/2;
◇ Connection_pool_timeout: Connection timeout duration in the connection pool;
◇ Mount: the context path provided by the current worker. If there are multiple context paths, use an empty lattice. This attribute can be replaced by the JkMount command;
◇ Retries: Number of retries when an error occurs;
◇ Socket_timeout: mod_jk wait time for the worker to respond. The default value is 0, that is, unlimited waiting;
◇ Socket_keepalive: whether to enable the keepalive function. 1 indicates enabled, 0 indicates disabled;
◇ Lbfactor: worker weight. This attribute can be defined for worker in Server Load balancer application scenarios;

In addition, in Server Load balancer mode, the dedicated attributes include:
◇ Balance_workers: Used to list the names of workers in Server Load balancer mode. Note that the names of workers that appear here must not be in any worker. the list attribute list is defined and worker. the

The worker name must contain the Server Load balancer worker. For specific examples, see the definition in the following section.
◇ Method: it can be set to R, T, or B. The default value is R, which schedules requests based on the number of requests. T indicates scheduling based on the actual traffic size sent to worker; B Indicates scheduling based on the actual load.
◇ Sticky_session: after a request is scheduled to a worker, all subsequent requests originating from this address are directly scheduled to this worker to bind the user session to a worker. The default value is 1, which enables this function. If

Worker supports session replication. You can set this attribute value to 0.

Configure Load Balancing Based on mod_jk
Httpd sub-configuration file

LoadModulejk_modulemodules/mod_jk.so
JkWorkersFile/etc/httpd/workers. properties
JkLogFilelogs/mod_jk.log
JkLogLeveldebug
JkMount/* lbcluster1
Jkmount/status/stat1


Workers. properties File

Worker. list = lbcluster1, stat1
Worker. TomcatA. host = 10.0.9.1
Worker. TomcatA. port = 8009
Worker. TomcatA. type = ajp13
Worker. TomcatA. lbfactor = 1
Worker. TomcatB. host = 10.0.9.11
Worker. TomcatB. port = 8009
Worker. TomcatB. type = ajp13
Worker. TomcatB. lbfactor = 1

Worker. lbcluster1.type = lb
# Worker. lbcluster1.method = T
Worker. lbcluster1.sticky _ session = 0
Worker. lbcluster1.balance _ workers = TomcatB, TomcatA

Worker. stat1.type = status

Others:

Application directory structure:
WEB-INF/web. xml: contains the deploy descriptor of the current webapp, such as the details of all dynamic files such as servlets and JSP, Session Timeout time, and data sources. Therefore, it is also usually used to define the unique resources of the current webapp;
/WEB-INF/classes: contains all server-side classes and other third-party classes related to the current application;
/WEB-INF/lib: contains the JAR file used by JSP;

Xml file format:
For example
<Contextpath = "" docBase = "/tomcat/app1"/> close sign/> must contain spaces before
Like this
<Servicename = "Catalina">
">" No space is required.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.