nginx-php类似nginx-lua的扩展

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的生态做的贡献

 

ngx_php7

nginx-php类似nginx-lua的扩展
nginx-php类似nginx-lua的扩展
nginx-php类似nginx-lua的扩展
nginx-php类似nginx-lua的扩展

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 的性能测试

Table of contents

官方php有什么不同

  • 全局变量在每个请求中都不安全
  • Static variable of a class is unsafe in per request
  • Do not design singleton mode
  • The native IO function works fine, but it slows down 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<php script code>

context: http, server, location, location if

phase: log

log_by_php_block

syntax: log_by_php_block{php script code}

context: location, location if

phase: log

header_filter_by_php

syntax: header_filter_by_php<php script code>

context: http, server, location, location if

phase: output-header-filter

header_filter_by_php_block

syntax: header_filter_by_php_block{php script code}

context: location, location if

phase: output-header-filter

body_filter_by_php

syntax: body_filter_by_php<php script code>

context: http, server, location, location if

phase: output-body-filter

body_filter_by_php_block

syntax: body_filter_by_php_block{php script code}

context: location, location if

phase: output-body-filter

php_keepalive

syntax: php_keepalive<size>

default: 0

context: http, server

In php, set upstream connection pool size.

php_set

syntax: php_set$variable <php script code>

context: http, server, location, location if

phase: loading-config

Installs a php handler for the specified variable.

php_socket_keepalive

syntax: php_socket_keepalive<size>

default: 0

context: http, server

php_socket_buffer_size

syntax: php_socket_buffer_size<size>

default: 4k

context: http, server, location, location if

Nginx API for php

ngx_exit

syntax: ngx_exit(int $status) : void

parameters:

  • status: int

context: rewrite_by_php*, access_by_php*, content_by_php*

当前请求结束并返回http状态代码。

ngx_query_args

syntax: ngx_query_args(void) : array or ngx::query_args(void) : array

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

通过URL参数(即查询字符串)传递给当前脚本的变量的关联数组。
而不是PHP官方常量$ _GET。

ngx_post_args

syntax: ngx_post_args(void) : array or ngx::post_args(void) : array

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

通过HTTP POST方法传递给当前脚本的变量的关联数组
在请求中使用application / x-www-form-urlencoded或multipart / form-data作为HTTP Content-Type时。
而不是php官方常量$ _POST。

ngx_log_error

syntax: ngx_log_error(int $level, string $log_str) : void or ngx_log::error(int $level, string $log_str) : void

parameters:

  • level: int
  • log_str: string

context: rewrite_by_php*, access_by_php*, content_by_php*

ngx_request_method

syntax: ngx_request_method(void) : string or ngx_request::method(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

使用哪种请求方法来访问页面,例如“ GET”,“ POST”,“ PUT”,“ DELETE”等等。

ngx_request_document_root

syntax: ngx_request_document_root(void) : string or ngx_request::document_root(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

服务器配置文件中定义的当前脚本正在其下执行的文档根目录。

ngx_request_document_uri

syntax: ngx_request_document_uri(void) : string or ngx_request::document_uri(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

ngx_request_script_name

syntax: ngx_request_script_name(void) : string or ngx_request::script_name(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

包含当前脚本的路径。 这对于需要指向自身的页面很有用。
__FILE__常量包含当前(包含)文件的完整路径和文件名。

ngx_request_script_filename

syntax: ngx_request_script_filename(void) : string or ngx_request::script_filename(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

当前正在执行的脚本文件名的绝对路径名。

ngx_request_query_string

syntax: ngx_request_query_string(void) : string or ngx_request::query_string(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

用于访问页面的查询字符串(如果有)。

ngx_request_uri

syntax: ngx_request_uri(void) : string or ngx_request::uri(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

为了访问该页面而给出的URI,例如‘/index.html‘。

ngx_request_server_protocol

syntax: ngx_request_server_protocol(void) : string or ngx_request::server_protocol(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

通过其请求页面的信息协议的名称和修订,例如“ HTTP / 1.0”。

ngx_request_remote_addr

syntax: ngx_request_remote_addr(void) : string or ngx_request::remote_addr(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

用户正在从中查看当前页面的IP地址。

ngx_request_server_addr

syntax: ngx_request_server_addr(void) : string or ngx_request::server_addr(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

当前脚本正在其下执行的服务器的IP地址。

ngx_request_remote_port

syntax: ngx_request_remote_port(void) : int or ngx_request::remote_port(void) : int

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

用户计算机上用于与Web服务器通信的端口。

ngx_request_server_port

syntax: ngx_request_server_port(void) : int or ngx_request::server_port(void) : int

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

Web服务器用于通信的服务器计算机上的端口。 对于默认设置,
这将是"80"; 例如,使用SSL会将其更改为您定义的安全HTTP端口。

ngx_request_server_name

syntax: ngx_request_server_name(void) : string or ngx_request::server_name(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

正在执行当前脚本的服务器主机的名称。
如果脚本在虚拟主机上运行,则将是为该虚拟主机定义的值。

ngx_request_headers

syntax: ngx_request_headers(void): array or ngx_request::headers(void) : array

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

获取http请求的标头完整信息。

ngx_var_get

syntax: ngx_var_get(string $key) : string or ngx_var::get(string $key) : string

parameters:

  • key: string

context: rewrite_by_php*, access_by_php*, content_by_php*

在nginx配置中获取变量。

ngx_var_set

syntax: ngx_var_set(string $key, string $value) : void or ngx_var::set(string $key, string $value) : void

parameters:

  • key: string
  • value: string

context: rewrite_by_php*, access_by_php*, content_by_php*

在nginx配置中设置变量。

ngx_header_set

syntax: ngx_header_set(string $key, string $value) : bool

parameters:

  • key: string
  • value: string

context: rewrite_by_php*, access_by_php*, content_by_php*

设置http响应的头信息。

ngx_header_get

syntax: ngx_header_get(string $key) : string

parameters:

  • key: string

context: rewrite_by_php*, access_by_php*, content_by_php*

获取http响应的头信息。

ngx_header_gets

syntax: ngx_header_gets(void) : array

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

获取http响应的标头完整信息。

ngx_redirect

syntax: ngx_redirect(string $uri, int $status) : bool

parameters:

  • uri: string
  • status: int

context: rewrite_by_php*, access_by_php*, content_by_php*

设置响应头重定向。

ngx_cookie_get_all

syntax: ngx_cookie_get_all(void) : string

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

ngx_cookie_get

syntax: ngx_cookie_get(string $key) : string

parameters:

  • key: string

context: rewrite_by_php*, access_by_php*, content_by_php*

ngx_cookie_set

syntax: ngx_cookie_set(string $data): bool

parameters:

  • data: string

context: rewrite_by_php*, access_by_php*, content_by_php*

Nginx non-blocking API for php

ngx_sleep

syntax: yield ngx_sleep(int seconds)

parameters:

  • secodes: int

context: rewrite_by_php*, access_by_php*, content_by_php*

将程序执行延迟给定的秒数。

ngx_msleep

syntax: yield ngx_msleep(int milliseconds)

parameters:

  • milliseconds: int

context: rewrite_by_php*, access_by_php*, content_by_php*

将程序执行延迟给定的毫秒数。

ngx_socket_create

syntax: ngx_socket_create(int $domain, int $type, int $protocol) : resource

parameters:

  • domain: int
  • type: int
  • protocol: int

context: rewrite_by_php*, access_by_php*, content_by_php*

创建并返回套接字资源,也称为通信的端点。
典型的网络连接由2个套接字组成,其中一个充当客户端的角色,
另一个执行服务器的角色。

ngx_socket_iskeepalive

syntax: ngx_socket_iskeepalive(void) : bool

parameters:

  • void

context: rewrite_by_php*, access_by_php*, content_by_php*

ngx_socket_connect

syntax: ( yield ngx_socket_connect(resource $socket, string $address, int $port) ) : bool

parameters:

  • socket: resource
  • address: string
  • port: int

context: rewrite_by_php*, access_by_php*, content_by_php*

使用套接字资源套接字启动到地址的连接,该套接字必须是有效的
使用ngx_socket_create()创建的套接字资源。

ngx_socket_close

syntax: ( yield ngx_socket_close(resource $socket) ) : bool

parameters:

  • socket: resource

context: rewrite_by_php*, access_by_php*, content_by_php*

ngx_socket_close()关闭套接字给定的套接字资源。 此功能特定于
套接字,不能在任何其他类型的资源上使用。

ngx_socket_send

syntax: ( yield ngx_socket_send(resource $socket, string $buf, int $len) ) : int

parameters:

  • socket: resource
  • buf: string
  • len: int

context: rewrite_by_php*, access_by_php*, content_by_php*

函数ngx_socket_send()将len个字节从buf发送到套接字套接字。

ngx_socket_recv

syntax: ( yield ngx_socket_recv(resource $socket, string &$buf, int $len) ) : int

parameters:

  • socket: resource
  • buf: string
  • len: int

context: rewrite_by_php*, access_by_php*, content_by_php*

ngx_socket_recv()函数从套接字接收buf中的len字节数据。 ngx_socket_recv()可以是
用于从连接的套接字收集数据。

buf通过引用传递,因此必须在参数列表中将其指定为变量。
ngx_socket_recv()从套接字读取的数据将以buf返回。

ngx_socket_recvpage

syntax: ( yield ngx_socket_recvpage(resource $socket, string &$buf, int &$rc) ) : int

parameters:

  • socket: resource
  • buf: string
  • rc: int

context: rewrite_by_php*, access_by_php*, content_by_php*

ngx_socket_recvsync

syntax: ngx_socket_recvsync(resource $socket, string &$buf, int $len) : int

parameters:

  • socket: resource
  • buf: string
  • len: int

context: rewrite_by_php*, access_by_php*, content_by_php*

ngx_socket_clear

syntax: ngx_socket_recv(resource $socket) : bool

parameters:

  • socket: resource

context: rewrite_by_php*, access_by_php*, content_by_php*

Close the socket resource and is blocking but hight performance.

Nginx 常量

版本常量

name value
NGINX_VAR NGINX
NGINX_VERSION 1.12.2
NGX_HTTP_PHP_MODULE_VERSION 0.0.21
NGX_HTTP_PHP_MODULE_NAME ngx_php

log constants for php

name value
NGX_OK 0
NGX_ERROR -1
NGX_AGAIN -2
NGX_BUSY -3
NGX_DONE -4
NGX_DECLINED -5
NGX_ABORT -6

status constants for php

name value
NGX_LOG_STDERR 0
NGX_LOG_EMERG 1
NGX_LOG_ALERT 2
NGX_LOG_CRIT 3
NGX_LOG_ERR 4
NGX_LOG_WARN 5
NGX_LOG_NOTICE 6
NGX_LOG_INFO 7
NGX_LOG_DEBUG 8

http status constants for php

name value
NGX_HTTP_CONTINUE 100
NGX_HTTP_SWITCHING_PROTOCOLS 101
NGX_HTTP_PROCESSING 102
NGX_HTTP_OK 200
NGX_HTTP_CREATED 201
NGX_HTTP_ACCEPTED 202
NGX_HTTP_NO_CONTENT 204
NGX_HTTP_PARTIAL_CONTENT 206
NGX_HTTP_SPECIAL_RESPONSE 300
NGX_HTTP_MOVED_PERMANENTLY 301
NGX_HTTP_MOVED_TEMPORARILY 302
NGX_HTTP_SEE_OTHER 303
NGX_HTTP_NOT_MODIFIED 304
NGX_HTTP_TEMPORARY_REDIRECT 307
NGX_HTTP_PERMANENT_REDIRECT 308
NGX_HTTP_BAD_REQUEST 400
NGX_HTTP_UNAUTHORIZED 401
NGX_HTTP_FORBIDDEN 403
NGX_HTTP_NOT_FOUND 404
NGX_HTTP_NOT_ALLOWED 405
NGX_HTTP_REQUEST_TIME_OUT 408
NGX_HTTP_CONFLICT 409
NGX_HTTP_LENGTH_REQUIRED 411
NGX_HTTP_PRECONDITION_FAILED 412
NGX_HTTP_REQUEST_ENTITY_TOO_LARGE 413
NGX_HTTP_REQUEST_URI_TOO_LARGE 414
NGX_HTTP_UNSUPPORTED_MEDIA_TYPE 415
NGX_HTTP_RANGE_NOT_SATISFIABLE 416
NGX_HTTP_CLOSE 444
NGX_HTTP_NGINX_CODES 494
NGX_HTTP_REQUEST_HEADER_TOO_LARGE 494
NGX_HTTPS_CERT_ERROR 495
NGX_HTTPS_NO_CERT 496
NGX_HTTP_TO_HTTPS 497
NGX_HTTP_CLIENT_CLOSED_REQUEST 499
NGX_HTTP_INTERNAL_SERVER_ERROR 500
NGX_HTTP_NOT_IMPLEMENTED 501
NGX_HTTP_BAD_GATEWAY 502
NGX_HTTP_SERVICE_UNAVAILABLE 503
NGX_HTTP_GATEWAY_TIME_OUT 504
NGX_HTTP_INSUFFICIENT_STORAGE 507

版权和许可

 

Copyright (c) 2016-2020, rryqszq4 <rryqszq@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

nginx-php类似nginx-lua的扩展

上一篇:第一次网页制作


下一篇:关于手机设备网络类型的判断