Tomcat Security Configuration and performance optimization

Source: Internet
Author: User
Tomcat Security Configuration and performance optimization

Tomcat is a free, open source Web application server under the Apache Software Foundation, it can run on many platforms, such as Linux and Windows, because of its stable performance, good scalability, free and so on the characteristics of the majority of users. At present, many Internet applications and enterprise applications are deployed on TOMCAT servers, such as our company, ha.

We have previously used the default configuration of Tomcat, so there are security concerns. The Tomcat for all servers in the test environment was safely optimized last week, and some of the performance optimizations were made here, with a simple record sharing.


First, version security

Upgrade the current Tomcat version to the latest stable version. Hence the name incredible, the latest stable version should take into account the latest and the stability of these two concepts. A stable version, it takes time to precipitate, and the latest is relative to the stable version of the latest. Therefore, we generally choose the current version, the latest version to push several versions forward or forward a few months out of the version.

At present, the enterprise commonly used tomcat large version of version 6.0 and 7.0, 8.0 version Although has been out for a long time, but still do not recommend use.


In the upgraded version, there are two things to note:

1, try to avoid across the major version of the upgrade

2. Back up the current version of Tomcat's Server.xml, catalina.sh, Web.xml, and Tomcat-users.xml files, and then after deploying the new version of Tomcat, overwrite these profiles and then deactivate the old version, Start the new version to complete the upgrade operation.


Second, hide version information

To prevent hackers from attacking certain versions, we need to hide or disguise the version information of Tomcat.

The default Tomcat version information is as follows:

The display of this information is controlled by a jar package that is stored under the Lib directory under the Tomcat installation directory with the name Catalina.jar.

We can extract this jar package through the jar XF command to get two directories meta-inf and org, by modifying the ServerInfo field in the Org/apache/catalina/util/serverinfo.properties file to actually Now to change the version information of our Tomcat.

The file information is as follows:

[Root@localhost ~]# cat org/apache/catalina/util/serverinfo.properties |grep-v ' ^$|# ' Server.info=apache
Tomcat/ 7.0.53
server.number=7.0.53.0
Server.built=mar 2014 06:20:16
of course, there is another way to hide or disguise the version information of Tomcat, But essentially the same as above, the operation is as follows:
[root@localhost ~]# cd/usr/local/apache-tomcat-7.0.53/lib
[root@localhost lib]# mkdir-p org /apache/catalina/util
[root@localhost lib]# cd org/apache/catalina/util
[root@localhost util]# vim Serverinfo.properties
Server.info=nolinux # If you want to change to another version number, you can change the value of this place to something else.


After the modifications are completed, restart Tomcat to see the effect.

The effect is as follows:


Iii. optimization of Web.xml

The servlet and other configuration files that apply to the settings of the entire Web application must conform to the standard format of the servlet specification. It allows you to configure the relevant options for your Web application, and Tomcat reads this file at startup and completes some initialization of the system you are developing.

It can do the following things:

1, provide the relevant configuration based on servlet

2. Add listeners, monitor sessions, or load some of the resources you want to load when Tomcat starts. Like creating a database connection pool, etc.

3, set session expiration time, tomcat default is 30 minutes

4, change the application of the default page, commonly used for index.html/index.jsp, etc.

5, add filter, do some you want to filter operations, such as sensitive words filter

6, add some jstl (standard tag library) definition, convenient in JSP directly includ in, directly use these tags

7, struts,spring or hibernate some of the configuration, etc.

The following excerpt from the "Tomcat Authority Guide" of O ' REILLY:

The Web.xml file format is defined in the servlet specification, so all Java servlet Container that conform to the servlet specification will use it. When Tomcat deploys the application (during the activation process, or after the application is loaded), it reads the generic conf/web.xml and then reads the Web-inf/web.xml in the Web application. In fact, depending on their location, we know that the settings in the Conf/web.xml file are applied to all Web applications, and that the settings in the web-inf/web.xml of some Web applications apply only to the application itself.
If there is no web-inf/ Web.xml file, Tomcat will output a message that cannot be found, but will still deploy and use the Web application, and the servlet specification's author wants to implement a method that can quickly and easily set a new scope to use as a test, so this web.xml is not necessary, but it is usually best to have every online Web application The program has a web-inf/web.xml of its own, even if it is used only for recognition, but I think it's a good habit.

Because the servlet specification is primarily for web programmers, not for system administrators. Therefore, for the operational dimension, we may be more concerned about the site's default page, custom error page, prohibit column directory features.

Because, in the normal production environment, certainly will not directly by Tomcat to provide the service, the front end must put has Apache or nginx. Therefore, for the site's default home page and custom error pages, we do it in front of Apache or Nginx. In addition, the company may also be referred to the program Ape in the project Web-inf/web.xml to do the definition.

In the new Tomcat version, the column directory feature is disabled automatically by default.

Below, I list some of the common features that are represented in Web.xml:


Site default Home page:


Custom error page:


To define a session timeout period:


Prohibit column directory:


Iv. optimization of Tomcat-user.xml

The file contains a list file of user names, roles, and passwords. Responsible for providing login authentication management for manager project under WebApps.

In a production environment, we need to annotate all of the files.

The annotation effect is as follows:


V. Optimization of Server.xml

Tomcat's master profile, which contains a number of key elements, such as service, Connector, host, etc., which create software "objects", sorting, and nesting of elements set in the process pipeline so that we can perform filtering, grouping, and so on.

If you want to optimize the file, we need to understand the structure of the file first.

Server.xml structure diagram:

This file describes how to start Tomcat Server

<Server>
 <listener/>
 <GlobaNamingResources>
 </globanamingresources
 <s ervice>
 <connector/>
 <Engine>
 <logger/>
 <rea LM/>
  


For this file, the points we need to optimize are as follows:

1, maxthreads connection number limit

MaxThreads is the maximum number of connections that Tomcat can accept. General settings do not exceed 8000, if your site traffic is very large may use a method to run multiple Tomcat instances, that is, on a server to start multiple tomcat and then do load balancing process.

The point to note here is that Tomcat and PHP are different. PHP can configure the number of connections according to CPU and memory, tens of thousands of normal. Java also needs to be aware of the JVM's parameter configuration. If you don't pay attention, you will crash because the JVM parameter is too small.

2, multiple virtual host

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.