Ich versuche, das Lua-Modul in nginx zu verwenden, um eine Variable ("foo") basierend auf JSON im Hauptteil einer Anfrage festzulegen. Dann möchte ich den Wert dieser Variablen im Zugriffsprotokoll protokollieren.Eine durch das Lua-Modul von nginx gesetzte Variable protokollieren
Wie so:
http {
log_format mylogfmt '$remote_addr - $remote_user [$time_local] \
"$request" $status $body_bytes_sent "$http_referer" \
"$http_user_agent" "$foo"'
}
location/{
proxy_pass http://remote-server.example.com/;
proxy_set_header X-Real-IP $remote_addr;
proxy_connect_timeout 150;
proxy_send_timeout 100;
proxy_read_timeout 100;
proxy_buffers 4 32k;
client_max_body_size 8m;
client_body_buffer_size 128k;
rewrite_by_lua '
cjson = require "cjson"
ngx.req.read_body()
body_table = cjson.decode(ngx.var.request_body)
ngx.var.foo = body_table["foo"]
';
access_log /var/log/nginx/access.log mylogfmt;
}
jedoch nginx nicht mit dieser Konfiguration starten. Es beklagt sich so:
[email protected]:~$ sudo /etc/init.d/nginx reload
Reloading nginx configuration: nginx: [emerg] unknown "foo" variable
nginx: configuration file /etc/nginx/nginx.conf test failed
Ich versuchte, ein Hinzufügen von ‚set $ foo‚-‘‘ an den Ort, aber das scheint nur außer Kraft zu setzen, was ich in Lua mache.
Gedanken?
Nginx sagt, dass Sie die Variable 'foo' nicht deklariert haben. Sie haben recht, dass Sie '$ foo 'setzen sollten, bevor Sie' rewrite_by_lua' aufrufen. Warum denkst du, dass es außer Kraft setzt, was du in Lua machst? – catwell