Nginx

How does Nginx serve static files faster that app servers like Unicorn?

빠빠담 2020. 9. 1. 21:34
반응형

https://www.reddit.com/r/devops/comments/5ereqf/how_does_nginx_serve_static_files_faster_that_app/daez65w/

 

How does Nginx serve static files faster that app servers like Unicorn?

I don't know what you mean by "Nginx isn't sending it, the kernel is" Nginx makes sendfile syscall, which triggers DMA. The concurrency comes...

www.reddit.com

 

If you're using Nginx as a reverse proxy for a Rails app and you configure it to handle requests for the Rail's static files, how does it make faster?

Does it cache those files? or does it somehow grab those static files without going through the App server?

Thanks for any help guys!

 

 

For static files nginx uses sendfile(). So nginx itself is actually not sending the file, the kernel is. This means nginx can do other things while the kernel sends the file.

 

 

 

 

 

======================================================================

 

 

 

 

 

 

I don't know what you mean by

"Nginx isn't sending it, the kernel is"

Nginx makes sendfile syscall, which triggers DMA. The concurrency comes from the fact that DMA is performed by a hardware other than the CPU so it can execute another thread/process.

What you are saying implies that all syscalls are asynchronous, which is not true. You certainly are blocked while a page fault is handled, for instance.

 

 

 

level 2

peatymike

4 points·3 years ago

Its not DMA. You are correct in that all syscalls aren't async. Nginx is async and it uses sendfile to get the kernel to write data frome one file descriptor to another. The file descripors involved in this case is a file on disk and a network socket.

Sendfile frees Nginx from having to read the file from disk and then write it to the network socket. So Nginx can do other things while the kermel sends the data. When the kernel is done Nginx will get notified and do further procesding if needed.

Another good thing about sendfile is that data doesnt have to go from kernelspace to userspace and then back again. It all happens in kernelspace, saving some context switching and unnecessary data copying.

 

 

 

 

level 3

ihatemovingparts

1 point·3 years ago

Sendfile frees Nginx from having to read the file from disk and then write it to the network socket.

True.

So Nginx can do other things while the kermel sends the data.

False. sendfile(2) can be used in a blocking and non-blocking manner. From the man page, the gain is in not having to transfer the data out of the kernel and then back into the kernel.

 

 

 

 

 

level 4

peatymike

2 points·3 years ago

You are correct about the async part of sendfile usage. I read up on it, and it seems like threadpools were implemented in Nginx to get disk IO out of the main event loop.

https://www.nginx.com/blog/thread-pools-boost-performance-9x/

 

 

 

 

level 3

nthcxd

-1 points·3 years ago

So you're saying it's not the CPU nor a DMA engine moving data from a hardware buffer in the disk to a hardware send buffer in the network interface card. Now I'm curious how that magic happens in your system actually.

 

 

 

 

 

level 4

peatymike

2 points·3 years ago

The kernel does the work and it uses the CPU, disk, memory and NIC to do it. There is no magic.

반응형