title: Remove "Connection: keep-alive" response header in NGINX 204 response with HTTP/1.1
body: <p>In any response served by NGINX, how can one remove the "Connection: keep-alive" header?</p>
  
  <p>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.</p>
  
  <p>We have successfully removed the default response headers from our "no operation" response (204) using <a href="https://github.com/agentzh/headers-more-nginx-module" rel="noreferrer">HttpHeadersMoreModule</a>. It looks like this:</p>
  
  <pre><code>HTTP/1.1 204 No Content
  Connection: keep-alive
  </code></pre>
  
  <p>We want to remove the other header so it looks like this:</p>
  
  <pre><code>HTTP/1.1 204 No Content
  </code></pre>
  
  <p>Our keepalive_timeout is set without a second value. As per <a href="http://wiki.nginx.org/HttpCoreModule#keepalive_timeout" rel="noreferrer">NGINX HttpCoreModule documentation</a>, without this [second] parameter, nginx does not send a Keep-Alive header. Out setting is:</p>
  
  <pre><code>keepalive_timeout 60s;
  </code></pre>
  
  <p>We've tried using and setting both <a href="http://wiki.nginx.org/HttpHeadersMoreModule#more_clear_headers" rel="noreferrer">http://wiki.nginx.org/HttpHeadersMoreModule#more_clear_headers</a>
  and <a href="http://wiki.nginx.org/HttpHeadersMoreModule#more_clear_input_headers" rel="noreferrer">http://wiki.nginx.org/HttpHeadersMoreModule#more_clear_input_headers</a>:</p>
  
  <pre><code>more_clear_headers 'Connection';
  more_clear_input_headers 'Connection';
  </code></pre>
  
  <p>We've also tried:</p>
  
  <pre><code>rewrite_by_lua '
      ngx.req.clear_header("Connection")
      ';
  </code></pre>
  
  <p>And even the:</p>
  
  <pre><code>more_clear_headers 'Connection*';
  </code></pre>
  
  <p>But we still see the header.  We send so many responses that the Connection header actually costs us like $200 dollars a month.</p>
  
  <p>Any help is appreciated.</p>
  
  <p>Related and helpful: <a href="https://stackoverflow.com/questions/11817662/nginx-and-raw-headers">Nginx and raw headers</a></p>