如何在nginx集成lua脚本,并且可以在mirror中使用lua脚本发送http请求A,同时这个请求A能够支持keep-alive?

新手上路,请多包涵

如何在nginx集成lua脚本,并且可以在mirror中使用lua脚本发送http请求A,同时这个请求A能够支持keep-alive?

我尝试使用nginx集成lua脚本,并且在mirror1中使用lua脚本发送请求,但是没办法keep-alive
报错failed to set keepalive。
如下:

user  nginx;
worker_processes  auto;

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


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
        '"$status" $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for" '
        '"$gzip_ratio" $request_time $bytes_sent $request_length';

    open_log_file_cache max=1000 inactive=60s;

    access_log  /var/log/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    upstream backends {
        server localhost:9099;
    }
    upstream mirror1_backend {
        server localhost:9099;
    }
    upstream mirror2_backend {
        server localhost:9099;
    }
    upstream mirror3_backend {
        server localhost:9099;
    }
    upstream mirror4_backend {
        server localhost:9099;
    }
    upstream mirror5_backend {
        server localhost:9099;
    }
    upstream mirror6_backend {
        server localhost:9099;
    }
    upstream mirror7_backend {
        server localhost:9099;
    }
    upstream mirror8_backend {
        server localhost:9099;
    }
    upstream mirror9_backend {
        server localhost:9099;
    }
    upstream mirror10_backend {
        server localhost:9099;
    }

    server {
        listen 9099;
        location / {
            return 200;
        }
    }

    #gzip  on;

    server {
        listen       8000-8050;
        listen       9000-9050;
        server_name  _;

        location / {
            mirror /mirror1;

            proxy_pass  http://backends;

            proxy_method $request_method;

            mirror_request_body on;
            proxy_pass_request_body on;

            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location = /mirror1 {
            internal;
            resolver 127.0.0.11 valid=10s;
            set $mirror1_url http://mirror1_backend$request_uri;
            set $mirror_request_body $request_body;
            set $mirror_request_method $request_method;
            rewrite_by_lua_block {
                local http = require "resty.http"
                local httpc = http.new()
                math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,7)))
                local random_number = math.random(1, 100)
                local traffic1 = 0
                local is_mirror = (random_number <= traffic1) and true or false
                local url = ngx.var.mirror1_url
                if is_mirror then
                    local res, err = httpc:request_uri(url, {
                        method = ngx.var.mirror_request_method,
                        body = ngx.var.mirror_request_body,
                        headers = ngx.req.get_headers(),
                        ssl_verify = false, timeout = 5000,
                    })
                end
                local ok, err = httpc:set_keepalive(60000, 100)
                if not ok then
                    ngx_log(ngx_ERR, "failed to set keepalive: ", err)
                end
            }

            proxy_pass http://mirror1_backend$request_uri;

            proxy_method $request_method;

            mirror_request_body on;
            proxy_pass_request_body on;

            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location = /mirror2 {
            internal;
            resolver 127.0.0.11 valid=10s;
            set $mirror2_url http://mirror2_backend$request_uri;
            set $mirror_request_body $request_body;
            set $mirror_request_method $request_method;
            rewrite_by_lua_block {
                local http = require "resty.http"
                local httpc = http.new()
                math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,7)))
                local random_number = math.random(1, 100)
                local traffic2 = 0
                local is_mirror = (random_number <= traffic2) and true or false
                local url = ngx.var.mirror2_url
                if is_mirror then
                    local res, err = httpc:request_uri(url, {
                        method = ngx.var.mirror_request_method,
                        body = ngx.var.mirror_request_body,
                        headers = ngx.req.get_headers(),
                        ssl_verify = false, timeout = 5000,
                    })
                    httpc:set_keepalive()
                end
            }
            proxy_pass http://mirror2_backend$request_uri;

            proxy_method $request_method;

            mirror_request_body on;
            proxy_pass_request_body on;

            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location = /mirror3 {
            internal;
            resolver 127.0.0.11 valid=10s;
            set $mirror3_url http://mirror3_backend$request_uri;
            set $mirror_request_body $request_body;
            set $mirror_request_method $request_method;
            rewrite_by_lua_block {
                local http = require "resty.http"
                local httpc = http.new()
                math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,7)))
                local random_number = math.random(1, 100)
                local traffic3 = 0
                local is_mirror = (random_number <= traffic3) and true or false
                local url = ngx.var.mirror3_url
                if is_mirror then
                    local res, err = httpc:request_uri(url, {
                        method = ngx.var.mirror_request_method,
                        body = ngx.var.mirror_request_body,
                        headers = ngx.req.get_headers(),
                        ssl_verify = false, timeout = 5000,
                    })
                    httpc:set_keepalive()
                end
            }
            proxy_pass http://mirror3_backend$request_uri;

            proxy_method $request_method;

            mirror_request_body on;
            proxy_pass_request_body on;

            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location = /mirror4 {
            internal;
            resolver 127.0.0.11 valid=10s;
            set $mirror4_url http://mirror4_backend$request_uri;
            set $mirror_request_body $request_body;
            set $mirror_request_method $request_method;
            rewrite_by_lua_block {
                local http = require "resty.http"
                local httpc = http.new()
                math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,7)))
                local random_number = math.random(1, 100)
                local traffic4 = 0
                local is_mirror = (random_number <= traffic4) and true or false
                local url = ngx.var.mirror4_url
                if is_mirror then
                    local res, err = httpc:request_uri(url, {
                        method = ngx.var.mirror_request_method,
                        body = ngx.var.mirror_request_body,
                        headers = ngx.req.get_headers(),
                        ssl_verify = false, timeout = 5000,
                    })
                    httpc:set_keepalive()
                end
            }
            proxy_pass http://mirror4_backend$request_uri;

            proxy_method $request_method;

            mirror_request_body on;
            proxy_pass_request_body on;

            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location = /mirror5 {
            internal;
            resolver 127.0.0.11 valid=10s;
            set $mirror5_url http://mirror5_backend$request_uri;
            set $mirror_request_body $request_body;
            set $mirror_request_method $request_method;
            rewrite_by_lua_block {
                local http = require "resty.http"
                local httpc = http.new()
                math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,7)))
                local random_number = math.random(1, 100)
                local traffic5 = 0
                local is_mirror = (random_number <= traffic5) and true or false
                local url = ngx.var.mirror5_url
                if is_mirror then
                    local res, err = httpc:request_uri(url, {
                        method = ngx.var.mirror_request_method,
                        body = ngx.var.mirror_request_body,
                        headers = ngx.req.get_headers(),
                        ssl_verify = false, timeout = 5000,
                    })
                    httpc:set_keepalive()
                end
            }
            proxy_pass http://mirror5_backend$request_uri;

            proxy_method $request_method;

            mirror_request_body on;
            proxy_pass_request_body on;

            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location = /mirror6 {
            internal;
            resolver 127.0.0.11 valid=10s;
            set $mirror6_url http://mirror6_backend$request_uri;
            set $mirror_request_body $request_body;
            set $mirror_request_method $request_method;
            rewrite_by_lua_block {
                local http = require "resty.http"
                local httpc = http.new()
                math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,7)))
                local random_number = math.random(1, 100)
                local traffic6 = 0
                local is_mirror = (random_number <= traffic6) and true or false
                local url = ngx.var.mirror6_url
                if is_mirror then
                    local res, err = httpc:request_uri(url, {
                        method = ngx.var.mirror_request_method,
                        body = ngx.var.mirror_request_body,
                        headers = ngx.req.get_headers(),
                        ssl_verify = false, timeout = 5000,
                    })
                    httpc:set_keepalive()
                end
            }
            proxy_pass http://mirror6_backend$request_uri;

            proxy_method $request_method;

            mirror_request_body on;
            proxy_pass_request_body on;

            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location = /mirror7 {
            internal;
            resolver 127.0.0.11 valid=10s;
            set $mirror7_url http://mirror7_backend$request_uri;
            set $mirror_request_body $request_body;
            set $mirror_request_method $request_method;
            rewrite_by_lua_block {
                local http = require "resty.http"
                local httpc = http.new()
                math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,7)))
                local random_number = math.random(1, 100)
                local traffic7 = 0
                local is_mirror = (random_number <= traffic7) and true or false
                local url = ngx.var.mirror7_url
                if is_mirror then
                    local res, err = httpc:request_uri(url, {
                        method = ngx.var.mirror_request_method,
                        body = ngx.var.mirror_request_body,
                        headers = ngx.req.get_headers(),
                        ssl_verify = false, timeout = 5000,
                    })
                    httpc:set_keepalive()
                end
            }
            proxy_pass http://mirror7_backend$request_uri;

            proxy_method $request_method;

            mirror_request_body on;
            proxy_pass_request_body on;

            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location = /mirror8 {
            internal;
            resolver 127.0.0.11 valid=10s;
            set $mirror8_url http://mirror8_backend$request_uri;
            set $mirror_request_body $request_body;
            set $mirror_request_method $request_method;
            rewrite_by_lua_block {
                local http = require "resty.http"
                local httpc = http.new()
                math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,7)))
                local random_number = math.random(1, 100)
                local traffic8 = 0
                local is_mirror = (random_number <= traffic8) and true or false
                local url = ngx.var.mirror8_url
                if is_mirror then
                    local res, err = httpc:request_uri(url, {
                        method = ngx.var.mirror_request_method,
                        body = ngx.var.mirror_request_body,
                        headers = ngx.req.get_headers(),
                        ssl_verify = false, timeout = 5000,
                    })
                    httpc:set_keepalive()
                end
            }
            proxy_pass http://mirror8_backend$request_uri;

            proxy_method $request_method;

            mirror_request_body on;
            proxy_pass_request_body on;

            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location = /mirror9 {
            internal;
            resolver 127.0.0.11 valid=10s;
            set $mirror9_url http://mirror9_backend$request_uri;
            set $mirror_request_body $request_body;
            set $mirror_request_method $request_method;
            rewrite_by_lua_block {
                local http = require "resty.http"
                local httpc = http.new()
                math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,7)))
                local random_number = math.random(1, 100)
                local traffic9 = 0
                local is_mirror = (random_number <= traffic9) and true or false
                local url = ngx.var.mirror9_url
                if is_mirror then
                    local res, err = httpc:request_uri(url, {
                        method = ngx.var.mirror_request_method,
                        body = ngx.var.mirror_request_body,
                        headers = ngx.req.get_headers(),
                        ssl_verify = false, timeout = 5000,
                    })
                    httpc:set_keepalive()
                end
            }
            proxy_pass http://mirror9_backend$request_uri;

            proxy_method $request_method;

            mirror_request_body on;
            proxy_pass_request_body on;

            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location = /mirror10 {
            internal;
            resolver 127.0.0.11 valid=10s;
            set $mirror10_url http://mirror10_backend$request_uri;
            set $mirror_request_body $request_body;
            set $mirror_request_method $request_method;
            rewrite_by_lua_block {
                local http = require "resty.http"
                local httpc = http.new()
                math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,7)))
                local random_number = math.random(1, 100)
                local traffic10 = 0
                local is_mirror = (random_number <= traffic10) and true or false
                local url = ngx.var.mirror10_url
                if is_mirror then
                    local res, err = httpc:request_uri(url, {
                        method = ngx.var.mirror_request_method,
                        body = ngx.var.mirror_request_body,
                        headers = ngx.req.get_headers(),
                        ssl_verify = false, timeout = 5000,
                    })
                    httpc:set_keepalive()
                end
            }
            proxy_pass http://mirror10_backend$request_uri;

            proxy_method $request_method;

            mirror_request_body on;
            proxy_pass_request_body on;

            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
阅读 668
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏