我有 nginx 作为反向代理服务器,用于在各种应用程序服务器上运行的多个应用程序。我想将特定的 html/脚本引入到设置预注册 url 的响应内容中。这些 URL 可能属于 nginx 之后的任何应用程序。为此,我对所有 url 使用 sub_filter,它工作正常。但是当我尝试将“sub_filter”放在“IF”块中时,它现在是允许的。
我需要像下面这样的东西。
resolver 8.8.8.8;
proxy_pass http://www.example.com;
proxy_set_header Accept-Encoding *;
gunzip on;
if ( $induceScript = 1) {
sub_filter "</body>" "<div class='induced' <font color=red size=8>Induced Text </font></div></body>";
sub_filter_types *;
sub_filter_once off;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试重新启动 nginx 时显示以下错误消息。
nginx: [emerg] "sub_filter" directive is not allowed here in /etc/openresty/openresty.conf
从此,如果我正确理解“IF”指令,则不允许在其中使用“sub_fiter”。有什么具体原因吗?还提供正确的方法/替代方法来解决这个问题。
出现错误是因为sub_filter
除以下指令之外的任何其他指令中都不允许该指令:http, server, location
取自http://nginx.org/en/docs/http/ngx_http_sub_module.html#sub_filter
server {
listen 82;
listen 83;
server_name example.com;
root /var/www/9000;
try_files $uri $uri/index.html;
sub_filter "</body>" $conditional_filter;
sub_filter_types *;
sub_filter_once off;
}
map $server_port $conditional_filter {
82 "<br>Added content</body>";
default "</body>";
}
Run Code Online (Sandbox Code Playgroud)
您需要将 替换if
为map
指令。
在此示例中,我根据服务器/网站的端口添加其他内容。
http://example.com:82
产生的内容类似于<body>abc</body>
wherehttp://example.com:83
产生以下内容<body>abc<br>Added content</body>
。
您$server_port
可以使用自己的变量$induceScript
或不同的条件。
归档时间: |
|
查看次数: |
3125 次 |
最近记录: |