Nginx的黑魔法:使用NGX-PHP模块低成本实现高性能应用
提到“NGX和PHP”,用过Nginx和PHP的同学第一反应可能是Nginx + PHP -FPM 这种结构。不过本文要提到的技术架构更加简单高效:直接使用Nginx和第三方模块(NGX-PHP),调用PHP Embedded库来实现原本需要跨进程实现的功能。 ,这将显着提高应用程序的性能。
之所以能这么玩,得益于以下两个项目的相关实现:
- PHP 提供了一个有趣的调用方式:它通过支持符号绑定允许其他程序调用它 C Bindings Core 引擎、Zend 。这种调用接口的方法称为PHP SAPI或PHP-Embede d。项目地址:https://github.com/php/php-src/tree/master/sapi/embed
- 2016 年,工程师 Soho 的 rryqszq4 家伙开始尝试 GitHub 上的一个开源项目,以桥接“Nginx”和“ PHP-Embede图书馆”。经过多年的发展,该项目逐渐支持PHP5、PHP7以及最近的PHP8。项目地址:https://github.com/rryqszq4/ngx-php
在Techem Power的测试中,自2020年选择“NGX-PHP”技术出现以来,取得了不错的成绩。例如:2020年第19轮和2022年第21轮。
2020年和2022年两轮框架评分测试
在上次测试中他排名第二,排名第五和第六。
在2022年的测试中,对框架的开销进行评估
如果我们使用众所周知的Node.js + MongoDB作为基准,这个解决方案的成本降低了300%:运行更快、更吃草少了。从另一个角度来看,这个解决方案非常适合“Nginx”和“PHP”的特点:快速、强大。
好的,这就是这个项目的概述。首先,使用 Docker 对其性能进行快速实用的测试。
快速体验
运行以下命令:
docker run --rm -it -v `pwd`/data:/usr/share/nginx/html/data:rw -p 8090:80 soulteary/ngx-php:8-microblog
下载完Docker镜像后,我们会看到与Nginx镜像正常运行时相同的日志输出:
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/10/05 10:25:39 [notice] 1#1: using the "epoll" event method
2022/10/05 10:25:39 [notice] 1#1: nginx/1.23.1
2022/10/05 10:25:39 [notice] 1#1: built by gcc 11.2.1 20220219 (Alpine 11.2.1_git20220219)
2022/10/05 10:25:39 [notice] 1#1: OS: Linux 5.10.76-linuxkit
...
打开浏览器并输入
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/10/05 10:25:39 [notice] 1#1: using the "epoll" event method
2022/10/05 10:25:39 [notice] 1#1: nginx/1.23.1
2022/10/05 10:25:39 [notice] 1#1: built by gcc 11.2.1 20220219 (Alpine 11.2.1_git20220219)
2022/10/05 10:25:39 [notice] 1#1: OS: Linux 5.10.76-linuxkit
...
http://localhost:8090 ,就可以看到效果了。
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
code前端网