Search code examples
spring-bootsslamazon-ec2httpsamazon-cloudfront

IllegalArgumentException: Invalid character found in the HTTP protocol


I've developed and deployed a Spring Boot REST application in an Amazon EC2 instance. Then I created a CloudFront distribution with behavior of "Redirect HTTP to HTTPS" and origin configuration as "HTTP Only" to be able to access the Rest API via HTTPS.

Normally, everything works fine while the app is accessed through http with EC2's self domain. But when I started to access through https CloudFront link, the application starts dying silently after a while.

What actions should I take to prevent app being terminated?

Here is what I saw in log files. But this is not the crash log, app keeps running after this log:

2020-09-09 00:09:15.877  INFO 27720 --- [nio-8080-exec-3] o.apache.coyote.http11.Http11Processor   : Error parsing HTTP request header
 Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.

java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol [HTTP/1.10x0aHost:]
    at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:560) ~[tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260) ~[tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_252]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_252]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_252]

2020-09-09 11:24:35.544  INFO 27720 --- [nio-8080-exec-5] o.apache.coyote.http11.Http11Processor   : Error parsing HTTP request header
 Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.

java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol [HTTP/1.10x0aHost:]
    at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:560) ~[tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260) ~[tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_252]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_252]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.36.jar!/:9.0.36]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_252]

LOGBACK: No context given for c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@532445947

That's all, no other exception, no shutdown logs, no other clue unfortunately...

I'm using following command to run the application:

sudo nohup java -Dspring.profiles.active=stage -jar myapplication.jar &

Spring Boot version is 2 and EC2 instance is Amazon Linux 2018.3

Edit: I'm adding some images about CloudFront configurations.

Origin Configurations: CloudFront Origin configurations

Behavior Configurations: CloudFront Behavior configurations


Solution

  • Configuring Supported HTTP Versions of CloudFront distribution as

    HTTP/1.1, HTTP/1.0

    instead of

    HTTP/2, HTTP/1.1, HTTP/1.0

    solved the problem.

    The exception in the original post is still present, but it's no longer terminating.

    I think the application serving the Rest API doesn't support HTTP 2, and when redirected from CloudFront some HTTP 2 request arrives. Then handling not supported version of a http request somehow causes some fatal errors resulting termination. Please correct me if I'm wrong.