openresty替换掉nginx并打印response返回内容

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;

读者配置时,以实际路径为准。

  1. 重启openresty,验证其代理是否成功
openresty -s reload
# 或者 /usr/local/openresty/nginx/sbin/nginx -s reload

4.添加日志配置,打印response body

  1. 编辑/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;
  1. 在需要监听的配置文件中(如:/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的内容。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335