10

In any response served by NGINX, how can one remove the "Connection: keep-alive" header?

We serve 100B+/month of sub 100-byte responses to under 10 clients from NGINX for RTB based ad serving. We're attempting to minimize the overhead (data size) to save on bandwidth costs. We do not want to actually have NGINX close the connection, only remove it from the header. The client is not a browser and will leave the connection open as per the HTTP/1.1 spec.

We have successfully removed the default response headers from our "no operation" response (204) using HttpHeadersMoreModule. It looks like this:

HTTP/1.1 204 No Content
Connection: keep-alive

We want to remove the other header so it looks like this:

HTTP/1.1 204 No Content

Our keepalive_timeout is set without a second value. As per NGINX HttpCoreModule documentation, without this [second] parameter, nginx does not send a Keep-Alive header. Out setting is:

keepalive_timeout 60s;

We've tried using and setting both http://wiki.nginx.org/HttpHeadersMoreModule#more_clear_headers and http://wiki.nginx.org/HttpHeadersMoreModule#more_clear_input_headers:

more_clear_headers 'Connection';
more_clear_input_headers 'Connection';

We've also tried:

rewrite_by_lua '
    ngx.req.clear_header("Connection")
    ';

And even the:

more_clear_headers 'Connection*';

But we still see the header. We send so many responses that the Connection header actually costs us like $200 dollars a month.

Any help is appreciated.

Related and helpful: Nginx and raw headers

Community
  • 1
  • 1
Michael Orlando
  • 111
  • 2
  • 5
  • 3
    as pointed out by @agentzh here: https://github.com/agentzh/headers-more-nginx-module/issues/22#issuecomment-31585052, "The only way to actually remove the Connection header is to patch the Nginx core, that is, editing the C function ngx_http_header_filter in the src/http/ngx_http_header_filter_module.c file." – Michael Orlando Jan 04 '14 at 19:13

1 Answers1

0

I don't know if this works in your case, but I had the same issue and I fixed it using keepalive_requests:

    location xxx {
      keepalive_requests 1;
 
      ....

    }
Federico Bellini
  • 365
  • 1
  • 7
  • 18
  • This will disable keep-alive. From the keepalive_requests doc: "After the maximum number of requests is made, the connection is closed.". – pba Oct 15 '22 at 12:22