四月 19

最近在测试是安装nginx发现不起作用,原因是低版本的linux内核不支持AIO,但是nginx rpm编译的时候加入了–-with-file-aio。



Nginx 性能优异在于善于利用操作系统内核的各种特性,比如 aio/epoll/sendfile (Linux), kqueue (FreeBSD) 等。对于使用 VPS 做图片站的站长来说,使用 nginx 的 aio 特性会大大提高性能,图片站的特点是大量的读 io 操作,nginx aio 不用等待每次 io 的结果有助于并发处理大量 io 和提高 nginx 处理效率。

前段时间有位客户要用 nginx aio,他用 rpm 升级安装 nginx 到 0.8.5 版本时候发现 rpm 包里的 nginx 没有包含 Linux-native aio (asynchronous I/O) 的支持,所以需要下载 nginx 源代码并带上参数 –with-file-aio 编译。除了要带参数外,Linux 内核还必须有支持这一特性的 api,eventfd(),否则在 nginx 错误日志(/var/log/nginx/error.log)里会看到类似的报错:

eventfd() failed (38: Function not implemented)
worker process 1858 exited with fatal code 2 and can not be respawn

要让 nginx 使用 aio 特性还需要修改 nginx 配置文件:

# vi /etc/nginx/nginx.conf
location / {
aio on;
directio 1;
output_buffers 1 128k;

昨天另一客户遇到 nginx 启动配置都正确却无法看到网页的情况,VPSee 刚开始怀疑是他自己的 nginx 配置有误或者防火墙屏蔽了80端口,后来登录到他的 VPS 上发现 nginx 配置的确没问题,能正常启动,可以 telnet 80 端口,但是不能 get 到网页。打开 nginx 日志也发现 eventfd() failed 的错误提示。后来检查他的 Linux VPS 内核版本是 2.6.18,查了一下 man 帮助发现只有 2.6.22 以后版本才支持 eventfd,解决方法很简单,升级内核就可以了。

Linux-native aio 比传统的 POSIX aio 功能更丰富一些,重要的一点是能通过内核加速提供高性能。直接用 Linux-native aio API 比较晦涩,为了方便使用和开发 Linux-native aio 应用程序我们可以用 libaio/libaio-devel 库。不过 nginx 的作者没有用这些库,因为 nginx 需要 eventfd(),而 libaio 库里只有 0.3.107 版本起才支持 eventfd;nginx 也没有用 glibc,因为 glibc 要到 2.8 版本才支持 eventfd(),为了减少对库的依赖性,nginx 干脆直接用 Linux-native aio API (system calls).

$ vi nginx-0.9.1/src/event/modules/ngx_epoll_module.c

 * We call io_setup(), io_destroy() io_submit(), and io_getevents() directly
 * as syscalls instead of libaio usage, because the library header file
 * supports eventfd() since 0.3.107 version only.
 * Also we do not use eventfd() in glibc, because glibc supports it
 * since 2.8 version and glibc maps two syscalls eventfd() and eventfd2()
 * into single eventfd() function with different number of parameters.

这里说到了 eventfd(),eventfd 是 Linux-native aio 其中的一个 API,用来生成 file descriptors,这些 file descriptors 可为应用程序提供更高效 “等待/通知” 的事件机制。和 pipe 作用相似,但比 pipe 更好,一方面它只用到一个 file descriptor(pipe 要用两个),节省了内核资源;另一方面,eventfd 的缓冲区管理要简单得多,pipe 需要不定长的缓冲区,而 eventfd 全部缓冲只有定长 8 bytes.


17 条评论 ("Nginx 不使用 Linux-native aio ")

  1. BHW Says:

    Good day! I know this is kind of off topic but I
    was wondering if you knew where I could locate a captcha plugin for my comment form?
    I'm using the same blog platform as yours and I'm having trouble finding one?
    Thanks a lot!

  2. manicure Says:

    Link exchange is nothing else but it is only placing the other person's website link on your page at proper place and other person will also do similar in support of you.

  3. Leah Says:

    Wow, incredible web site format! How lengthy are you blogging for? you built blogging look easy. The complete look of your respective website is excellent, let alone this article!

  4. Riley Says:

    Way cool! Some very valid points! I appreciate you penning this post and the rest of the site is really good.

  5. Werner Says:

    You really make it seem really easy with your presentation however I in finding this matter to be really one thing that I think I might by no means understand. It seems too complicated and extremely large for me. I’m taking a look forward on your next put up, I will try to get the hold of it!

  6. Lucy Says:

    It’s no wonder alot of the many points are going bad in the meantime. Good job for sending my team in the right direction.

  7. Elizabeth Says:

    Somebody necessarily assist to make critically articles I’d state. That is the very first time I frequented your website page and to this point? I surprised with the analysis you made to create this actual submit amazing. Excellent job!

  8. Al Says:

    Iˇve recently started a web site, the info you offer on this web site has helped me tremendously. Thank you for all of your time & work.

  9. Jaime Says:

    Bashfulness is an ornament to youth, but a reproach to old age.

  10. Stella Says:

    You lost me, friend. I mean, I imagine I get what youre indicating. I recognize what you are saying, but you just seem to have forgotten that you can find some other folks within the world who see this issue for what it genuinely is and may not agree with you. You may possibly be turning away a lot of persons who may have been followers of your blog.

  11. Gale Says:

    Wow, incredible blog format! How lengthy have you ever been running a blog for? you make running a blog glance easy. The full look of your website is magnificent, as well as the content!

  12. Williams Says:

    whoah this blog is excellent i love studying your posts. Keep up the great paintings! You understand, a lot of individuals are searching around for this info, you could aid them greatly.

  13. Merlin Says:

    Some tips i have seen in terms of computer memory is that often there are technical specs such as SDRAM, DDR etc, that must fit the specific features of the motherboard. If the pc’s motherboard is kind of current and there are no computer OS issues, replacing the ram literally takes under an hour. It’s one of several easiest personal computer upgrade processes one can think about. Thanks for discussing your ideas.

  14. Caroline Says:

    Outstanding post but I was wondering if you could write a litte more on this topic? I’d be very grateful if you could elaborate a little bit more. Cheers!

  15. Peyton Says:

    Hello, Neat post. There’s an issue with your web site in web explorer, would test this¡K IE still is the market chief and a large portion of folks will omit your fantastic writing due to this problem.

  16. Matt Says:

    Have you given any consideration at all with converting your main website in to German? I know a several of translaters right here that will might help you do it for no cost if you want to make contact with me personally.

  17. Violet Says:

    We are a group of volunteers and starting a new scheme in our community. Your web site provided us with valuable information to work on. You’ve done an impressive job and our whole community will be grateful to you.