Skip to content

Commit 8294dd0

Browse files
committed
Initial commit
0 parents  commit 8294dd0

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
nginx post handler
2+
=====
3+
[post_handler](https://github.com/aiwhj/post_handler.git) 的 Nginx 版,嵌入了 PHP-embed,能对 HTTP 请求实现拦截,通过 Header 中是否有相应的 key,判断是否允许当次请求。
4+
做这个的原因是想对 HTTP 大文件上传拦截。php-fpm 默认会直接上传文件,在传递 body 之前,先对 header 进行判断。
5+
6+
## install
7+
1. 需要 PHP Embed SAPI 的共享库,可以实现通过 `--enable-embed` 编译出来共享库。
8+
2. 下载源码
9+
```shell
10+
git clone https://github.com/aiwhj/ngx_post_handler.git
11+
```
12+
3. 修改 config 编译配置
13+
```shell
14+
ngx_addon_name=ngx_http_post_handler_module
15+
HTTP_MODULES="$HTTP_MODULES ngx_http_post_handler_module"
16+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_post_handler_module.c"
17+
CORE_INCS="$CORE_INCS /Users/roger/.phpbrew/php/php-7.1.15-embed/include/php/ \
18+
/Users/roger/.phpbrew/php/php-7.1.15-embed/include/php/main \
19+
/Users/roger/.phpbrew/php/php-7.1.15-embed/include/php/Zend \
20+
/Users/roger/.phpbrew/php/php-7.1.15-embed/include/php/TSRM \
21+
-Wall -g"
22+
CORE_LIBS="$CORE_LIBS -lphp7"
23+
24+
```
25+
4. 把拓扑编译进 Nginx
26+
```shell
27+
cd nginx
28+
./configure --add-module=/source/ngx_post_handler
29+
make && make install
30+
```
31+
5. location 下配置关键字 `post_handler`,值为想要运行的 PHP 脚本。
32+
```conf
33+
location ~ \.php$ {
34+
try_files $uri =404;
35+
36+
post_handler "if (!isset($_SERVER['HTTP_HANDLER']) || $_SERVER['HTTP_HANDLER'] != 'post') { echo 'do not receive the body' . PHP_EOL; exit(1); }";
37+
38+
include fastcgi.conf;
39+
fastcgi_pass php71;
40+
}
41+
```
42+
也可以直接使用 php 输出。
43+
```conf
44+
location /hello_world {
45+
post_handler "echo 'this is a php script'; exit(1);";
46+
}
47+
```
48+
## 注意
49+
`exit status` 必须返回一个大于 0 的整数
50+
`exit status` 是一个大于 0 的整数的时候,PHP 脚本能输出。

0 commit comments

Comments
 (0)