11

私は次のことを行う方法を理解しようとしています:

  1. リクエストが来ています。

  2. HttpLuaModuleリクエストに対して何らかのアクションを実行します。リクエストが有効な場合、Lua は で処理を終了しngx.exit(202)ます。ただし、処理中に発生する可能性があり、発生するnginx可能性があり、 403 、 404 、 503 エラーを返す可能性のある条件がいくつかあります。

私がやりたいことは、200 ステータス コードを持つリクエストのみにアクセス ログに書き込むことです。基本的に私はこのようなことをしたいと思います:

location /foo {
    content_by_lua_file "/opt/nginx/lua/process.lua";
    if (status == 200) {
        access_log "/path/to/the/access_log"
    } 

私はnginxとluaの両方に非常に慣れていないので、どこに配置するか、ifステートメントを配置する場所(イーサの後content_by_lua_fileまたはサイドluaファイル内)、およびこのifステートメントがどのように見えるかを理解するのは少し難しいです。

4

4 に答える 4

30

nginx 1.7.0+ では、access_logディレクティブ自体で if 条件を使用できます。

access_log path [format [buffer=size [flush=time]] [if=condition]];

The if parameter (1.7.0) enables conditional logging.
A request will not be logged if the condition evaluates to “0” or an empty string

ディレクティブと組み合わせるとmap、さまざまな条件に基づいてログ イベントを別のログに送信できます。

http {

    map $status $normal {
        ~^2  1;
        default 0;
    }
    map $status $abnormal {
        ~^2  0;
        default 1;
    }
    map $remote_addr $islocal {
        ~^127  1;
        default 0;
    }

    server {

        access_log logs/access.log combined if=$normal;
        access_log logs/access_abnormal.log combined if=$abnormal;
        access_log logs/access_local.log combined if=$islocal;

    }  
}

http://nginx.org/en/docs/http/ngx_http_log_module.html
http://nginx.org/en/docs/http/ngx_http_map_module.html

于 2014-09-15T16:26:22.240 に答える
4

すべての質問には答えの一部があります。あなたはとても親しかった:

if ($status != "200") {
    access_log off;
}

バージョンの可用性については、こちらの情報を確認してください。 http://nginx.org/en/docs/http/ngx_http_core_module.html#変数

また、ほとんどすべてのアクセス ログ形式変数は、「最新」バージョンで利用できます: http://nginx.org/en/docs/http/ngx_http_log_module.html

于 2014-01-27T15:15:56.163 に答える
4

ngx.logandlog_by_luaディレクティブを使用してそれを行うことができます。

location /conditional_log{
        log_by_lua 'if ngx.status == 200 then ngx.log(ngx.ERR, "It is 200") end';
        content_by_lua 'ngx.say("I am ok") ngx.exit(200)';
    }

上記のコードではlog_by_lua、ログ フェーズでの実行中に呼び出される which を使用しています。その ifngx.status == 200では、ngx.log を使用して を使用してロギングをトリガーしngx.logます。

これは に書き込みerror_logます。への書き込み方法がわかりませんaccess_log

参考のため

http://wiki.nginx.org/HttpLuaModule#ngx.log

http://wiki.nginx.org/HttpLuaModule#log_by_lua

于 2013-09-26T13:40:04.800 に答える
2

これは私が思いついた解決策です:

auth.lua

-- Some logic goes here
-- ....
-- ....
ngx.var.return_status = 200

nginx.conf

http {
   lua_package_path .....;
   lua_package_cpath ....;

   rewrite_by_lua_no_postpone on;

   server {
      
     set $return_status 1;
    
     location /foo {
        rewrite_by_lua_file "<apth_to_aut.lua";

        if ($return_status = 200) {
            access_log  <path_to_access_log>  format;
            return 200;
        }
     }
   }  
}
于 2013-09-27T12:38:16.810 に答える