vert.x icon indicating copy to clipboard operation
vert.x copied to clipboard

Non keep-alive HTTP/1.x requests close the connection immediately with compressed responses

Open leobispo opened this issue 9 years ago • 54 comments

Hi all,

First of all, Thank you for the amazing work with vertx.

I am testing the Version 3.5.0 with an nginx reverse proxy.

If I turn off the keep-alive functionality, the HttpServerResponseImpl is not answering anything.

You can test using the following HTTP requests:

Not working

GET / HTTP/1.0.
Host: my-server.com
X-Real-IP: 127.0.0.1.
X-Forwarded-For: 127.0.0.1.
Connection: close.
Cache-Control: max-age=0.
Upgrade-Insecure-Requests: 1.
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36.
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8.
Accept-Encoding: gzip, deflate.
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4.
.

Working

GET / HTTP/1.1.
Host: my-server.com
X-Real-IP: 127.0.0.1.
X-Forwarded-For: 127.0.0.1.
Cache-Control: max-age=0.
Upgrade-Insecure-Requests: 1.
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36.
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8.
Accept-Encoding: gzip, deflate.
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4.
.

This is because of the Connection: close

After a debug, I found that if HttpServerResponseImpl line 442 keepAlive is false, it is working as expected

    if (!keepAlive) {
      closeConnAfterWrite();
      closed = true;
    }

So I suspect that the closeConnAfterWrite is not working as expected.

I would appreciate if you can take a look on it.

leobispo avatar Oct 20 '17 15:10 leobispo

thanks we will investigate soon

vietj avatar Oct 20 '17 15:10 vietj

can you provide a reproducer ? I've got a case that works here.

vietj avatar Oct 20 '17 15:10 vietj

with HTTP/1.0 the Connection: close header has no meaning, what matters is the Connection: keep-alive header.

vietj avatar Oct 20 '17 15:10 vietj

The easiest way to reproduce is to create a nginx reverse proxy to a vertx http app and then try to access it via the proxy

leobispo avatar Oct 20 '17 15:10 leobispo

can you provide a small nginx configuration so we can try it ?

vietj avatar Oct 20 '17 15:10 vietj

The following configuration should do the trick.

server {
  listen       *:80;
  server_name  my-server.com;

  access_log            /var/log/nginx/my-server-access.log combined;
  error_log             /var/log/nginx/my-server-error.log;

  location / {            
    proxy_pass            http://localhost:8080;
    proxy_read_timeout    90;
    proxy_connect_timeout 90;
    proxy_set_header      Host $host;
    proxy_set_header      X-Real-IP $remote_addr;
    proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header      Proxy "";
  }
}

leobispo avatar Oct 20 '17 16:10 leobispo

can you give a CURL command line to send to the proxy to do the check ?

vietj avatar Oct 20 '17 16:10 vietj

This will call the proxy. Nginx will redirect to you app on port 8080

curl -XGET http://my-server.com

leobispo avatar Oct 20 '17 16:10 leobispo

Don't forget to add the my-server to your host pointing to 127.0.0.1

leobispo avatar Oct 20 '17 16:10 leobispo

I can't see where HTTP/1.0 is configured

vietj avatar Oct 20 '17 16:10 vietj

it is default... if you don't pass in the configuration

proxy_http_version 1.1; proxy_set_header Connection "";

nginx will assume 1.0

leobispo avatar Oct 20 '17 16:10 leobispo

ok, thanks will investigate

vietj avatar Oct 20 '17 16:10 vietj

I've just tried and the server disconnects after the response is sent, here is my server app test:

    Vertx vertx = Vertx.vertx();
    HttpServer server = vertx.createHttpServer();
    server.requestHandler(req -> {
      System.out.println(req.version());
      req.response().end("Hello World");
    }).listen(8081);

full nginx config:

events {
}
http {
  server {
    listen       *:8080;
    server_name  my-server.com;
    location / {
      proxy_pass            http://localhost:8081;
      proxy_read_timeout    90;
      proxy_connect_timeout 90;
      proxy_set_header      Host $host;
      proxy_set_header      X-Real-IP $remote_addr;
      proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header      Proxy "";
    }
  }
}

vietj avatar Oct 20 '17 16:10 vietj

This example worked for me as well...

leobispo avatar Oct 20 '17 16:10 leobispo

maybe it comes from the application ?

vietj avatar Oct 20 '17 16:10 vietj

I don't know. It looks like that the connection is closed before the event loop flushes the data. Maybe because your example is too simple, or there is nothing else in the event loop, things seems to work.

But this is just my guess, because when i turn on the keep-alive in the nginx, it is working flawless

leobispo avatar Oct 20 '17 16:10 leobispo

Ok, just to give you one more feedback. Even with keep-alive, after sometime, the app started to not respond connections.

With 3.4.2 it is working perfectly

leobispo avatar Oct 20 '17 17:10 leobispo

I will add a delay to see how it goes

vietj avatar Oct 20 '17 17:10 vietj

it would be great if you can investigate and give more insight about what is causing the issue

vietj avatar Oct 20 '17 17:10 vietj

I will.

leobispo avatar Oct 20 '17 17:10 leobispo