1. 軟件版本
- 系統(tǒng)
centos6.7X86_64
- nginx
1.11.5
- lua-nginx-module
0.10.7
- PHP
5.6.27
2. 環(huán)境準備
配置yum倉庫
wget -O /etc/yum.repos.d/CentOS-Base.repo[https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/centos?codeblock=2](https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/centos?codeblock=2)
wget -O /etc/yum.repos.d/epel.repo[https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/epel?codeblock=0](https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/epel?codeblock=0)
wget -O /etc/yum.repos.d/epel-testing.repo[https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/epel?codeblock=1](https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/epel?codeblock=1)
/usr/sbin/ntpdate asia.pool.ntp.org
安裝編譯所需的依賴
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
3. 軟件安裝
安裝php5.6
rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
yum -y install php56w-cli php56w-mysql php56w-xml php56w-mbstring php56w-pdo php56w-bcmath php56w-mcrypt php56w-fpm
編譯安裝nginx
cd /opt/software/
wget https://codeload.github.com/openresty/lua-nginx-module/tar.gz/v0.10.7
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
wget https://nginx.org/download/nginx-1.11.5.tar.gz
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx
tar zxf lua-nginx-module-0.10.7.tar.gz
tar zxf LuaJIT-2.0.4.tar.gz
tar zxf nginx-1.11.5.tar.gz
cd LuaJIT-2.0.4/
make && make install
cat >> /etc/profile <<EOF
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
EOF
source /etc/profile
cd ../nginx-1.11.5/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --add-module=../lua-nginx-module-0.10.7
make -j2
make install
4. 配置lua腳本
在nginx配置文件中添加下列l(wèi)ocation
# /usr/local/nginx/conf/nginx.conf
location ~* ^/lua(/.*) {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
啟動nginx
/usr/local/nginx/sbin/nginx
測試lua腳本執(zhí)行
curl http://127.0.0.1/lua/
返回 hello, lua 為正常
5. 配置php
啟用如下選項
#/usr/local/nginx/conf/nginx.conf
location / {
root /web;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /web/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /$document_root$fastcgi_script_name;
include fastcgi_params;
}
在/web/目錄下 新建index.php的測試頁面吟宦,測試php是否能正常工作
mkdir /web/
cat > /web/info.php << EOF
<?php
phpinfo();
?>
EOF
chown nginx.nginx -R /web/
啟動php-fpm和nginx
/etc/init.d/php-fpm start
/usr/local/nginx/sbin/nginx -s reload
測試
curl http://127.0.0.1/info.php
顯示phpinfo頁面,極為正常
6. 配置日志,記錄post請求的request_body 和response_body
使用下列配置
# /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format postdata '$remote_addr | $request_body | $resp_body';
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
charset utf-8;
set $resp_body "";
location / {
root /web/;
index index.php index.html index.htm;
}
location ~* ^/lua(/.*) {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
location ~ \.php$ {
root /web/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /$document_root$fastcgi_script_name;
include fastcgi_params;
access_log /tmp/nginx_access.log postdata;
lua_need_request_body on;
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or"") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
}
}
}
創(chuàng)建php文件艾恼,用來接收post請求,并返回數據
cat >> /web/index.php <<EOF
<?php
header("Content-type:text/html;charset=utf-8");
print_r(file_get_contents('php://input'));
?>
EOF
chown nginx.nginx /web/index.php
重啟nginx
/usr/local/nginx/sbin/nginx -s reload
使用postman工具來發(fā)送post請求
Paste_Image.png
查看日志
cat /tmp/nginx_access.log
Paste_Image.png
已經記錄post請求的request 和response數據了冒掌。
第二行是因為post數據有中文字符血巍,所以變成了16進制。解決方法見 解決nginx在記錄post數據時 中文字符轉成16進制的問題