网友回复
可以使用ngx_http_addition_module模块,必须在编译的时候添加过--with-http_addition_module
location / { add_before_body /hello.html add_after_body /after.html; }
注意,hello.html与after.html可以写一些添加到html前面和后面的代码,例如hello.html中代码为hello,after.html代码为after,
那么请求一个html文件demo.html,里面包含代码demo,那么实际返回的代码就是hello demo after
可以使用ngx_http_sub_module模块进行html代码替换,例如下面就是在返回的html页面注入插入了一段js代码
location / { sub_filter <head> '<head><script>window[\'adrum-start-time\'] = new Date().getTime();</script><script src="/adrum.js"></script>'; sub_filter_once on; }还可以按照标签进行替换;
location / { sub_filter '<a href="http://127.0.0.1:8080/' '<a href="https://$host/'; sub_filter '<img src="http://127.0.0.1:8080/' '<img src="https://$host/'; sub_filter_once on; }