Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于通过lua nginx的另一个内部请求的结果的代理传递请求_Nginx_Lua_Openresty - Fatal编程技术网

基于通过lua nginx的另一个内部请求的结果的代理传递请求

基于通过lua nginx的另一个内部请求的结果的代理传递请求,nginx,lua,openresty,Nginx,Lua,Openresty,我想根据内部服务的结果代理您传递请求或返回响应。 为了达到这个目的,我使用了docker image 这是我的nginx.conf: # nginx.vh.default.conf -- docker-openresty # # This file is installed to: # `/etc/nginx/conf.d/default.conf` # # It tracks the `server` section of the upstream OpenResty's `nginx

我想根据内部服务的结果代理您传递请求或返回响应。 为了达到这个目的,我使用了docker image

这是我的
nginx.conf

# nginx.vh.default.conf  --  docker-openresty
#
# This file is installed to:
#   `/etc/nginx/conf.d/default.conf`
#
# It tracks the `server` section of the upstream OpenResty's `nginx.conf`.
#
# This config (and any other configs in `etc/nginx/conf.d/`) is loaded by
# default by the `include` directive in `/usr/local/openresty/nginx/conf/nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
#
# load_module /usr/lib/nginx/modules/ndk_http_module.so;
# load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;
lua_package_path '/usr/local/nginx/includes.d/?.lua;/home/user/lua/?.lua;/home/lua/?.lua;;';

error_log  logs/error.log  info;
error_log  logs/error.log  debug;

upstream flask_webservice {
    server challenge:5000;
}

server {
    listen 80;
    server_name  localhost;
    lua_need_request_body on;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        # default_type text/plain;
        # proxy_pass http://challenge:5000/;
        content_by_lua_file /home/user/lua/proxy_middleware.lua;
        # content_by_lua_block {}
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/openresty/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           /usr/local/openresty/nginx/html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
这里是代理中间件。lua:

localhttp=require“resty.http”
本地cjson=需要“cjson”
本地httpc=http.new()
本地质询_url=“http://”。。ngx.var.flask\u webservice。。ngx.var.uri
本地请求_头=ngx.req.get_头()
本地请求_方法=ngx.req.get_方法()
本地请求\u body=ngx.req.get\u body\u data()
如果请求_body==nil,则
请求_body={}
结束
如果请求_头==nil,则
请求头={}
结束
日志(ngx.DEBUG,challenge_url)
日志(ngx.DEBUG,“请求\u头:“…cjson.encode(请求\u头))
log(ngx.DEBUG,“请求方法:“…请求方法”)
日志(ngx.DEBUG,“请求\正文:“…cjson.encode(请求\正文))
本地res,err=httpc:request\u uri(challenge\u url{
方法=请求方法,
headers=请求头,
body=请求\正文
})
log(ngx.DEBUG,“响应为:…cjson.encode(res))
log(ngx.DEBUG,“错误是:“…cjson.encode(err))
以下是我得到的错误:

2020/12/24 19:17:52 [error] 7#7: *3 lua entry thread aborted: runtime error: /home/user/lua/proxy_middleware.lua:6: attempt to concatenate field 'flask_webservice' (a nil value)
这是正在进行的项目

我有两个问题:

  • 有没有其他方法可以达到这个目的
  • 如何在
    Lua
    code中访问上游服务器

  • 我通过管理第一个请求,然后使用
    ngx.location.capture
    根据第一个请求的响应结果代理传递请求

    这里是lua\u中间件。lua:

    localhttp=require“resty.http”
    本地cjson=需要“cjson”
    本地httpc=http.new()
    本地质询_url=”http://challenge:5000" .. ngx.var.uri
    本地请求_头=ngx.req.get_头()
    本地请求_方法=ngx.req.get_方法()
    本地请求\u body=ngx.req.get\u body\u data()
    如果请求_body==nil,则
    请求_body={}
    结束
    如果请求_头==nil,则
    请求头={}
    结束
    日志(ngx.DEBUG,challenge_url)
    日志(ngx.DEBUG,“请求\u头:“…cjson.encode(请求\u头))
    log(ngx.DEBUG,“请求方法:“…请求方法”)
    日志(ngx.DEBUG,“请求\正文:“…cjson.encode(请求\正文))
    本地res,err=httpc:request\u uri(challenge\u url{
    方法=请求方法,
    headers=请求头,
    body=请求\正文
    })
    局部响应_body=res.body
    本地响应头=res.headers
    本地响应状态=恢复状态
    日志(ngx.DEBUG,“response\u body:…cjson.encode(response\u body))
    ngx.log(ngx.DEBUG,“响应头:“…cjson.encode(响应头))
    ngx.log(ngx.DEBUG,“响应\状态:…cjson.encode(响应\状态))
    日志(ngx.DEBUG,“错误:…cjson.encode(err))
    日志(ngx.DEBUG,“记录器:…响应_状态==200)
    日志(ngx.DEBUG,“状态类型:”…类型(响应状态))
    如果响应状态=200,则
    日志(ngx.DEBUG,“重定向…”)
    本地重定向\u res=ngx.location.capture(“/redirect\u to”{
    方法=ngx.HTTP_POST,
    始终\u向前\u正文=真
    })
    ngx.log(ngx.DEBUG,“重定向res body:…cjson.encode(重定向res.body))
    ngx.log(ngx.DEBUG,“重定向资源状态:”…cjson.encode(重定向资源状态))
    ngx.status=已重定向\u res.status
    ngx.say(重定向的\u res.body)
    退出(ngx.HTTP_OK)
    其他的
    ngx.status=响应状态
    ngx.say(响应体)
    退出(ngx.HTTP_OK)
    结束
    
    这是我的
    nginx.conf

    # nginx.vh.default.conf  --  docker-openresty
    #
    # This file is installed to:
    #   `/etc/nginx/conf.d/default.conf`
    #
    # It tracks the `server` section of the upstream OpenResty's `nginx.conf`.
    #
    # This config (and any other configs in `etc/nginx/conf.d/`) is loaded by
    # default by the `include` directive in `/usr/local/openresty/nginx/conf/nginx.conf`.
    #
    # See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
    #
    # load_module /usr/lib/nginx/modules/ndk_http_module.so;
    # load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;
    lua_package_path '/usr/local/nginx/includes.d/?.lua;/home/user/lua/?.lua;/home/lua/?.lua;;';
    
    error_log  logs/error.log  info;
    error_log  logs/error.log  debug;
    
    upstream flask_webservice {
        server challenge:5000;
    }
    
    server {
        listen 80;
        server_name  localhost;
        lua_need_request_body on;
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            # default_type text/plain;
            # proxy_pass http://challenge:5000/;
            content_by_lua_file /home/user/lua/proxy_middleware.lua;
            # content_by_lua_block {}
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/openresty/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           /usr/local/openresty/nginx/html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    
    #nginx.vh.default.conf——docker openresty
    #
    #此文件安装到:
    #`/etc/nginx/conf.d/default.conf`
    #
    #它跟踪上游OpenResty的“nginx.conf”的“server”部分。
    #
    #此配置(以及`etc/nginx/conf.d/`中的任何其他配置)由加载
    #默认情况下,`/usr/local/openresty/nginx/conf/nginx.conf`中的`include`指令。
    #
    #看https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-配置文件
    #
    #load_module/usr/lib/nginx/modules/ndk_http_module.so;
    #load_module/usr/lib/nginx/modules/ngx_http_lua_module.so;
    lua\u包路径'/usr/local/nginx/includes.d/?.lua/主页/用户/lua/?.lua/主页/lua/?.lua;;';
    错误日志/error.log信息;
    错误日志/error.log调试;
    上游网络服务{
    服务器挑战:5000;
    }
    服务器{
    听80;
    服务器名称localhost;
    分解器127.0.0.11;
    lua_需要______________________________;
    #charset-koi8-r;
    #access_log/var/log/nginx/host.access.log main;
    地点/{
    客户机最大机身尺寸50k;
    客户机\主体\缓冲区\大小50k;
    #默认类型为文本/纯文本;
    #代理通行证http://challenge:5000/;
    content_by_lua_file/home/user/lua/proxy_middleware.lua;
    }
    位置/重定向到{
    代理通行证http://challenge:5000/arvan;
    }
    #错误\u第404/404.html页;
    #将服务器错误页面重定向到静态页面/50x.html
    #
    错误\u第500页502 503 504/50x.html;
    location=/50x.html{
    root/usr/local/openresty/nginx/html;
    }
    #将PHP脚本代理到Apache,并在127.0.0.1:80上侦听
    #
    #位置~\.php${
    #代理通行证http://127.0.0.1;
    #}
    #将PHP脚本传递给FastCGI服务器,并在127.0.0.1:9000上侦听
    #
    #位置~\.php${
    #root/usr/local/openresty/nginx/html;
    #fastcgi_pass 127.0.0.1:9000;
    #fastcgi_index.php;
    #fastcgi_参数SCRIPT_FILENAME/scripts$fastcgi_SCRIPT_name;
    #包括fastcgi_参数;
    #}
    #拒绝访问.htaccess文件,如果Apache的文档根
    #同意nginx的观点
    #
    #位置~/\.ht{
    #否认一切;
    #}
    }
    
    这里是