2016-08-02 5 views
0

은 내가 '설정'기능에 대한 설명서가 나와 있기 때문에 "이 지시문은 짧은 실행하도록 설계되어, https://github.com/openresty/lua-nginx-modulengx_http_lua_module에서 Nginx fastcgi_pass로 전달하는 방법은 무엇입니까?

내가 대신 set_by_lua_blockcontent_by_lua_block를 사용하는 것을 선호 우수한 라이브러리를 사용하여 내 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; 
} 

문제 예를 들어 암호를 사용하여 특정 코드 경로를 사용하는 경우 내 루아 코드가 "시간 소모적 인 코드 시퀀스"가 될 수 있습니다.

다음 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'은 ngx.say()를 사용하여 PHP가 반환 한 HTML을 표시하기 때문입니다. – AaronDanielson