json-var: NGINX JSON-Variablenmodul
Installation
Sie können dieses Modul in jeder RHEL-basierten Distribution installieren, einschließlich, aber nicht beschränkt auf:
- RedHat Enterprise Linux 7, 8, 9 und 10
- CentOS 7, 8, 9
- AlmaLinux 8, 9
- Rocky Linux 8, 9
- Amazon Linux 2 und Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-json-var
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-json-var
Aktivieren Sie das Modul, indem Sie Folgendes an den Anfang von /etc/nginx/nginx.conf hinzufügen:
load_module modules/ngx_http_json_var_module.so;
Dieses Dokument beschreibt nginx-module-json-var v1.1, veröffentlicht am 11. Februar 2022.
json_var
- syntax:
json_var $variable { ... } - default:
none - context:
http
Erstellt eine neue Variable, deren Wert ein JSON ist, das die innerhalb des Blocks aufgelisteten Elemente enthält.
Parameter innerhalb des json_var-Blocks geben ein Feld an, das im resultierenden JSON enthalten sein soll.
Jeder Parameter muss zwei Argumente enthalten - Schlüssel und Wert.
Der Wert kann NGINX-Variablen enthalten.
Beispielkonfiguration
http {
json_var $output {
timestamp $time_local;
remoteAddr $remote_addr;
xForwardedFor $http_x_forwarded_for;
userAgent $http_user_agent;
params $args;
}
server {
location /get_json/ {
return 200 $output;
}
}
http://domain/get_json/?key1=value1&key2=value2 kann ein JSON wie folgt zurückgeben:
{
"timestamp": "21/Jul/2017:12:44:18 -0400",
"remoteAddr": "127.0.0.1",
"xForwardedFor": "",
"userAgent": "curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3",
"params": "key1=value1&key2=value2"
}
GitHub
Sie finden zusätzliche Konfigurationstipps und Dokumentation für dieses Modul im GitHub-Repository für nginx-module-json-var.