label: 用于动态配置的全局键值标签
安装
您可以在任何基于 RHEL 的发行版中安装此模块,包括但不限于:
- RedHat Enterprise Linux 7、8、9 和 10
- CentOS 7、8、9
- AlmaLinux 8、9
- Rocky Linux 8、9
- Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-label
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-label
通过在 /etc/nginx/nginx.conf 的顶部添加以下内容来启用该模块:
load_module modules/ngx_http_label_module.so;
本文档描述了 nginx-module-label v0.1.0,发布于 2026 年 1 月 6 日。
概述
http {
label environment production;
label cluster_id my_cluster_id;
label server_region us-east-1;
label server_id my_server_id;
label ...
server {
listen 80;
server_name example.com;
location / {
add_header Server-Id $label_server_id;
add_header Cluster-Id $label_cluster_id;
add_header All-Labels $labels;
return 204;
}
}
}
指令
label
语法: label key value;
默认值: 无
上下文: http
定义一个全局键值标签,可以通过变量访问。标签键仅允许使用字母、数字和 _。同一键不能重复定义。标签值不允许使用 & 和 =。
示例:
label environment production;
label region us-east-1;
labels_hash_max_size
语法: labels_hash_max_size number;
默认值: labels_hash_max_size 512;
上下文: http
设置用于存储标签的哈希表的最大大小。
labels_hash_bucket_size
语法: labels_hash_bucket_size number;
默认值: labels_hash_bucket_size 32|64|128;
上下文: http
设置标签哈希表的桶大小。默认值取决于处理器的缓存行大小。有关设置哈希表的详细信息,请参见单独的 文档。
变量
$label_name
通过其键访问特定标签的值。
$labels
以 key1=value1&key2=value2 的格式返回所有定义的标签,类似于 $args。所有标签键将以小写字母打印。
GitHub
您可以在 nginx-module-label 的 GitHub 仓库 中找到此模块的其他配置提示和文档。