Tomcat調(diào)優(yōu)整理
目錄
1、隱藏版本號(hào)
2、禁用不安全的方法
3、錯(cuò)誤頁(yè)面跳轉(zhuǎn)
4、使tomcat支持軟鏈接
5、tomcat增加http安全響應(yīng)頭
6、禁用管理端,強(qiáng)制或使用nginx配置規(guī)則
7、Server header重寫
8、訪問(wèn)日志規(guī)范
9、tomcat設(shè)置字符集UTF-8
10、修復(fù)某些項(xiàng)目Java中文字體不顯示(中文亂碼問(wèn)題)
11、tomcat遵循JVM的delegate機(jī)制
12、tomcat8靜態(tài)資源緩存配置
13、未完待續(xù)

本文只記錄工作中遇到的Tomcat配置、性能、安全等方面的調(diào)優(yōu)處理,所以不會(huì)有長(zhǎng)篇大論,畢竟如今已經(jīng)是
java -jar的時(shí)代了~ ~ ~
1、隱藏版本號(hào)
進(jìn)入tomcat的lib目錄找到catalina.jar文件
unzip catalina.jar
之后會(huì)多出兩個(gè)文件夾
進(jìn)入org/apache/catalina/util編輯配置文件ServerInfo.properties修改為
server.info=Apache Tomcat
server.number=0.0.0.0
server.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方法,如put、delete等等,設(shè)置方法在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、錯(cuò)誤頁(yè)面跳轉(zhuǎn)
tomcat的404、502、403等等錯(cuò)誤頁(yè)面的跳轉(zhuǎn)設(shè)置為指定跳轉(zhuǎn)頁(yè)面,設(shè)置方法在conf/web.xml里添加跳轉(zhuǎn)如下格式:
<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安全響應(yīng)頭
修改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、禁用管理端,強(qiáng)制或使用nginx配置規(guī)則
刪除默認(rèn)的{Tomcat安裝目錄}/conf/tomcat-users.xml文件(強(qiáng)制) 刪除{Tomcat安裝目錄}/webapps下默認(rèn)的所有目錄和文件(強(qiáng)制)
7、Server header重寫
當(dāng)tomcat HTTP端口直接提供web服務(wù)時(shí)此配置生效,加入此配置,將會(huì)替換http響應(yīng)Server header部分的默認(rèn)配置,默認(rèn)是Apache-Coyote/1.1
修改conf/server.xml:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
server="webserver" />
8、訪問(wèn)日志規(guī)范
開啟Tomcat默認(rèn)訪問(wèn)日志中的Referer和User-Agent記錄,一旦出現(xiàn)安全問(wèn)題能夠更好的根據(jù)日志進(jìn)行問(wèn)題排查;X-Forwarded-For用于nginx作為反向代理服務(wù)器時(shí),獲取客戶端真實(shí)的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設(shè)置字符集UTF-8
修改conf/server.xml:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
10、修復(fù)某些項(xiàng)目Java中文字體不顯示(中文亂碼問(wèn)題)
這種情況有可能是項(xiàng)目代碼以及項(xiàng)目編譯時(shí)的編碼問(wèn)題,也有可能是項(xiàng)目使用了特殊的中文字體,如果有特殊的中文字體,需要將字體文件放到jdk目錄下
例如:在jdk中新建目錄
/jdk1.8.0_191/jre/lib/fonts/fallback
將系統(tǒng)中simsun.ttc字體文件拷貝到此目錄,并重命名為simsun.ttf
11、tomcat遵循JVM的delegate機(jī)制
修改conf/context.xml
<Loader delegate="true"/>
</Context>
Loader對(duì)象可出現(xiàn)在Context中以控制Java類的加載。屬性:delegate、含義:True代表使用正式的Java代理模式(先詢問(wèn)父類的加載器);false代表先在Web應(yīng)用程序中尋找。默認(rèn)值:FALSE
True,表示tomcat將遵循JVM的delegate機(jī)制,即一個(gè)WebAppClassLoader在加載類文件時(shí),會(huì)先遞交給SharedClassLoader加載,SharedClassLoader無(wú)法加載成功,會(huì)繼續(xù)向自己的父類委托,一直到BootstarpClassLoader,如果都沒(méi)有加載成功,則最后由WebAppClassLoader自己進(jìn)行加載。
False,表示將不遵循這個(gè)delegate機(jī)制,即WebAppClassLoader在加載類文件時(shí),會(huì)優(yōu)先自己嘗試加載,如果加載失敗,才會(huì)沿著繼承鏈,依次委托父類加載。
12、tomcat8靜態(tài)資源緩存配置
tomcat8增加了靜態(tài)資源緩存的配置,.cacheMaxSize:靜態(tài)資源緩存最大值,以KB為單位,默認(rèn)值為10240KB .cachingAllowed:是否允許靜態(tài)資源緩存,默認(rèn)為true解決緩存溢出的辦法
對(duì)應(yīng)兩個(gè)參數(shù),解決方法有兩種:
1:考慮增加cache的最大大小
2:關(guān)閉緩存
修改conf/context.xml
<Resources cachingAllowed="true" cacheMaxSize="1048576" ></Resources>
13、未完待續(xù)
未完待續(xù)
