Apache Coyote Mitigation: A Practical Guide
In this article
Apache Coyote is the default connector used by Apache Tomcat to handle HTTP and HTTPS requests. While it is not a vulnerability itself, it is often associated with security issues such as slowloris attacks, request smuggling, or excessive resource consumption. Mitigating Apache Coyote means hardening Tomcat's connector configuration to reduce these risks, improve performance, and ensure stable operation under load. This guide walks you through the essential steps to secure and optimize your Coyote connector.
What You'll Need
- Access to the Tomcat server (either the server console or the configuration files).
- Administrative privileges to edit the
server.xmlfile (usually located in$CATALINA_HOME/conf/). - A basic understanding of XML syntax and Tomcat's directory structure.
- A text editor (e.g., nano, vim, or Notepad++).
- Optional: a load testing tool like Apache Bench (
ab) or JMeter to verify changes.
Step-by-Step
- Locate the Coyote connector definition. Open
server.xmlin a text editor. Look for the<Connector>elements. The default Coyote HTTP/1.1 connector typically looks like this:<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />. Note theprotocolattribute — if it'sHTTP/1.1ororg.apache.coyote.http11.Http11NioProtocol, that's the Coyote connector. - Set a reasonable connection timeout. The
connectionTimeoutattribute defines the maximum time (in milliseconds) the server will wait for the client to send a request line. A default of 20000 (20 seconds) is common, but for high-security environments, reduce it to 5000–10000 ms. This helps mitigate slowloris attacks where clients send data slowly. - Limit the maximum number of threads. Add or adjust the
maxThreadsattribute. This controls the maximum number of request processing threads. A typical value is 200, but you should base it on your expected concurrent load and available system resources. Setting it too high can exhaust memory; too low may cause request queuing. For example:maxThreads="200". - Enable the accept count. The
acceptCountattribute specifies the maximum queue length for incoming connection requests when all threads are busy. Set it to a reasonable value like 100. This prevents the OS from rejecting connections abruptly. Example:acceptCount="100". - Configure the maximum HTTP header size. Add
maxHttpHeaderSizeto limit the total size of the request headers (in bytes). The default is 8192 (8KB). If you don't need large headers (e.g., for cookies or custom headers), keep it at 8192 or lower. This helps prevent header-based attacks. - Set a request body size limit. The
maxPostSizeattribute limits the maximum size of a POST request body (in bytes). The default is 2MB (2097152). If your application doesn't require large uploads, reduce it to 1MB or less. This prevents memory exhaustion from huge requests. - Enable compression (optional but recommended). Add
compression="on"andcompressionMinSize="2048"to reduce bandwidth usage and improve response times for text-based resources. Also setcompressableMimeType="text/html,text/xml,text/plain,text/css,text/javascript,application/javascript". - Disable unused connectors. If you have multiple connectors (e.g., an AJP connector on port 8009) that you don't use, comment them out or remove them. AJP is often a target for smuggling attacks. Only keep the connectors you actually need.
- Set a keep-alive timeout. The
keepAliveTimeoutattribute defines how long to keep a connection open for subsequent requests. A typical value is 5000–15000 ms. This prevents idle connections from consuming resources indefinitely. - Restart Tomcat. After saving your changes, restart the Tomcat service for the changes to take effect. Use
sudo systemctl restart tomcat(or the equivalent for your system).
Common Mistakes
- Setting maxThreads too high. This can lead to excessive memory usage and thread contention. Always monitor your actual load and adjust accordingly.
- Ignoring the acceptCount. If you set maxThreads but leave acceptCount at its default (100), you may still see connection refusals under heavy load. Tune both together.
- Forgetting to disable unused connectors. Leaving the AJP connector open is a common oversight that exposes an unnecessary attack surface.
- Making changes without testing. Always test after changing connector settings, especially if you reduce timeouts or size limits, to ensure your application still functions correctly.
- Not backing up server.xml. If you make a syntax error, Tomcat may fail to start. Always back up the original file before editing.
Tips
- Use environment-specific configurations. If you have development, staging, and production environments, use different connector settings. For example, production may need higher timeouts for large file uploads.
- Monitor your logs. Tomcat logs (in
$CATALINA_HOME/logs/) can show you if connections are being rejected or if timeouts are occurring. Check them regularly. - Combine with a reverse proxy. Placing a reverse proxy (like Nginx or Apache HTTP Server) in front of Tomcat can handle TLS termination and provide additional security filters, reducing the load on Coyote.
- Consider using the NIO protocol. The default HTTP/1.1 protocol is NIO (Non-blocking I/O), which is more scalable than the older BIO. Ensure you are not using the deprecated BIO protocol.
- Test with a load tool. Use
ab -n 1000 -c 50 http://localhost:8080/to see how your server behaves under load. Adjust parameters based on response times and error rates.
Troubleshooting
- Tomcat fails to start after editing server.xml. Check the syntax of the XML file. Look for missing closing tags or invalid attributes. Use
xmllint --noout server.xmlto validate. If you can't fix it, restore from backup. - Connections are being reset or timing out unexpectedly. This could be due to too low a connectionTimeout or keepAliveTimeout. Increase these values slightly and test again.
- Large file uploads fail. If you reduced maxPostSize, increase it to accommodate your largest legitimate upload. Alternatively, consider using a different connector for uploads.
- Error "Too many open files" in logs. This is usually an OS-level limit. Increase the file descriptor limit for the Tomcat user (e.g., in
/etc/security/limits.conf). - Performance degradation after changes. If you set maxThreads too low, requests may queue up. Monitor thread usage via JMX or the Tomcat Manager app. Adjust maxThreads and acceptCount based on observed metrics.
Frequently Asked Questions
What exactly is Apache Coyote?
Coyote is the name of the connector component in Apache Tomcat that handles HTTP/1.1 and AJP protocols. It processes incoming requests and sends responses.
Is Apache Coyote a security vulnerability?
No, Coyote itself is not a vulnerability. However, its default configuration may be too permissive, which can be exploited if left unhardened.
How do I know which protocol my connector is using?
Check the protocol attribute in the <Connector> element. If it's HTTP/1.1 or org.apache.coyote.http11.Http11NioProtocol, it's Coyote.
Can I disable Coyote entirely?
No, Coyote is essential for handling HTTP requests. Instead, you should configure it securely.
What is the difference between maxThreads and acceptCount?
maxThreads controls the maximum number of concurrent request processing threads. acceptCount is the maximum queue length for pending connections when all threads are busy.
Should I use a reverse proxy in front of Tomcat?
It's a good practice for production environments. A reverse proxy can handle TLS, load balancing, and additional security filtering, while Tomcat focuses on serving the application.