Nginx 'if' 语句在 http 上下文中吗?

Nginx 'if' 语句在 http 上下文中吗?

我想在 nginx 的 http 上下文中设置一个变量,以便它适用于我的所有服务器。但是,“if”仅在服务器和位置中受支持。

如何在 http 上下文中设置变量以使其影响所有服务器?

lua 模块是否能够帮助解决此问题(尽管我更愿意使用纯 nginx 解决方案)。如果可以,请提供示例。

我只想设置以下变量以使其适用于所有服务器:

# only allow gzip encoding. For all other cases, use identity (uncompressed)
if ($http_accept_encoding ~* "gzip") {
    set $sanitised_accept_encoding "gzip";
}

if ($http_accept_encoding != "gzip") {
    set $sanitised_accept_encoding "";
}

答案1

使用地图模块: http://nginx.org/en/docs/http/ngx_http_map_module.html

map $http_accept_encoding $sanitised_accept_encoding {
    ~*gzip                gzip;
}

相关内容