2013-07-15 10 views
8

मैं अनुरोध के शरीर में JSON के आधार पर एक चर ("foo") सेट करने के लिए nginx में Lua मॉड्यूल का उपयोग करने का प्रयास कर रहा हूं। फिर मैं उस चर के मान को लॉग लॉग में लॉग करना चाहता हूं।nginx के Lua मॉड्यूल द्वारा एक चर सेट को लॉगिंग

तो जैसा

:

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 इस विन्यास के साथ शुरू नहीं करेंगे। यह thusly शिकायत:

[email protected]atever:~$ sudo /etc/init.d/nginx reload 
Reloading nginx configuration: nginx: [emerg] unknown "foo" variable 
nginx: configuration file /etc/nginx/nginx.conf test failed 

मैं एक जोड़ने की कोशिश की 'सेट $ foo "-"' स्थान पर है, लेकिन यह सिर्फ मैं लुआ में क्या कर रहा ओवरराइड करने के लिए लगता है।

विचार?

My nginx -V output

+0

nginx कह रहा है कि आपने चर 'foo' घोषित नहीं किया है। आप सही हैं कि आपको 'rewrite_by_lua' पर कॉल करने से पहले' $ foo' सेट करना चाहिए। आपको लगता है कि लुआ में आप क्या करते हैं इसे ओवरराइड करते हैं? – catwell

उत्तर

5

आप से पहले लुआ मॉड्यूल में उपयोग कर सकते चर $ foo को परिभाषित करने की जरूरत है। इसका उपयोग करने से पहले the doc for an example defining the variable within the location directive देखें।

+0

यह एक बाहरी संसाधन को उत्तर के रूप में जोड़ने की समस्या है ... अब वह पृष्ठ मौजूद नहीं है – higuita

0

तो कड़ी के रूप में इसके बाद के संस्करण अब और काम नहीं करते, ऐसा करते हैं:

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

इसका कारण यह है कि आप सर्वर ब्लॉक के अंदर set उपयोग नहीं कर सकते और यह सब vhosts चर को परिभाषित करने के लिए सबसे अच्छा तरीका है। geoip भी एक अच्छा विकल्प हो सकता है

संबंधित मुद्दे