nginx-php類似nginx-lua的擴展,nginx-php中文開發文檔


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

Build Status
GitHub release
license
QQ group

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<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

PHP的日志常量

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

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

PHP的HTTP狀態常量

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.

ngx_php7

Build Status
GitHub release
license
QQ group

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有什么不同

  • 全局變量在每個請求中都不安全
  • 類的靜態變量在每個請求中都不安全
  • 不要設計單例模式
  • 本機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<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

版權和許可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM