Tomcat调优处理

琉璃1年前技术文章472

1、隐藏版本号

进入tomcatlib目录找到catalina.jar文件

unzip catalina.jar


之后会多出两个文件夹 进入org/apache/catalina/util编辑配置文件ServerInfo.properties修改为

server.info=Apache Tomcatserver.number=0.0.0.0server.built=Nov 7 2016 20:05:27 UTC


将修改后的信息压缩回jar包

cd  /tomcat/lib
jar uvf catalina.jar org/apache/catalina/util/ServerInfo.properties


2、禁用不安全的方法

tomcat限制不安全http方法,如putdelete等等,设置方法在conf/web.xml里添加限制如下格式:

<security-constraint> 
        <web-resource-collection> 
            <url-pattern>/*</url-pattern> 
            <http-method>PUT</http-method> 
            <http-method>DELETE</http-method> 
            <http-method>HEAD</http-method> 
            <http-method>OPTIONS</http-method>  
            <http-method>TRACE</http-method> 
        </web-resource-collection> 
        <auth-constraint> 
        </auth-constraint> 
 </security-constraint>

3、错误页面跳转

tomcat404、502、403等等错误页面的跳转设置为指定跳转页面,设置方法在conf/web.xml里添加跳转如下格式:

    <error-page> 
        <exception-type>java.lang.Exception</exception-type> 
        <location>/404.html</location> 
    </error-page> 
    <error-page> 
        <error-code>404</error-code> 
        <location>/404.html</location> 
    </error-page> 
    <error-page> 
        <error-code>400</error-code> 
        <location>/404.html</location> 
    </error-page> 
    <error-page> 
        <error-code>500</error-code> 
        <location>/404.html</location> 
    </error-page>


4、使tomcat支持软链接

修改conf/context.xml文件:

tomcat7配置方法:

<!-- The contents of this file will be loaded for each web application --><Context allowLinking="true">


tomcat8配置方法:

<Context>
    <Resources allowLinking="true" />
</Context>

5、tomcat增加http安全响应头

修改web.xml文件:

配置方法:

 <filter>
        <filter-name>httpHeaderSecurity</filter-name>
        <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
          <param-name>antiClickJackingEnabled</param-name>
          <param-value>true</param-value>
        </init-param>
        <init-param>
          <param-name>antiClickJackingOption</param-name>
          <param-value>SAMEORIGIN</param-value>
        </init-param>
        <init-param>
          <param-name>blockContentTypeSniffingEnabled</param-name>
          <param-value>false</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>httpHeaderSecurity</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>


6、禁用管理端,强制或使用nginx配置规则

  • 删除默认的{Tomcat安装目录}/conf/tomcat-users.xml文件(强制)

  • 删除{Tomcat安装目录}/webapps下默认的所有目录和文件(强制)

7、Server header重写

tomcat HTTP端口直接提供web服务时此配置生效,加入此配置,将会替换http响应Server header部分的默认配置,默认是Apache-Coyote/1.1

修改conf/server.xml

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
               server="webserver" />


8、访问日志规范

开启Tomcat默认访问日志中的RefererUser-Agent记录,一旦出现安全问题能够更好的根据日志进行问题排查;X-Forwarded-For用于nginx作为反向代理服务器时,获取客户端真实的IP

修改conf/server.xml

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
       prefix="localhost_access_log" suffix=".txt"
       pattern="%{X-Forwarded-For}i %l %u %t %r %s %b %{Referer}i %{User-Agent}i %D" resolveHosts="false" />


9、tomcat设置字符集UTF-8

修改conf/server.xml

<Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" URIEncoding="UTF-8" />


10、修复某些项目Java中文字体不显示(中文乱码问题)

这种情况有可能是项目代码以及项目编译时的编码问题,也有可能是项目使用了特殊的中文字体,如果有特殊的中文字体,需要将字体文件放到jdk目录下

例如:在jdk中新建目录

/jdk1.8.0_191/jre/lib/fonts/fallback


将系统中simsun.ttc字体文件拷贝到此目录,并重命名为simsun.ttf

11、tomcat遵循JVM的delegate机制

修改conf/context.xml

<Loader delegate="true"/>
</Context>
Loader`对象可出现在`Context`中以控制`Java`类的加载。属性:`delegate`、含义:`True`代表使用正式的`Java`代理模式(先询问父类的加载器);`false`代表先在`Web`应用程序中寻找。默认值:`FALSE

True,表示tomcat将遵循JVMdelegate机制,即一个WebAppClassLoader在加载类文件时,会先递交给SharedClassLoader加载,SharedClassLoader无法加载成功,会继续向自己的父类委托,一直到BootstarpClassLoader,如果都没有加载成功,则最后由WebAppClassLoader自己进行加载。

False,表示将不遵循这个delegate机制,即WebAppClassLoader在加载类文件时,会优先自己尝试加载,如果加载失败,才会沿着继承链,依次委托父类加载。

12、tomcat8静态资源缓存配置

tomcat8增加了静态资源缓存的配置,.cacheMaxSize:静态资源缓存最大值,以KB为单位,默认值为10240KB .cachingAllowed:是否允许静态资源缓存,默认为true解决缓存溢出的办法 对应两个参数,解决方法有两种: 1:考虑增加cache的最大大小 2:关闭缓存 修改conf/context.xml

<Resources cachingAllowed="true" cacheMaxSize="1048576" ></Resources>


相关文章

某网络环境下访问业务异常问题排查

某网络环境下访问业务异常问题排查

问题现象在办公网络环境下访问业务:http://xxx服务,无法正常跳转至登陆页面,如下:但是在另外一台机房服务器访问业务:http://xxx 是正常的,会自动跳转到登陆页面,如下:排查步骤1、查找...

CPU--上下文切换

CPU--上下文切换

一、概述1、Linux 是一个多任务操作系统,它支持远大于 CPU 数量的任务同时运行。当然,这些任务实际上并不是真的在同时运行,而是因为系统在很短的时间内,将 ...

开源大数据集群部署(六)Keytab文件生成

开源大数据集群部署(六)Keytab文件生成

1、 创建keytab文件除了使用明文密码登录之外,Kerberos还可以使用keytab密码文件登陆,现在为testcuser创建它的keytab文件ipa-getkeytab -s ipa.hdp...

Alluxio部署

安装前准备1.1. 添加环境变量vi /etc/profile export ALLUXIO_HOME=/opt/alluxioexport PATH=$PATH:$ALLUXIO_HOME/bin ...

MySQL 复制延迟是如何计算的?

MySQL 复制延迟是如何计算的?

前言日常运维中总会收到 MySQL 备库延迟告警,一般数据库监控只读实例延迟都是采集 Seconds_Behind_Master 值,我们都知道它在某些场景下不可靠,今天一起探索 MySQL 是如何计...

canal原理及使用

canal原理及使用

什么是canalcanal,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费。这里我们可以简单地把canal理解为一个用来同步增量数据的一个工具。工作原理MySQL主备复制原理M...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。