nginx 的 ngx_http_addition_module 模块

nginx 的ngx_http_addition_module 提供了before 以及after 的能力,可以方便进行请求的处理

参考使用

  • 环境
    docker-compose 文件
version: '3'
services:
   app:
     image: openresty/openresty:1.21.4.1-3-alpine-fat
     volumes:
     - "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
     - "./app.html:/usr/local/openresty/nginx/html/app.html"
     - "./flush.html:/usr/local/openresty/nginx/html/flush.html"
     - "./header.html:/usr/local/openresty/nginx/html/header.html"
     - "./footer.html:/usr/local/openresty/nginx/html/footer.html"
     ports:
     - "80:80"

nginx 配置

user root;  
worker_processes  1;
events {
    worker_connections  1024;
}
 
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      '$http_token'
                      '$cookie_ingress_uid';
    include       mime.types;
    default_type  application/octet-stream;
    gzip  on;
    rewrite_log on;
    log_subrequest on;
    real_ip_header     X-Forwarded-For;
    server {
       listen 81;
       charset utf-8;
       userid         on;
       userid_name    ingress_uid;
       userid_path    /;
       root html;
       userid_expires 365d;
       default_type text/html;
       location / {
            index  index.html;
       }
       location  /before_action {
          default_type text/html;
          content_by_lua_block {
            ngx.say("this is before content")
            ngx.log(ngx.ERR, "before_action")
            ngx.flush(true)
            ngx.exit(ngx.HTTP_OK)
          }
       }
       location  /after_action {
          default_type text/html;
          content_by_lua_block {
            ngx.say("this is after content")
            ngx.log(ngx.ERR, "after_action")
          }
       }
    }
    server {
       listen 80;
       charset utf-8;
       userid         on;
       userid_name    ingress_uid;
       userid_path    /;
       root html;
       userid_expires 365d;
       default_type text/html;
       location / {
            index  index.html;
       }
       location = /app.html {
          add_before_body /header.html;
          add_after_body  /footer.html;
       }
       location = /flush.html {
          # not working with request
          add_before_body /before_action;
          add_after_body  /after_action;
       }
       location  /before_action {
          default_type text/html;
          content_by_lua_block {
            ngx.say("this is before content")
            ngx.log(ngx.ERR, "before_action")
            ngx.flush(true)
            ngx.exit(ngx.HTTP_OK)
          }
       }
       location  /after_action {
         default_type text/html;
          content_by_lua_block {
             ngx.say("this is after content")
            ngx.log(ngx.ERR, "after_action")
            ngx.flush(true)
            ngx.exit(ngx.HTTP_OK)
          }
       }
       location = /footer.html {
           proxy_pass http://localhost:81;
       }
    }
}

参考调用效果

 

 


效果

 

 

  • 使用静态文件

http://localhost/app.html

 

 


效果

 

 

说明

从测试可以看出有一些问题,使用了静态页面的问题不大, 可以按照流程生成数据,但是对于基于openresty content 阶段处理的数据,会有after 实际顺序不太对的问题,具体还需要研究下,应该是在处理子请求的时候与openresty 的机制有关系

https://nginx.org/en/docs/http/ngx_http_addition_module.html

posted on   荣锋亮  阅读(146)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示