【问题标题】:How to inject custom content via Nginx?如何通过 Nginx 注入自定义内容?
【发布时间】:2013-11-11 03:00:50
【问题描述】:

有没有办法为每个服务的 php/html/etc 注入几行脚本等。页? 例如 -tag 之后的一些自定义 javascript?

我知道,你应该可以在 nginx 中使用 lua 但有更好的解决方案吗?

我在 Nginx 后面运行多个不同的 Web 应用程序,所以感觉这样做是正确的方法。我无法访问每个应用程序的源代码,维护它们会很麻烦。

【问题讨论】:

    标签: nginx code-injection


    【解决方案1】:

    我找到了方法:http://nginx.org/en/docs/http/ngx_http_sub_module.html

    location / {
        sub_filter </head>
            '</head><script language="javascript" src="$script"></script>';
        sub_filter_once on;
    }
    

    【讨论】:

    • IndexHeadInsert 用于 Apache 我正在寻找 NGINX 中的类似命令
    • 快速注意,这个模块不是默认构建的(如链接文章所述),因此您的安装可能没有它。你可以看看你是否拥有它:nginx -V,它“将列出所有配置的模块”(Source)并寻找--with-http_sub_module
    • 如果内容是gzipped,替换将不起作用。为此,添加proxy_set_header Accept-Encoding "";,它告诉后端在响应中不允许压缩。
    • 但是如果我想在head开头注入basehead可以包含一些我事先不知道的属性?
    猜你喜欢
    • 1970-01-01
    • 2022-12-14
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多