如何使用 mmap() 启用 Nginx 内存缓存

如何使用 mmap() 启用 Nginx 内存缓存

我是 nginx 管理员新手。我使用 nginx-quic 来通过 QUIC 获得 HTTP/3 等功能。然而,HTTP/3 的执行速度比 HTTP/2 慢 2 到 3 倍,因此我正在尝试优化实验服务器。 (测试用例是 1 GiB MP4 视频。)

显然,nginx 通过标准的 open()/read() 机制将 MP4 文件加载到主内存中。从打开的文件列表可以看出:

root@efk:~# lsof -p 31433
COMMAND   PID     USER   FD      TYPE             DEVICE   SIZE/OFF    NODE NAME
nginx   31433 www-data  cwd       DIR              253,0       4096       2 /
nginx   31433 www-data  rtd       DIR              253,0       4096       2 /
nginx   31433 www-data  txt       REG              253,0   22118168 1978901 /usr/sbin/nginx
nginx   31433 www-data  mem       REG              253,0      31680  786667 /lib/x86_64-linux-gnu/librt-2.27.so
nginx   31433 www-data  mem       REG              253,0     253944  786523 /lib/x86_64-linux-gnu/libnss_systemd.so.2
nginx   31433 www-data  mem       REG              253,0      47568  786659 /lib/x86_64-linux-gnu/libnss_files-2.27.so
nginx   31433 www-data  mem       REG              253,0      97176  786656 /lib/x86_64-linux-gnu/libnsl-2.27.so
nginx   31433 www-data  mem       REG              253,0      47576  786661 /lib/x86_64-linux-gnu/libnss_nis-2.27.so
nginx   31433 www-data  mem       REG              253,0      39744  786657 /lib/x86_64-linux-gnu/libnss_compat-2.27.so
nginx   31433 www-data  mem       REG              253,0    2030928  786645 /lib/x86_64-linux-gnu/libc-2.27.so
nginx   31433 www-data  mem       REG              253,0     116960  786976 /lib/x86_64-linux-gnu/libz.so.1.2.11
nginx   31433 www-data  mem       REG              253,0     461008  786609 /lib/x86_64-linux-gnu/libpcre.so.3.13.3
nginx   31433 www-data  mem       REG              253,0    1700792  786651 /lib/x86_64-linux-gnu/libm-2.27.so
nginx   31433 www-data  mem       REG              253,0      39208  786649 /lib/x86_64-linux-gnu/libcrypt-2.27.so
nginx   31433 www-data  mem       REG              253,0     144976  786664 /lib/x86_64-linux-gnu/libpthread-2.27.so
nginx   31433 www-data  mem       REG              253,0      14560  786650 /lib/x86_64-linux-gnu/libdl-2.27.so
nginx   31433 www-data  mem       REG              253,0     179152  786619 /lib/x86_64-linux-gnu/ld-2.27.so
nginx   31433 www-data  DEL       REG               0,18            3262848 /[aio]
nginx   31433 www-data  DEL       REG                0,5            3262840 /dev/zero
nginx   31433 www-data    0u      CHR                1,3        0t0       6 /dev/null
nginx   31433 www-data    1u      CHR                1,3        0t0       6 /dev/null
nginx   31433 www-data    2w      REG              253,0       3576 4064444 /var/log/nginx/error.log
nginx   31433 www-data    3u     unix 0xffff8f68c5349000        0t0 3262844 type=STREAM
nginx   31433 www-data    4w      REG              253,0          0 4064327 /var/log/nginx/access.log
nginx   31433 www-data    5w      REG              253,0       3576 4064444 /var/log/nginx/error.log
nginx   31433 www-data    6w      REG              253,0      14289 4068656 /var/log/nginx/access-ssl.log
nginx   31433 www-data    7w      REG              253,0      16943 4068657 /var/log/nginx/error-ssl.log
nginx   31433 www-data    8u     IPv4            3262832        0t0     TCP *:http-alt (LISTEN)
nginx   31433 www-data    9u     IPv6            3262833        0t0     TCP *:http-alt (LISTEN)
nginx   31433 www-data   10u     IPv4            3262834        0t0     TCP *:8443 (LISTEN)
nginx   31433 www-data   11u     IPv4            3262835        0t0     UDP *:8443
nginx   31433 www-data   12u     IPv6            3262836        0t0     TCP *:8443 (LISTEN)
nginx   31433 www-data   13u     IPv6            3262837        0t0     UDP *:8443
nginx   31433 www-data   14u     IPv4            3262838        0t0     UDP *:8443
nginx   31433 www-data   15u     IPv6            3262839        0t0     UDP *:8443
nginx   31433 www-data   16r      REG              253,0 1111252479 3148096 /home/www/media/LandArt/LandArt_Novoselec/Land_Art_Ozalj_2020.mp4
nginx   31433 www-data   18u     unix 0xffff8f68c5349c00        0t0 3262847 type=STREAM
nginx   31433 www-data   19u  a_inode               0,13          0    9700 [eventpoll]
nginx   31433 www-data   20u  a_inode               0,13          0    9700 [eventfd]
nginx   31433 www-data   21u  a_inode               0,13          0    9700 [eventfd]
root@efk:~#

特别是,这一行很重要,因为它表明 MP4 正在像常规文件一样打开,而不是 mmap()ed 文件:

nginx   31433 www-data   16r      REG              253,0 1111252479 3148096 /home/www/media/LandArt/LandArt_Novoselec/Land_Art_Ozalj_2020.mp4

我想通过 apache2 中的 MMapFile 指令实现内存 mmap() 缓存。

apache2 中的内存映射文件在 lsof 输出中看起来像这样:

apache2 7778 root  mem    REG   253,0    25076 2496871 /home/www/alu/cms/upload/vijesti/promo/fanzin_skola_hodanja/FRANCUSKI_INSTITUT_13__smb1.jpg
apache2 7778 root  mem    REG   253,0    98367 2490374 /home/www/alu/cms/upload/vijesti/predavanja/Branko_Senoa__deform__sfw.jpg
apache2 7778 root  mem    REG   253,0   147284 2500369 /home/www/alu/cms/upload/vijesti/pozivnice/aluperspektiva_program_plakat_2019.pdf
apache2 7778 root  mem    REG   253,0     5812 2492772 /home/www/alu/cms/upload/vijesti/promo/KtT/KunstTeatar_Tresnjevka__Sl8__smb1.jpg
apache2 7778 root  mem    REG   253,0    38218 2492832 /home/www/alu/cms/upload/vijesti/predavanja/Zvjezdana_Jembrih__Pretkrscansko_u_krscanskom__smb1.jpg
apache2 7778 root  mem    REG   253,0    26240 2496678 /home/www/alu/cms/upload/vijesti/predavanja/Predavanje_i_promocija_u_Atelieru_Mestrovic_image001__smb1.jpg
apache2 7778 root  mem    REG   253,0   111703 2247498 /home/www/alu/cms/upload/vijesti/plakat_martinis.jpg
apache2 7778 root  mem    REG   253,0   123947 2496574 /home/www/alu/cms/upload/vijesti/plakati/VODIC_za_brucose_ak.god._2017._2018._za_web-alu.pdf
apache2 7778 root  mem    REG   253,0   319054 2492595 /home/www/alu/cms/upload/vijesti/natjecaji/Natjecaj_za_upis_na_studij_Intelektualno_vlasnistvo_ak.god._2019-2020.pdf
apache2 7778 root  mem    REG   253,0     7996 2496738 /home/www/alu/cms/upload/vijesti/projekti/Ana_Husman_6_sa_7_METARA_image1.jpg
apache2 7778 root  mem    REG   253,0    22264 2496648 /home/www/alu/cms/upload/vijesti/predavanja/Looking_Forward_to_Yesterday__Slika-2__timo-i-nadia__smb1.jpg
apache2 7778 root  mem    REG   253,0      192 2499950 /home/www/alu/contenido/images/but_ok.gif

我在 nginx 文档中找不到相关命令。仅磁盘缓存和代理。

非常感谢。

马文

相关内容