HttpRequest maximum allowable size in tomcat?
Asked Answered
R

5

72

What is the maximum data size I can send in a single HttpURLConnection to Tomcat? Is there any limitation for the request size?

Redman answered 1/6, 2010 at 6:30 Comment(0)
J
66

The connector section has the parameter

maxPostSize

The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes).

Another Limit is:

maxHttpHeaderSize The maximum size of the request and response HTTP header, specified in bytes. If not specified, this attribute is set to 4096 (4 KB).

You find them in

$TOMCAT_HOME/conf/server.xml
Jordonjorey answered 1/6, 2010 at 6:40 Comment(3)
SO to set a limit on the size of a request I should set "maxPostSize" with the maximum allowed value ...right ?Shenyang
Setting value of maxPostSize to 0 cause to 0 POST size limit. For unlimited size value must be less than 0.Isbell
This doesn't affect the maximum allowed POST size though, just the maximum size that Tomcat will try to parse as www-form-urlencoded.Clemence
E
80

You have to modify two possible limits:

In conf\server.xml

<Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
                maxPostSize="67589953" />

In webapps\manager\WEB-INF\web.xml

<multipart-config>
  <!-- 52MB max -->
  <max-file-size>52428800</max-file-size>
  <max-request-size>52428800</max-request-size>
  <file-size-threshold>0</file-size-threshold>
</multipart-config>
Eal answered 1/6, 2012 at 10:19 Comment(4)
what if I dont have multipart-config tag in my web xml, is there a default value ?Handmedown
I also dont have multipart-config tag in my web xml. Editing server.xml alone made no difference. Can you please tell me what other changes I need to make to receive a POST request where Form param value is greater than 2MB?Calque
I think the default size is unlimited. docs.oracle.com/javaee/6/tutorial/doc/gmhal.htmlBirkenhead
FYI - 52428800 is 50MB, not 52MB.Hesitation
J
66

The connector section has the parameter

maxPostSize

The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes).

Another Limit is:

maxHttpHeaderSize The maximum size of the request and response HTTP header, specified in bytes. If not specified, this attribute is set to 4096 (4 KB).

You find them in

$TOMCAT_HOME/conf/server.xml
Jordonjorey answered 1/6, 2010 at 6:40 Comment(3)
SO to set a limit on the size of a request I should set "maxPostSize" with the maximum allowed value ...right ?Shenyang
Setting value of maxPostSize to 0 cause to 0 POST size limit. For unlimited size value must be less than 0.Isbell
This doesn't affect the maximum allowed POST size though, just the maximum size that Tomcat will try to parse as www-form-urlencoded.Clemence
C
20

The full answer

1. The default (fresh install of tomcat)

When you download tomcat from their official website (of today that's tomcat version 9.0.26), all the apps you installed to tomcat can handle HTTP requests of unlimited size, given that the apps themselves do not have any limits on request size.

However, when you try to upload an app in tomcat's manager app, that app has a default war file limit of 50MB. If you're trying to install Jenkins for example which is 77 MB as ot today, it will fail.

2. Configure tomcat's per port http request size limit

Tomcat itself has size limit for each port, and this is defined in conf\server.xml. This is controlled by maxPostSize attribute of each Connector(port). If this attribute does not exist, which it is by default, there is no limit on the request size.

To add a limit to a specific port, set a byte size for the attribute. For example, the below config for the default 8080 port limits request size to 200 MB. This means that all the apps installed under port 8080 now has the size limit of 200MB

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443"
           maxPostSize="209715200" />

3. Configure app level size limit

After passing the port level size limit, you can still configure app level limit. This also means that app level limit should be less than port level limit. The limit can be done through annotation within each servlet, or in the web.xml file. Again, if this is not set at all, there is no limit on request size.

To set limit through java annotation

@WebServlet("/uploadFiles")
@MultipartConfig( fileSizeThreshold = 0, maxFileSize = 209715200, maxRequestSize = 209715200)
public class FileUploadServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        // ...
    }
}

To set limit through web.xml

<web-app>
  ...
  <servlet>
    ...
    <multipart-config>
      <file-size-threshold>0</file-size-threshold>
      <max-file-size>209715200</max-file-size>
      <max-request-size>209715200</max-request-size>
    </multipart-config>
    ...
  </servlet>
  ...
</web-app>

4. Appendix - If you see file upload size error when trying to install app through Tomcat's Manager app

Tomcat's Manager app (by default localhost:8080/manager) is nothing but a default web app. By default that app has a web.xml configuration of request limit of 50MB. To install (upload) app with size greater than 50MB through this manager app, you have to change the limit. Open the manager app's web.xml file from webapps\manager\WEB-INF\web.xml and follow the above guide to change the size limit and finally restart tomcat.

Coeternal answered 24/9, 2019 at 3:45 Comment(3)
In debian 10, at least, the default tomcat install does not come with a web.xml for the manager app -- or any other files under webapps, for that matter . How can I get it to unpack the app so I can get at the config file?Newish
@Coeternal thanks for your great description. Could you maybe point to some doc which confirms the "unlimited size"? Thank you.Hennessy
@Coeternal you said: "If this attribute does not exist, which it is by default, there is no limit on the request size." but the documentation (tomcat.apache.org/tomcat-9.0-doc/config/http.html) mention for maxPostSize "If not specified, this attribute is set to 2097152 (2 megabytes)."Piles
O
3

Although other answers include some of the following information, this is the absolute minimum that needs to be changed on EC2 instances, specifically regarding deployment of large WAR files, and is the least likely to cause issues during future updates. I've been running into these limits about every other year due to the ever-increasing size of the Jenkins WAR file (now ~72MB).

More specifically, this answer is applicable if you encounter a variant of the following error in catalina.out:

SEVERE [https-jsse-nio-8443-exec-17] org.apache.catalina.core.ApplicationContext.log HTMLManager:
FAIL - Deploy Upload Failed, Exception:
[org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException:
the request was rejected because its size (75333656) exceeds the configured maximum (52428800)]

On Amazon EC2 Linux instances, the only file that needs to be modified from the default installation of Tomcat (sudo yum install tomcat8) is:

/usr/share/tomcat8/webapps/manager/WEB-INF/web.xml

By default, the maximum upload size is exactly 50MB:

<multipart-config>
  <!-- 50MB max -->
  <max-file-size>52428800</max-file-size>
  <max-request-size>52428800</max-request-size>
  <file-size-threshold>0</file-size-threshold>
</multipart-config>

There are only two values that need to be modified (max-file-size and max-request-size):

<multipart-config>
  <!-- 100MB max -->
  <max-file-size>104857600</max-file-size>
  <max-request-size>104857600</max-request-size>
  <file-size-threshold>0</file-size-threshold>
</multipart-config>

When Tomcat is upgraded on these instances, the new version of the manager web.xml will be placed in web.xml.rpmnew, so any modifications to the original file will not be overwritten during future updates.

Omphalos answered 29/6, 2018 at 13:57 Comment(0)
M
2

Just to add to the answers, App Server Apache Geronimo 3.0 uses Tomcat 7 as the web server, and in that environment the file server.xml is located at <%GERONIMO_HOME%>/var/catalina/server.xml.

The configuration does take effect even when the Geronimo Console at Application Server->WebServer->TomcatWebConnector->maxPostSize still displays 2097152 (the default value)

Module answered 19/4, 2013 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.