Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

ngx-php

> DevOps
Open source

ngx-php - Embedded php7 or php8 scripting language for nginx module. Mainline development version of the ngx-php.

700 stars0 likes0 views
WebsiteGitHub

About

ngx-php - Embedded php7 or php8 scripting language for nginx module. Mainline development version of the ngx-php.

ngx-php

ngx-php is an extension module of high-performance web server nginx, which implements embedded PHP7 and PHP8 script to process nginx location and variables.

ngx-php draws on the design of ngx_lua and is committed to providing non-blocking web services with significant performance advantages over php-cgi, mod_php, php-fpm and hhvm.

ngx-php doesn't want to replace anything, just want to provide a solution.

There is a legacy version of ngx_php5, which records some of my past code practices and is also valuable.

Benchmarks about ngx-php and php

Table of contents

  • What's different with official php
  • Requirement
  • Installation
  • Synopsis
  • Test
  • Directives
  • Nginx API for php
  • Nginx non-blocking API for php
  • Nginx constants
  • Copyright and License

What's different with official php

  • Global variable is unsafe in per request
  • 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

Requirement

  • Linux only
  • PHP-7.* ~ PHP-8.3
  • nginx-1.4.7 ~ nginx-1.26.x

Installation

Compile 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

Edit nginx.conf and load the required modules at the top:

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

Ubuntu

…

Mac osx

https://github.com/rryqszq4/ngx-php/blob/master/docs/zh-cn/osx_install.md

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

Synopsis

…

Test

Using the perl of Test::Nginx module to testing, searching and finding out problem in ngx-php.

…

Directives

  • php_ini_path
  • init_worker_by_php
  • init_worker_by_php_block
  • rewrite_by_php
  • rewrite_by_php_block
  • access_by_php
  • access_by_php_block
  • content_by_php
  • content_by_php_block
  • log_by_php
  • log_by_php_block
  • header_filter_by_php
  • header_filter_by_php_block
  • body_filter_by_php
  • body_filter_by_php_block
  • php_keepalive
  • php_set
  • php_socket_keepalive
  • php_socket_buffer_size

php_ini_path

syntax: php_ini_path``

context: http

phase: loading-config

This directive allows loading the official php configuration file php.ini, which will be used by subsequent PHP code.

init_worker_by_php

syntax: init_worker_by_php``

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

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

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

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

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

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

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

default: 0

context: http, server

In php, set upstream connection pool size.

php_set

syntax: php_set$variable ``

context: http, server, location, location if

phase: loading-config

Installs a php handler for the specified variable.

php_socket_keepalive

syntax: php_socket_keepalive``

default: 0

context: http, server

php_socket_buffer_size

syntax: php_socket_buffer_size``

default: 4k

context: http, server, location, location if

Nginx API for php

  • ngx_exit
  • ngx_query_args
  • ngx_post_args
  • ngx_log_error
  • ngx_request_method
  • ngx_request_document_root
  • ngx_request_document_uri
  • ngx_request_script_name
  • ngx_request_script_filename
  • ngx_request_query_string
  • ngx_request_uri
  • ngx_request_server_protocol
  • ngx_request_remote_addr
  • ngx_request_server_addr
  • ngx_request_remote_port
  • ngx_request_server_port
  • ngx_request_server_name
  • ngx_request_headers
  • ngx_var_get
  • ngx_var_set
  • ngx_header_set
  • ngx_header_get
  • ngx_header_get_all
  • ngx_redirect
  • ngx_cookie_get_all
  • ngx_cookie_get
  • ngx_cookie_set

ngx_exit

syntax: ngx_exit(int $status) : void

parameters:

  • status: int

context: rewrite_by_php*, access_by_php*, content_by_php*

End of current request and return http status code.

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*

An associative array of variables passed to the current script via the URL parameters (aka. query string).
Instead of php official constant $_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*

An associative array of variables passed to the current script via the HTTP POST method
when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
Instead of php official constant $_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*

Which request method was used to access the page, such as 'GET','POST','PUT','DELETE' and so on.

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*

The document root directory under which the current script is executing, as defined in the server's configuration file.

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*

Contains the current script'path. This is useful for pages which need to point to the themselves.
The FILE constant contains the full path and filename of the current (included) 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*

The absolute pathname of the currently executing script file name.

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*

The query string, if any, via which the page was accessed.

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*

The URI which was given in order to access this page, for instance, '/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*

Name and revision of th information protocol via which the page was requested, such as 'HTTP/1.0'.

ngx_request_remote_addr

syntax: `ngx_request_remote_addr(void) :

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Cmodulenginxnginx-modulephp

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理