nginx and proxy_pass - send Connection: close headers

10,212

Solution 1

Setting keepalive_requests 0; convinced nginx to send Connection: close.

Solution 2

The Connection header is specific to a connection.

From the HTTP/1.1 spec,

The Connection general-header field allows the sender to specify options that are desired for that particular connection and MUST NOT be communicated by proxies over further connections.

So what nginx sends is independent of what's being sent from upstream and must be. Here are some options:

keepalive_requests 0 works if you never want connections reused.

keepalive_disable ua works for a particular user agent.

And this answer works an ip.

Solution 3

Take a look at fastcgi_finish_request() if you're using PHP-FPM or PHP FastCGI:

This feature allows you to speed up implementation of some php queries. Acceleration is possible when there are actions in the process of script execution that do not affect server response. For example, saving the session in memcached can occur after the page has been formed and passed to a web server. fastcgi_finish_request() is a php feature, that stops the response output. Web server immediately starts to transfer response “slowly and sadly” to the client, and php at the same time can do a lot of useful things in the context of a query, such as saving the session, converting the downloaded video, handling all kinds of statistics, etc.

http://php-fpm.org/wiki/Features#fastcgi_finish_request.28.29

Share:
10,212
nornagon
Author by

nornagon

Changing the world, one pipette instruction at a time.

Updated on June 20, 2022

Comments

  • nornagon
    nornagon almost 2 years

    nginx seems to be replacing the Connection: close header that upstream is sending, and replacing it with a Connection: keep-alive header. Is there any way I can override it?

    http {
      upstream main {
        server 127.0.0.1:8000;
      }
      server {
        listen 443;
        ssl on;
        ssl_certificate server.crt;
        ssl_certificate_key server.key;
        location / {
          proxy_pass http://main;
        }
        location /find {
          proxy_pass http://main;
          proxy_buffering off;
        }
      }
    }