首页 > 系统相关 >nginx 的 ngx_http_addition_module 模块

nginx 的 ngx_http_addition_module 模块

时间:2022-10-07 11:36:53浏览次数:78  
标签:http addition after module nginx html ngx before

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;
}
}
}

参考调用效果

nginx 的 ngx_http_addition_module 模块_nginx

 

 

效果

nginx 的 ngx_http_addition_module 模块_nginx_02

 

 

  • 使用静态文件

​http://localhost/app.html​

nginx 的 ngx_http_addition_module 模块_html_03

 

 

效果

nginx 的 ngx_http_addition_module 模块_nginx_04

 

 

说明

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

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

标签:http,addition,after,module,nginx,html,ngx,before
From: https://blog.51cto.com/rongfengliang/5734332

相关文章