nginx lua入口线程已中止:运行时错误:用lua重写

jucafojl  于 2023-01-25  发布在  Nginx
关注(0)|答案(1)|浏览(482)

nginx.conf文件如下所示:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;

events {
    worker_connections  1024;
}

http {
proxy_ssl_session_reuse off;

proxy_buffering on; 
proxy_buffer_size 4k;
proxy_buffers 32 4k; 
proxy_max_temp_file_size 0;

proxy_read_timeout 3600;
keepalive_timeout 10m;
proxy_connect_timeout  600s;
fastcgi_send_timeout 600s;
send_timeout 600;
fastcgi_read_timeout 600s;

lua_package_path "./lua/?.lua;;";
#    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

log_format main2 '$remote_addr - $remote_user [$time_local] '
    '\n\n"$request" \n status: $status body_bytes_sent: $body_bytes_sent '
    '\n\n http_referer:  "$http_referer" \n "$http_user_agent" $request_time '
    '\n\n req_headers: \n "$req_headers" \n\n req_body: \n "$req_body" \n\n resp_body: \n "$resp_body"';

    access_log  /var/log/nginx/access.log  main2;

    sendfile        on;
    #tcp_nopush     on;

#    keepalive_timeout  65;

    #gzip  on;

#    include /etc/nginx/conf.d/*.conf;
server {

        server_name xxxxxxxxxxxxx.com;

        listen 80;

  lua_need_request_body on;

        
        set $resp_body "";
        set $req_body "";
        set $req_headers "";

        client_body_buffer_size 16k;
        client_max_body_size 16k;

        rewrite_by_lua_block {
            local req_headers = "Headers: ";
            ngx.var.req_body = ngx.req.get_body_data();
            local h, err = ngx.req.get_headers()
            for k, v in pairs(h) do
                req_headers = req_headers .. k .. ": " .. v .. "\n";
            end

            ngx.var.req_headers = req_headers;
        }

        body_filter_by_lua '
        local resp_body = string.sub(ngx.arg[1], 1, 1000)
        ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
        if ngx.arg[2] then
          ngx.var.resp_body = ngx.ctx.buffered
        end
        ';

        location /  {
                proxy_pass https://xxxxxxxxxxxxx.com;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#               proxy_set_header Host $http_host;
                proxy_set_header Cookie $http_cookie;
                proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                proxy_ssl_ciphers GOST2012-GOST8912-GOST8912:HIGH:MEDIUM;
                proxy_ssl_certificate /etc/nginx/ssl/ivanovIvanIvanovich.pem;
                proxy_ssl_certificate_key /etc/nginx/ssl/prkeyunitIvanov.key;
                proxy_ssl_server_name on;
#               proxy_redirect off;
        
#               root   html;
#               index  index.html index.htm;
            }      
    }

server {

       listen 8080;


 lua_need_request_body on;
       
                set $resp_body "";
        set $req_body "";
        set $req_headers "";

        client_body_buffer_size 16k;
        client_max_body_size 16k;

        rewrite_by_lua_block {
            local req_headers = "Headers: ";
            ngx.var.req_body = ngx.req.get_body_data();
            local h, err = ngx.req.get_headers()
            for k, v in pairs(h) do
                req_headers = req_headers .. k .. ": " .. v .. "\n";
            end

            ngx.var.req_headers = req_headers;
        }

        body_filter_by_lua '
        local resp_body = string.sub(ngx.arg[1], 1, 1000)
        ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
        if ngx.arg[2] then
          ngx.var.resp_body = ngx.ctx.buffered
        end
        ';


       location /  {

        proxy_pass https://xxxxxxxxxxxxx.com;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Cookie $http_cookie;
                proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                proxy_ssl_ciphers GOST2012-GOST8912-GOST8912:HIGH:MEDIUM;
                proxy_ssl_certificate /etc/nginx/ssl/PetrovIvanSemenovich.pem;
                proxy_ssl_certificate_key /etc/nginx/ssl/prkeyunitSemenovich.key;
                proxy_ssl_server_name on;
                }

    }

server {

       listen 8585;

 lua_need_request_body on;
 
            set $resp_body "";
        set $req_body "";
        set $req_headers "";

        client_body_buffer_size 16k;
        client_max_body_size 16k;

        rewrite_by_lua_block {
            local req_headers = "Headers: ";
            ngx.var.req_body = ngx.req.get_body_data();
            local h, err = ngx.req.get_headers()
            for k, v in pairs(h) do
                req_headers = req_headers .. k .. ": " .. v .. "\n";
            end

            ngx.var.req_headers = req_headers;
        }

        body_filter_by_lua '
        local resp_body = string.sub(ngx.arg[1], 1, 1000)
        ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
        if ngx.arg[2] then
          ngx.var.resp_body = ngx.ctx.buffered
        end
        ';


       location /  {

        proxy_pass https://xxxxxxxxxxxxx.com;
              
proxy_ssl_server_name on;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Cookie $http_cookie;
                proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                proxy_ssl_ciphers GOST2012-GOST8912-GOST8912:HIGH:MEDIUM;
                proxy_ssl_certificate /etc/nginx/ssl/Petrovich.pem;
                proxy_ssl_certificate_key /etc/nginx/ssl/prkeyunitPetrovich.key;


                }

    }

}

错误日志

2023/01/23 07:50:11 [error] 3294740#3294740: *2312 lua entry thread aborted: runtime error: rewrite_by_lua(nginx.conf:194):6: attempt to concatenate local 'v' (a table value)
stack traceback:
coroutine 0:
    rewrite_by_lua(nginx.conf:194): in function <rewrite_by_lua(nginx.conf:194):1>, client: XX.XXX.XXX.89, server: , request: "POST /INC/zkau;jsessionid=cu9a1076567akai7q9hjr4h HTTP/1.1", host: "XX.XXX.XX.XXX:8585", referrer: "http://XX.XXX.XX.XXX/INC/INC/scroller.zul?navigationId=INC_ApplRefund_D07_2"
2023/01/23 07:52:01 [error] 3294740#3294740: *2337 peer closed connection in SSL handshake (104: Connection reset by peer) while SSL handshaking to upstream, client: XX.XXX.XXX.89, server: , request: "POST /INC/zkau;jsessionid=1koc01pjtptx01dg6yskg6mndo HTTP/1.1", upstream: "https://XX.XXX.X.60:443/INC/zkau;jsessionid=1koc01pjtptx01dg6yskg6mndo", host: "XX.XXX.XX.XXX:8585", referrer: "http://XX.XXX.XX.XXX/INC/INC/scroller.zul?navigationId=D04_Client"
2023/01/23 14:28:38 [error] 3294740#3294740: *2724 lua entry thread aborted: runtime error: rewrite_by_lua(nginx.conf:194):6: attempt to concatenate local 'v' (a table value)
stack traceback:
coroutine 0:
    rewrite_by_lua(nginx.conf:194): in function <rewrite_by_lua(nginx.conf:194):1>, client: XX.XXX.XXX.89, server: , request: "POST /INC/zkau;jsessionid=1ubqzl52zhbbz1d6zpytdgsd8i HTTP/1.1", host: "XX.XXX.XX.XXX:8585", referrer: "http://XX.XXX.XX.XXX/INC/INC/scroller.zul?navigationId=INC_ApplRefund_D07_2"
2023/01/23 14:44:25 [error] 3294740#3294740: *2796 lua entry thread aborted: runtime error: rewrite_by_lua(nginx.conf:194):6: attempt to concatenate local 'v' (a table value)
stack traceback:
coroutine 0:
    rewrite_by_lua(nginx.conf:194): in function <rewrite_by_lua(nginx.conf:194):1>, client: XX.XXX.XXX.89, server: , request: "POST /INC/zkau;jsessionid=mm26zovs3fwo1xnstgy39zlhi HTTP/1.1", host: "XX.XXX.XX.XXX:8585", referrer: "http://XX.XXX.XX.XXX/INC/INC/scroller.zul?navigationId=INC_ApplRefund_D07_2"

通过添加以下内容修复了与“SSL握手中的对等方关闭连接”相关的错误:

proxy_ssl_session_reuse of;

但在日志中还是可以看到的。
现在我更感兴趣的是如何修复错误

[error] 3294740#3294740: *2796 lua entry thread aborted: runtime error: rewrite_by_lua(nginx.conf:194):6: attempt to concatenate local 'v' (a table value)
stack traceback:

在Jmeter中,此错误如下所示:

<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->

但是在Jmeter中,它强烈地描述了是什么原因导致的,因此,我附加了nginx日志

如果您能帮助我修复这些错误,我将非常高兴。

3qpi33ja

3qpi33ja1#

尝试连接局部'v'(表值)
这是很明显的,您正在尝试连接一个表值。
只有当表有一个实现__concat的元表时,才能对表值进行concat,但显然情况并非如此。
现在我更感兴趣的是如何修复错误
别把table的价值说得太清楚。
参考Lua Ngx API的手册,你会发现ngx.req.get_headers()返回的是一个由表组成的表,而不是一个由字符串或数字组成的表。
ngx.req.get_headers()返回的表中的每个表运行一个循环,然后执行concat操作。

相关问题