Thursday, February 15, 2018

Oracle Linux - Nginx sendfile issues

Nginx is currently a very popular http server and might take over the popular place currently hold by Apache HTTPD. For all good reasons Nginx is becoming very popular as it is blazing fast and is build without the legacy Apache is having. It is build with the modern day requirement set for a http server. One of the things that support the speed of Nginx is the sendfile option.

Sendfile is by default enabled, and if you run a production server that will also provide end users with static files you do want this enabled for sure.

Nginx initial fame came from its awesomeness at sending static files. This has lots to do with the association of sendfile, tcp_nodelay and tcp_nopush in nginx.conf. The sendfile Nginx option enables to use of sendfile for everything related to… sending file.
sendfile allows to transfer data from a file descriptor to another directly in kernel space. sendfile allows to save lots of resources:
sendfile is a syscall, which means execution is done inside the kernel space, hence no costly context switching.
sendfile replaces the combination of both read and write.
here, sendfile allows zero copy, which means writing directly the kernel buffer from the block device memory through DMA.
Even though sendfile is great when you are running your production server you can run into issues when you are doing development. In one of my development setup I am running Oracle Linux 7 in a vagrant box on my Macbook to run an instance on Nginx. I use the default /vagrant mountpoint from within the Oracle Linux vagrant box towards the filesystem of my Macbook as the location to server html files.

Main reason I do like this setup is that I can edit all files directly on my Macbook and have Nginx serve them for testing. The issue being is that Nginx is not always noticing that I changed a html file and keeps serving the old version of the html file instead of the changed version.

As it turns out, the issue is the way sendfile is buffering files and checking for changes on the file. As it is not the Oracle Linux operating system that makes the change to the file but the OS from my Macbook it is not noticed always in the right manner. This is causing a lot of issues while making interactive changes to html code.

Solution for this issue is, disable senfile in Ngnix. You can do so by adding the below line to your Ngnix config file:

sendfile  off;

After this is done, your issue should be resolved and Ngnix should start to provide you updated content instead of buffered content.  
Post a Comment