• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

nginx-php类似nginx-lua的扩展,nginx-php中文开发文档

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

2020年6月2日10:19:03

github:https://github.com/rryqszq4/ngx_php7

php5的版本 https://github.com/rryqszq4/ngx_php

 

发现是从框架性能测试的 https://www.techempower.com/benchmarks/#section=data-r19&hw=ph&test=fortune发现的

这个是参照ngx_lua做的,目前我自己还未做测试,先把文档翻译成中文的,作者也应该是中国人,官方QQ群:558795330

感谢ta 对发展php的生态做的贡献

 

2020年6月3日09:31:49

因为cnblog的目前不支持mkdown语法,在有道上分享的一个更方便观看的版本

https://note.youdao.com/ynoteshare1/index.html?id=8131b1a6ad57cc15b109bbb9942a72f9&type=note

ngx_php7




ngx_php7是高性能Web服务器nginx的扩展模块,它实现嵌入式php7脚本来处理nginx的位置和变量。

ngx_php7借鉴ngx_lua的设计,并致力于提供比php-cgi,mod_php,php-fpm,和hhvm具有显着性能优势的非阻塞Web服务。

ngx_php7不想替换任何东西,只想提供一个解决方案。

ngx_php5的旧版,它记录了我过去的一些代码实践,也很有价值。

ngx_php7 and php 的性能测试

目录

官方php有什么不同

  • 全局变量在每个请求中都不安全
  • 类的静态变量在每个请求中都不安全
  • 不要设计单例模式
  • 本机IO功能可以正常工作,但是会减慢Nginx的速度

运行条件

  • 仅支持 Linux
  • PHP-7.0.* ~ PHP-7.4.*
  • nginx-1.4.7 ~ nginx-1.17.8

安装

编译安装

 

 

$ wget \'http://php.net/distributions/php-7.3.10.tar.gz\'
$ tar xf php-7.3.10.tar.gz
$ cd php-7.3.10

$ ./configure --prefix=/path/to/php --enable-embed
$ make && make install

$ git clone https://github.com/rryqszq4/ngx_php7.git

$ wget \'http://nginx.org/download/nginx-1.12.2.tar.gz\'
$ tar -zxvf nginx-1.12.2.tar.gz
$ cd nginx-1.12.2

$ export PHP_CONFIG=/path/to/php/bin/php-config
$ export PHP_BIN=/path/to/php/bin
$ export PHP_INC=/path/to/php/include/php
$ export PHP_LIB=/path/to/php/lib

$ ./configure --user=www --group=www \
$             --prefix=/path/to/nginx \
$             --with-ld-opt="-Wl,-rpath,$PHP_LIB" \
$             --add-module=/path/to/ngx_php7/third_party/ngx_devel_kit \
$             --add-module=/path/to/ngx_php7
$ make && make install

CentOS / RedHat 7

 

yum -y install https://extras.getpagespeed.com/release-el7-latest.rpm
yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm yum-utils
yum-config-manager --enable remi-php73
yum install nginx-module-php7

编辑 nginx.conf 并在顶部加载所需的模块:

load_module modules/ndk_http_module.so;
load_module modules/ngx_http_php_module.so;

Docker

 

$ docker build -t nginx-php7 .
$ : "app.conf: Create nginx config"
$ docker run -p 80:80 -v $PWD/app.conf:/etc/nginx/conf.d/default.conf nginx-php7

概要

 

worker_processes  auto;

events {
    worker_connections  102400;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    keepalive_timeout  65;
    
    client_max_body_size 64k;   
    client_body_buffer_size 64k;

    php_ini_path /usr/local/php/etc/php.ini;

    server {
        listen       80;
        server_name  localhost;
        default_type \'application/json; charset=UTF-8\';
    
        location /php {
            content_by_php_block {
                echo "hello ngx_php7";
            }
        }

        location = /ngx_request {
            content_by_php_block {
                echo ngx_request_document_uri();
            }
        }

        # curl /ngx_get?a=1&b=2
        location = /ngx_get {
            content_by_php_block {
                echo "ngx_query_args()\n";
                var_dump(ngx_query_args());
            }
        }

        # curl -d \'a=1&b=2\' /ngx_post
        location = /ngx_post {
            content_by_php_block {
                echo "ngx_post_args()\n";
                var_dump(ngx_post_args());
            }
        }

        location = /ngx_sleep {
            content_by_php_block {
                echo "ngx_sleep start\n";
                yield ngx_sleep(1);
                echo "ngx_sleep end\n";
            }
        }

        location = /ngx_socket2 {
            default_type \'application/json;charset=UTF-8\';
            content_by_php_block {
                $fd = ngx_socket_create();

                yield ngx_socket_connect($fd, "hq.sinajs.cn", 80);

                $send_buf = "GET /list=s_sh000001 HTTP/1.0\r\n
                                            Host: hq.sinajs.cn\r\nConnection: close\r\n\r\n";
                yield ngx_socket_send($fd, $send_buf, strlen($send_buf));

                $recv_buf = "";
                yield ngx_socket_recv($fd, $recv_buf);
                var_dump($recv_buf);
                
                yield ngx_socket_close($fd);
            }
        }

        location = /ngx_var {
            set $a 1234567890;
            content_by_php_block {
                $a = ngx_var_get("a");
                var_dump($a);
            }
        }
        
        # set content-type of response headers
        location = /ngx_header {
            content_by_php_block {
                ngx_header_set("Content-Type", "text/html; charset=UTF-8");
            }
        }

        # run a php file
        location = /php {
            content_by_php_block {
                include "name_of_php_file.php";
            }
        }
        
        # run any php file in root
        location = / {
            content_by_php_block {
                include ngx_var_get("uri");
            }
        }

    }
}

测试

使用Test :: Nginx模块的perl进行测试,搜索和发现ngx_php7中的问题。

 

ngx_php7 test ...
nginx version: nginx/1.12.2
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) 
configure arguments: --prefix=/home/travis/build/rryqszq4/ngx_php7/build/nginx --with-ld-opt=-Wl,-rpath,/home/travis/build/rryqszq4/ngx_php7/build/php/lib --add-module=../../../ngx_php7/third_party/ngx_devel_kit --add-module=../../../ngx_php7
t/001-hello.t ..................... ok
t/002-ini.t ....................... ok
t/003-error.t ..................... ok
t/004-ngx_request.t ............... ok
t/005-ngx_log.t ................... ok
t/006-ngx_sleep.t ................. ok
t/007-ngx_socket.t ................ ok
t/008-ngx_exit.t .................. ok
t/009-ngx_query_args.t ............ ok
t/010-ngx_post_args.t ............. ok
t/011-ngx_constants.t ............. ok
t/012-function.t .................. ok
t/013-class.t ..................... ok
t/014-ngx_var.t ................... ok
t/015-ngx_header.t ................ 1/? WARNING: TEST 2: set content-length of response headers - unexpected extra bytes after last chunk in response: "Testing ngx_header!\x{0a}"
t/015-ngx_header.t ................ ok
t/016-rewrite_by_php.t ............ ok
t/017-ngx_redirect.t .............. ok
t/018-ngx_mysql.t ................. ok
t/019-php_set.t ................... ok
t/020-ngx_cookie.t ................ ok
t/021-content_by_php_block.t ...... ok
t/022-init_worker_by_php_block.t .. ok
All tests successful.
Files=22, Tests=84, 14 wallclock secs ( 0.09 usr  0.02 sys +  2.19 cusr  0.43 csys =  2.73 CPU)
Result: PASS

指令

php_ini_path

syntax: php_ini_path<php.ini file path>

context: http

phase: loading-config

该指令允许加载正式的php配置文件php.ini,该文件将由后续的PHP代码使用。

init_worker_by_php

syntax: init_worker_by_php<php script code>

context: http

phase: starting-worker

init_worker_by_php_block

syntax: init_worker_by_php_block{php script code}

context: http

phase: starting-worker

rewrite_by_php

syntax: rewrite_by_php<php script code>

context: http, server, location, location if

phase: rewrite

In the rewrite phase of nginx, you can execute inline php code.

rewrite_by_php_block

syntax: rewrite_by_php_block{php script code}

context: location, location if

phase: rewrite

In the rewrite phase of nginx, you can execute inline php code.

access_by_php

syntax: access_by_php<php script code>

context: http, server, location, location if

phase: access

In the access phase of nginx, you can execute inline php code.

access_by_php_block

syntax: access_by_php_block{php script code}

context: location, location if

phase: access

In the access phase of nginx, you can execute inline php code.

content_by_php

syntax: content_by_php<php script code>

context: http, server, location, location if

phase: content

In the content phase of nginx, you can execute inline php code.

content_by_php_block

syntax: content_by_php_block{php script code}

context: location, location if

phase: content

In the content phase of nginx, you can execute inline php code.

log_by_php

syntax: log_by_php


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Lua基础简明教程发布时间:2022-07-22
下一篇:
LUA学习之一初次接触发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap