2

I want to modify the source code of Nginx (http://nginx.org/download/nginx-0.7.67.tar.gz) so when it serves a file (reads the file from disk) to count the bytes served and store them somewhere (a datababase perhaps) Since I don't understand C that well (I'm a php developer) I have trouble finding that part in the source coude (must be a look a while or something) Can anyone help me with that? Thank you

PartySoft
  • 2,749
  • 7
  • 39
  • 55
  • I have a hint that is the ngx_open_file ngx_read_file but I can't find the declaration of the functions – PartySoft Nov 29 '10 at 14:49
  • Sorry, but if you are not a C developer you are probably in for an extremely rough time here. – bmargulies Nov 29 '10 at 14:59
  • well I've found the declaration and it's just a renaming to the original open, read functions from C, and It's kind of used everywhere not only when reading a file for outputing it to the client.. – PartySoft Nov 29 '10 at 15:21

1 Answers1

5

Here is an alternate approach to access the information you want nginx to provide that is much safer.

Include bytes_sent as a column in your access log.

If you refer to the HttpLogModule you'll see you can specify bytes_sent as a column in the access log. Combine this with a php script that parses the log file (Perhaps after it is rotated) and you'll be able to avoid c.

log_format sampleformatname '$remote_addr - $remote_user [$time_local]  '
            '"$request" $status '
            '"$http_referer" "$http_user_agent" "$bytes_sent"';

access_log  /path/to/logs/access.log  sampleformatname;

Some benefits to this approach:

  • Ability to upgrade to newer versions of nginx without merging in your changes.
  • Stick with the tools you know (php)
  • offline processing
  • Simpler and safer
Daniel Rucci
  • 2,822
  • 2
  • 32
  • 42
  • I'm already doing that by passing the request transparently to php then with something called x-forward i'm controling the headers, but if the user wants to download a 4GB file and stops at 200mb i will think that he downloaded the whole 4Gb so that's my problem.. – PartySoft Nov 29 '10 at 18:08
  • 1
    I need to discount every 100mB or so as the server serves the file i think, and amazingly there is no module for that, at least for nginx. – PartySoft Nov 29 '10 at 18:09
  • 1
    Nginx will report how many bytes it has flushed to the client, if someone stops a download the number logged by nginx will very close to what the client actually saved. – Daniel Rucci Nov 29 '10 at 18:28
  • 1
    Thank you, this might just work only with tail , grep and a bash script :) It's a wonderfull ideea – PartySoft Nov 30 '10 at 08:30