Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
记录nginx';s Lua模块_Nginx_Lua - Fatal编程技术网

记录nginx';s Lua模块

记录nginx';s Lua模块,nginx,lua,Nginx,Lua,我试图使用nginx中的Lua模块在请求体中设置一个基于JSON的变量(“foo”)。然后我想将该变量的值记录到访问日志中 像这样: http { log_format mylogfmt '$remote_addr - $remote_user [$time_local] \ "$request" $status $body_bytes_sent "$http_referer" \ "$http_user_agent" "$foo"' } locatio

我试图使用nginx中的Lua模块在请求体中设置一个基于JSON的变量(“foo”)。然后我想将该变量的值记录到访问日志中

像这样:

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;
}
但是,nginx不会使用此配置启动。它抱怨道:

danslimmon@whatever:~$ sudo /etc/init.d/nginx reload
Reloading nginx configuration: nginx: [emerg] unknown "foo" variable
nginx: configuration file /etc/nginx/nginx.conf test failed
我尝试在位置中添加一个“set$foo”-”,但这似乎覆盖了我在Lua中所做的工作

想法


您需要先定义变量$foo,然后Lua模块才能使用它。使用前请检查。

如果上面的链接不再可用,请执行以下操作:

  server {
    (...)
    map $host $foo {
        default '';
    }
    (rest of your code)
  }

这是因为您不能在服务器块内使用
set
,这是为所有vhost定义变量的最佳方法
geoip
也可能是一个不错的选择

nginx说您没有声明变量
foo
。在调用
rewrite\u by\u lua
之前,您应该设置
$foo
,这是正确的。为什么您认为它会覆盖您在Lua中所做的工作?这是链接到外部资源作为答案的问题。。。现在这个页面已经不存在了