GithubHelp home page GithubHelp logo

ngx_post_handler's Introduction

nginx post handler

post_handler 的 Nginx 版,嵌入了 PHP-embed,能对 HTTP 请求实现拦截,通过 Header 中是否有相应的 key,判断是否允许当次请求。 做这个的原因是想对 HTTP 大文件上传拦截。php-fpm 默认会直接上传文件,在传递 body 之前,能先对 header 进行判断。

扩展一下可以做成 Nginx 拓展版的 SAPI,类似 Apache 的 apache2handler。

install

  1. 需要 PHP Embed SAPI 的共享库,可以实现通过 --enable-embed 编译出来共享库。
  2. 下载源码
git clone https://github.com/aiwhj/ngx_post_handler.git
  1. 修改 config 编译配置
ngx_addon_name=ngx_http_post_handler_module
HTTP_MODULES="$HTTP_MODULES ngx_http_post_handler_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_post_handler_module.c"
CORE_INCS="$CORE_INCS /Users/roger/.phpbrew/php/php-7.1.15-embed/include/php/ \
            /Users/roger/.phpbrew/php/php-7.1.15-embed/include/php/main \
            /Users/roger/.phpbrew/php/php-7.1.15-embed/include/php/Zend \
            /Users/roger/.phpbrew/php/php-7.1.15-embed/include/php/TSRM \
            -Wall -g"
CORE_LIBS="$CORE_LIBS -lphp7"
  1. 把拓扑编译进 Nginx
cd nginx
./configure --add-module=/source/ngx_post_handler
make && make install
  1. location 下配置关键字 post_handler,值为想要运行的 PHP 脚本。
location ~ \.php$ {
    try_files $uri =404;

    post_handler "if (!isset($_SERVER['HTTP_HANDLER']) || $_SERVER['HTTP_HANDLER'] != 'post') { echo 'do not receive the body' . PHP_EOL; exit(1); }";

    include fastcgi.conf;
    fastcgi_pass php71;
}

也可以直接使用 php 输出。

location /hello_world {
    post_handler "echo 'this is a php script'; exit(1);";
}

注意

exit status 必须返回一个大于 0 的整数 当 exit status 是一个大于 0 的整数的时候,PHP 脚本能输出。

ngx_post_handler's People

Contributors

aiwhj avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

jerrychen2022

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.