2017-09-11 12 views
0

私の問題

私はopenrestyを使って簡単なサーバーを構築しています。Openresty:luaでhttp呼び出しを行い、その解析結果を返します

このサーバーを呼び出すと、別のサーバーに別の呼び出しを行い、JSON結果をフェッチして処理し、解析結果を返します。

サーバーは、この質問の範囲外の理由でopenrestyで実装する必要があります。


コード

error_log /dev/stdout info; 

events { 
    worker_connections 14096; 
} 

http { 
    access_log off; 
    lua_package_path ";;/usr/local/openresty/nginx/?.lua;"; 

    server { 
     keepalive_requests 100000; 
     proxy_http_version 1.1; 
     keepalive_timeout 10; 

     location/{ 
     content_by_lua_block { 
       res = ngx.location.capture('http://localhost:8080/functions.json') 
       ngx.say(res.body) 
      } 
     } 

     location /functions { 
      root /usr/local/openresty/nginx/html/; 
     } 

     listen 0.0.0.0:80 default_server; 
    } 
} 

エラーログ

2017/09/11 08:27:49 [error] 7#7: *1 open() "/usr/local/openresty/nginx/htmlhttp://localhost:8080/functions.json" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET/HTTP/1.1", subrequest: "http://localhost:8080/functions.json", host: "localhost:8080"

私の質問私はnginx openrestyでLuaのコンテンツブロック内からのHTTP GETリクエストを作成するにはどうすればよい

答えて

1

キャプチャは、あなたが絶対URL lua-resty-httpパッケージを使用して解決

error_log /dev/stdout info; 

events { 
    worker_connections 14096; 
} 

http { 
    access_log off; 
    lua_package_path ";;/usr/local/openresty/nginx/?.lua;"; 

    server { 
     keepalive_requests 100000; 
     proxy_http_version 1.1; 
     keepalive_timeout 10; 

     location/{ 
     content_by_lua_block { 
       res = ngx.location.capture('/functions.json') 
       ngx.say(res.body) 
      } 
     } 
     location /functions.json { 
      proxy_pass http://localhost:8080/functions.json; 
     } 

     location /functions { 
      root /usr/local/openresty/nginx/html/; 
     } 

     listen 0.0.0.0:80 default_server; 
    } 
} 
+0

私のコードで 'capture'を試みましたが、ローカルファイル(ログ参照)として扱われました。 –

+0

これまでまたは今?私の代わりに 'location = /functions.json {'を使用してください。 –

+0

ありがとう、私はサードパーティのhttpライブラリを使って解決しました:https://github.com/pintsized/lua-resty-http –

0

内部nginxの位置を捕捉していないことができます。ライブラリをnginx openrestyのルートにコピーしました:

local http = require "resty.http" 
local httpc = http.new() 

local res, err = httpc:request_uri("http://127.0.0.1/functions.json", { method = "GET" }) 
// Use res.body to access the response 
関連する問題