6

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;
    }
  }
}
nornagon
  • 15,393
  • 18
  • 71
  • 85

5 Answers5

4

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

nornagon
  • 15,393
  • 18
  • 71
  • 85
  • Unsatisfactory. This directive can only take a literal number, not a variable. I need to be able to decide whether to close the connection or not on a per-request basis, possibly after examining upstream response with Lua. – Szczepan Hołyszewski Nov 02 '19 at 18:30
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.

Community
  • 1
  • 1
1

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

Kaspars
  • 1,168
  • 1
  • 9
  • 12
0

You can patch NGINX. Take FastCGI for example, add

if (strcmp((char *)h->key.data, "Connection") == 0 && strcmp((char *)h->value.data, "close") == 0) {
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "Set Connection: close");
     r->keepalive = 0;
}

in src/http/modules/ngx_http_fastcgi_module.c line 1977, right after the header parser.

Lw Cui
  • 483
  • 7
  • 15
0

Disabling keepalive on a secure server can increase the server load, check the "HTTPS server optimization" session on this document.. http://nginx.org/en/docs/http/configuring_https_servers.html

I may want to set a small keepalive_requests value and set an keepalive_requests also