2016-08-02 13 views
0

私は「設定」機能のドキュメントは、このディレクティブは短い実行するように設計された」と述べので、私は、content_by_lua_block代わりのset_by_lua_blockを使用することを好む優れたライブラリにhttps://github.com/openresty/lua-nginx-modulengx_http_lua_module内のNginx fastcgi_passに渡す方法は?

を使用して、私のPHP 7.0のバックエンドにnginxの変数を渡す必要がありますコード実行中にNginxイベントループとしての高速実行コードブロックがブロックされるため、時間がかかるコードシーケンスは避ける必要があります。 " https://github.com/openresty/lua-nginx-module#set_by_lua

しかし、「コンテンツ_...」関数はノンブロッキングであることから、次のコードが時間内に戻らないと、PHPに渡されたとき$ハローが設定されていない:

location ~ \.php{ 
    set $hello ''; 

    content_by_lua_block { 
     ngx.var.hello = "hello there."; 
    } 

    fastcgi_param HELLO $hello; 
    include fastcgi_params; 
    ... 
    fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
} 

問題例えば、暗号を使用して、特定のコードパスが使用される場合、私のLuaコードは "時間がかかるコードシーケンス"になる可能性があります。

次のnginxの場所

はうまく動作しますが、set_by_lua_block()はブロッキング関数呼び出しであるためです。

location ~ \.php { 
    set $hello ''; 

    set_by_lua_block $hello { 
     return "hello there."; 
    } 

    fastcgi_param HELLO $hello; 
    include fastcgi_params; 
    ... 
    fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
} 

私の質問があり、ここでの最善のアプローチは何ですか?変数が設定された後でなければ、Nginxディレクティブfastcgi_passと関連ディレクティブをcontent_by_lua_block()内から呼び出す方法はありますか?

答えて

1

はい、ngx.location.captureで可能です。

location /lua-subrequest-fastcgi { 
     internal; # this location block can only be seen by Nginx subrequests 

     # Need to transform the %2F back into '/'. Do this with set_unescape_uri() 
     # Nginx appends '$arg_' to arguments passed to another location block. 
     set_unescape_uri $r_uri $arg_r_uri; 
     set_unescape_uri $r_hello $arg_hello; 

     fastcgi_param HELLO $r_hello; 

     try_files $r_uri =404; 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     include fastcgi_params; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     fastcgi_param SCRIPT_NAME $fastcgi_script_name; 
     fastcgi_index index.php; 
     fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    } 

あなたはthusly呼び出すことができます:別の場所のブロック、例を書く

location ~ \.php { 
     set $hello ''; 

     content_by_lua_block { 
      ngx.var.hello = "hello, friend." 

      -- Send the URI from here (index.php) through the args list to the subrequest location. 
      -- Pass it from here because the URI in that location will change to "/lua-subrequest-fastcgi" 
      local res = ngx.location.capture ("/lua-subrequest-fastcgi", {args = {hello = ngx.var.hello, r_uri = ngx.var.uri}}) 

      if res.status == ngx.HTTP_OK then 
       ngx.say(res.body) 
      else 
       ngx.say(res.status) 
      end 
     } 
    } 
+0

nginxのconfの中に新しい 'default_type'はテキスト'にアプリケーション/オクテットstream' 'から変更されます/ html'はPHPが返すHTMLを表示するためにngx.say()を使用しているためです。 – AaronDanielson

関連する問題