0.备份nginx的配置文件
备份 /etc/nginx/nginx.conf 和 /etc/nginx/conf.d/
例如:
cp /etc/nginx/nginx.conf /home/temp/
cp -R /etc/nginx/conf.d/ /home/temp/
如果有其他路径的配置文件,也需要备份,一切以实际为准!
1.卸载nginx
systemctl disable nginx.service
rm -rf /usr/sbin/nginx
rm -rf /etc/nginx/
yum remove nginx
2.安装openresty
参考 https://openresty.org/cn/
cd /home/temp/
wget https://openresty.org/package/centos/openresty.repo
mv openresty.repo /etc/yum.repos.d/
yum install -y openresty
# 启动openresty里的nginx
openresty
#或者 /usr/local/openresty/nginx/sbin/nginx
2.1 也可通过压缩包安装
wget https://openresty.org/download/openresty-1.19.3.1.tar.gz
tar xzvf openresty-1.19.3.1.tar.gz
cd openresty-1.19.3.1/
./configure
make
make install
# better also add the following line to your ~/.bashrc or ~/.bash_profile file.
export PATH=/usr/local/openresty/bin:$PATH
默认情况下程序会被安装到 /usr/local/openresty 目录,你可以使用 ./configure --help 查看更多的配置选项。
访问 http://ip 能看到“Welcome to OpenResty!”的界面说明openresty安装并启动成功,如下:
image.png
3.还原nginx配置文件到openresty
1.将 /etc/nginx/nginx.conf 和 /etc/nginx/conf.d 复制到 openresty 的配置文件存放路径 /usr/local/openresty/nginx/conf/ 下。
cp /home/temp/nginx.conf /usr/local/openresty/nginx/conf/
cp -R /home/temp/conf.d/ /usr/local/openresty/nginx/conf/
注意:
nginx默认安装的地址是: /etc/nginx/
openresty默认安装的地址是: /usr/local/openresty/
openresty下的nginx配置路径是:/usr/local/openresty/nginx/conf/
所以需要将 /usr/local/openresty/nginx/conf/nginx.conf 文件中的include配置更改为实际路径:
# 原配置内容
include /etc/nginx/conf.d/*.conf;
# 更改后的配置内容
include /usr/local/openresty/nginx/conf/conf.d/*.conf;
读者配置时,以实际路径为准。
- 重启openresty,验证其代理是否成功
openresty -s reload
# 或者 /usr/local/openresty/nginx/sbin/nginx -s reload
4.添加日志配置,打印response body
- 编辑/usr/local/openresty/nginx/conf/nginx.conf文件,将log_format更改,
变更前:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
变更后:
log_format main escape=json '{ "@timestamp": "$time_local", '
'"remote_addr": "$remote_addr", '
'"upstream_addr": "$upstream_addr",'
'"remote_user": "$remote_user", '
'"body_bytes_sent": "$body_bytes_sent", '
'"request_time": "$request_time", '
'"status": "$status", '
'"request": "$request", '
'"request_method": "$request_method", '
'"http_referrer": "$http_referer", '
'"body_bytes_sent":"$body_bytes_sent", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"remote_addr":""$remote_addr",'
'"http_user_agent": "$http_user_agent",'
'"http_uri": "$uri",'
'"http_host":"$http_host",'
'"resp_body":"$resp_body" }';
access_log logs/access.log main;
- 在需要监听的配置文件中(如:/usr/local/openresty/nginx/conf/nginx.conf/default.conf),添加如下内容
lua_need_request_body on;
set $resp_body "";
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
';
示例:
server {
listen 80;
server_name localhost;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 200m;
lua_need_request_body on;
set $resp_body "";
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
';
# 根路径
location / {
root /home/temp/mdplus;
index index.html index.htm;
}
}
5.重启及验证
openresty -s reload
# 或者 /usr/local/openresty/nginx/sbin/nginx -s reload
tail -f /usr/local/openresty/nginx/logs/access.log
然后发起请求,查看/usr/local/openresty/nginx/logs/access.log是否打印response body的内容。