Check for connection:close
Problem
Reverse proxies like to turn a connection:keep-alive to a connection:close for the backend. This means a connection CAN'T be reused, it must be closed with res.end(). Else the reverse proxy thinks that the request isn't fully answered, and keeps it in the internal buffer. This is what I watched at lighttpd and nginx. For nginx there is a easy work around with just disabling the buffer, but there is no such setting for lighttpd. So please: close the connection if the connection:close header is set
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Connection Close Handling for Reverse Proxies
Reverse proxies like lighttpd and nginx may change the connection header from 'keep-alive' to 'close', preventing connection reuse. This leads to incomplete request handling as the proxy expects a closed connection to finalize the response. Without explicitly closing the connection when 'connection: close' is set, the proxy may retain the request in its internal buffer, causing delays or failures in response delivery.
Awaiting Verification
Be the first to verify this fix
- 1
Check for Connection Header
Inspect the incoming request headers to determine if 'connection: close' is present. This will dictate how the response should be handled.
javascriptconst isConnectionClose = req.headers['connection'] === 'close'; - 2
Handle Response Appropriately
If the 'connection: close' header is detected, ensure that the response is finalized with res.end() to properly close the connection.
javascriptif (isConnectionClose) { res.end(); } - 3
Log Connection Handling
Add logging to track when connections are closed due to the 'connection: close' header. This can help in monitoring and debugging issues related to connection handling.
javascriptconsole.log('Connection closed due to header: connection: close'); - 4
Test with Reverse Proxy
Deploy the changes in a staging environment with a reverse proxy (lighttpd or nginx) to verify that connections are being closed as expected when 'connection: close' is set.
bashcurl -H 'Connection: close' http://yourserver.com/test - 5
Monitor Application Behavior
After deployment, monitor the application logs and performance metrics to ensure that the changes have resolved the issue without introducing new problems.
bashtail -f /var/log/yourapp.log
Validation
Confirm that requests with the 'connection: close' header are being processed correctly and that no requests are left hanging in the proxy buffer. Use tools like curl to simulate requests and check server logs for connection closure events.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep