sendfile

WP:CC 原文链接

SENDFILE(2)                Linux Programmer's Manual               SENDFILE(2)

NAME
       sendfile - transfer data between file descriptors

SYNOPSIS
       #include <sys/sendfile.h>

       ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

DESCRIPTION
       sendfile()  copies  data  between  one  file  descriptor  and  another.
       Because this copying is done within  the  kernel,  sendfile()  is  more
       efficient  than  the  combination  of read(2) and write(2), which would
       require transferring data to and from user space.

       in_fd should be a file descriptor opened for reading and out_fd  should
       be a descriptor opened for writing.

       If  offset  is  not NULL, then it points to a variable holding the file
       offset from which sendfile() will start reading data from in_fd.   When
       sendfile() returns, this variable will be set to the offset of the byte
       following the last byte that was read.  If offset  is  not  NULL,  then
       sendfile()  does not modify the current file offset of in_fd; otherwise
       the current file offset is adjusted to reflect the number of bytes read
       from in_fd.

       If  offset  is  NULL, then data will be read from in_fd starting at the
       current file offset, and the file offset will be updated by the call.

       count is the number of bytes to copy between the file descriptors.

       Presently (Linux 2.6.9): in_fd, must correspond to a  file  which  sup-
       ports mmap(2)-like operations (i.e., it cannot be a socket); and out_fd
       must refer to a socket.

       Applications may wish to fall back  to  read(2)/write(2)  in  the  case
       where sendfile() fails with EINVAL or ENOSYS.

RETURN VALUE
       If  the  transfer was successful, the number of bytes written to out_fd
       is returned.  On error, -1 is returned, and errno is set appropriately.

ERRORS
       EAGAIN Nonblocking I/O has been selected using O_NONBLOCK and the write
              would block.

       EBADF  The input file was not opened for reading or the output file was
              not opened for writing.

       EFAULT Bad address.

       EINVAL Descriptor is not valid or locked, or an mmap(2)-like  operation
              is not available for in_fd.

       EIO    Unspecified error while reading from in_fd.

       ENOMEM Insufficient memory to read from in_fd.

VERSIONS
       sendfile()  is a new feature in Linux 2.2.  The include file <sys/send-
       file.h> is present since glibc 2.1.

CONFORMING TO
       Not specified in POSIX.1-2001, or other standards.

       Other Unix systems implement sendfile() with  different  semantics  and
       prototypes.  It should not be used in portable programs.

NOTES
       If  you  plan  to use sendfile() for sending files to a TCP socket, but
       need to send some header data in front of the file contents,  you  will
       find  it  useful to employ the TCP_CORK option, described in tcp(7), to
       minimize the number of packets and to tune performance.

       In Linux 2.4 and earlier, out_fd could refer to  a  regular  file,  and
       sendfile() changed the current offset of that file.

SEE ALSO
       mmap(2), open(2), socket(2), splice(2)

COLOPHON
       This  page  is  part of release 3.27 of the Linux man-pages project.  A
       description of the project, and information about reporting  bugs,  can
       be found at http://www.kernel.org/doc/man-pages/.

Linux                             2010-02-15                       SENDFILE(2)
### Nginx 中 `sendfile` 配置的使用方法与性能优化 `sendfile` 是 Nginx 提供的一个重要配置参数,用于优化文件传输性能。它通过利用操作系统提供的 `sendfile()` 系统调用,实现高效的零拷贝(zero-copy)文件传输机制。默认情况下,`sendfile` 的配置为 `on`,即启用零拷贝技术。 在 Nginx 的配置文件中,`sendfile` 的启用或禁用非常简单,只需在 `http` 或 `server` 块中添加以下配置: ```nginx sendfile on; ``` 如果需要禁用该功能,可以设置为: ```nginx sendfile off; ``` 通过启用 `sendfile on;`,Nginx 能够直接将文件从内核空间传输到网络接口,而无需将数据复制到用户空间。这种方式显著减少了数据传输过程中的拷贝步骤,从而提升了性能[^4]。 --- ### `sendfile` 的性能优化作用 1. **减少内存使用** 使用 `sendfile` 技术时,文件数据不需要被复制到用户空间缓冲区,因此服务器的内存资源可以用于处理其他任务。这种方式降低了内存的占用,提高了整体系统的资源利用率。 2. **提高传输效率** 由于数据直接从内核缓冲区传输到网络接口,减少了从内核到用户空间的复制操作,文件传输速度得到了显著提升。这种方式特别适用于静态文件传输场景,例如图片、视频、文档等资源的分发[^2]。 3. **降低 CPU 使用率** 数据传输过程中减少了拷贝步骤,CPU 的负担也随之减轻。这使得 CPU 可以更专注于处理其他请求,例如动态内容生成、连接管理等任务,从而进一步提升服务器的性能[^2]。 4. **避免上下文切换开销** 在传统文件传输方式中,每次数据传输都需要在内核模式和用户模式之间切换,而 `sendfile` 技术能够避免这种上下文切换,从而进一步优化性能[^1]。 --- ### 与其他性能优化配置的结合使用 为了充分发挥 `sendfile` 的优势,通常需要结合其他 Nginx 配置进行优化: - **`tcp_nopush` 和 `tcp_nodelay`** 在启用 `sendfile` 的同时,建议配置 `tcp_nopush on;` 和 `tcp_nodelay on;`。`tcp_nopush` 确保数据包尽可能完整地发送,而 `tcp_nodelay` 则减少网络延迟,从而进一步提升传输效率。 ```nginx tcp_nopush on; tcp_nodelay on; ``` - **调整文件描述符限制** 为了支持高并发场景,需要确保操作系统允许 Nginx 进程打开足够多的文件描述符。可以通过以下配置设置最大文件描述符数: ```nginx worker_rlimit_nofile 65535; ``` - **优化 worker 进程和连接数** 设置 `worker_processes` 为服务器的 CPU 核心数,并调整 `worker_connections` 以充分利用系统资源。例如: ```nginx worker_processes auto; events { worker_connections 65535; } ``` --- ### 使用场景与注意事项 1. **适用场景** `sendfile` 最适合用于静态文件的传输优化,例如网站的图片、CSS、JavaScript 文件等。对于动态内容(例如 PHP、Python 生成的响应),`sendfile` 的作用有限。 2. **兼容性** 某些情况下,例如启用了 `X-Accel-Redirect` 或者使用了某些代理配置时,`sendfile` 可能无法正常工作。此时需要根据具体场景进行调整。 3. **日志记录** 启用 `sendfile` 后,Nginx 的访问日志仍然可以正常记录请求信息,但需要注意日志格式和性能之间的平衡。 --- ### 总结 通过合理配置 `sendfile`,Nginx 可以显著提升文件传输的性能,减少内存和 CPU 的开销,尤其适用于高并发和大流量的静态资源传输场景。结合其他优化配置,如 `tcp_nopush`、`tcp_nodelay` 和 `worker_connections`,可以进一步提升服务器的整体性能。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值