Couple questions about module behaviour

Maxim Dounin mdounin at
Thu Feb 12 13:52:52 UTC 2015


On Thu, Feb 12, 2015 at 11:05:34AM +0700, hungnv at wrote:

> Hello, 
> I am doing some research and writing small modules using nginx. 
> One of the modules is serving file which is generated 
> dynamically based on user’s request. I found it’s hard to handle 
> 2 states of user’s connection: 
> 1. When user request for object A and object A is being 
> generated, server is processing user’s input to produce output, 
> user closes browser, but server still produce output and just 
> fire an error when it finished all the work and give output 
> buffer to next filter. How to prevent this happens? 

If you return control to the nginx event loop while generating the 
response, you may set appropriate events to detect if the client 
closed the connection.  See ngx_http_upstream_check_broken_connection() 
and related things in the ngx_http_upstream.c for an example.

> 2. When user request large static file, lets say we are using 
> mp4 module, a user requests a 1GB file, he just downloaded 100MB 
> then close the browser, nginx log module produce number of bytes 
> sent in log file is the file size (which is the content length). 
> How to know exactly how many bytes server sent to client (number 
> of bytes that client actually received)? 

Log module, once you use $bytes_sent (or $body_bytes_sent, as 
logged by default) variable, logs the number of bytes sent to the 
client.  It is not the same as the number of bytes actually 
received by the client though, because there are bytes which are 
sent (i.e., passed by nginx to the socket buffer) but not yet 

Maxim Dounin

More information about the nginx-devel mailing list