nginx一定要启用lua,可以选择OpenResty内置lua

nginx配置vhost

 #WEBSOCKET-SUPPORT START
    proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
    #WEBSOCKET-SUPPORT END

    #PROXY-CONF-START
    location ^~ / {
      
      
      proxy_pass https://www.google.cn;
      proxy_set_header Host www.google.cn;
      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;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header REMOTE-HOST $remote_addr;
      proxy_ssl_server_name on;
      proxy_connect_timeout 60s;
      proxy_send_timeout 600s;
      proxy_read_timeout 600s;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      # 关键核心1
      proxy_set_header Accept-Encoding "";  # 禁用后端 gzip
      # 关键核心0
      body_filter_by_lua_file /www/wwwroot/inject_scripts.lua;
      proxy_buffering off;
    }

# 关键核心2 需要引入的样式和js
location /custom.css {
            alias /www/wwwroot/custom.css;
            expires 30d;
}
location /sicau-custom.js {
            alias /www/wwwroot/custom.js;
            expires 30d;
}

lua内容 /www/wwwroot/inject_scripts.lua

ngx.log(ngx.ERR, "[LUA DEBUG] Script loaded! Request URI: ", ngx.var.request_uri)
-- 获取请求的URI
local request_uri = ngx.var.request_uri
ngx.log(ngx.INFO, "Request URI: ", request_uri)

-- 检查请求路径是否匹配目标路径
local target_path = '/wiki/path'
if string.sub(request_uri, 1, string.len(target_path)) == target_path then
    ngx.log(ngx.INFO, "Matched target path: ", target_path)
    
    local chunk, eof = ngx.arg[1], ngx.arg[2]
    local buffered = ngx.ctx.buffered or {}
    
    -- 缓冲分块数据
    if not eof then
        if chunk then
            ngx.log(ngx.INFO, "Buffering chunk (size: ", #chunk, ")")
            table.insert(buffered, chunk)
            ngx.ctx.buffered = buffered
        else
            ngx.log(ngx.INFO, "Received empty chunk")
        end
        return
    end
    
    -- 处理完整的响应体
    if #buffered > 0 then
        local res_body = table.concat(buffered)
        ngx.log(ngx.INFO, "Complete response body received (size: ", #res_body, ")")
        
        -- 检查是否为HTML内容
        local content_type = ngx.header["Content-Type"] or ""
        ngx.log(ngx.INFO, "Content-Type: ", content_type)
        
        if not content_type:match("text/html") then
            ngx.log(ngx.INFO, "Not HTML content, skipping injection")
            ngx.arg[1] = res_body
            return
        end
        
        -- 定义要注入的资源
        local css_link = '<link rel="stylesheet" href="/custom.css">'
        local js_script = '<script src="/custom.js"></script>'
        local injection = css_link .. js_script
        
        ngx.log(ngx.INFO, "Attempting to inject: ", injection)
        
        -- 在</html>前插入资源
        local new_body, n, err = res_body:gsub('</html>', injection .. '</html>')
        
        if not new_body then
            ngx.log(ngx.ERR, "gsub failed: ", err)
            ngx.arg[1] = res_body
            return
        end
        
        if n > 0 then
            ngx.log(ngx.INFO, "Injection successful (", n, " replacements)")
        else
            ngx.log(ngx.WARN, "No </html> tag found, injection failed")
        end
        
        -- 更新响应体
        ngx.arg[1] = new_body
    else
        ngx.log(ngx.WARN, "Empty buffered content")
    end
else
    ngx.log(ngx.INFO, "Request path does not match target")
end