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
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
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
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
-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
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.
'Nginx' 카테고리의 다른 글
Nginx - 컴파일해서 사용하자 (0) | 2020.09.19 |
---|---|
NGINX Tuning For Best Performance (0) | 2020.08.23 |
Nginx 에서 HTTP 에서 HTTPS 으로 Redirect 하기 (0) | 2020.05.23 |
Nginx - mp4 스트리밍 (0) | 2020.05.10 |
nginx 로 proxy 연결시 실제 아이피, 프로토콜 확인하기 (0) | 2020.04.19 |