nginx使用ngx_lua子請求配置反向代理

Nginx 使用 ngx_lua 子請求配置反向代理

nginx 配置文件配置

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

    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;

 	location /sub1{
	
		internal;
		proxy_http_version 1.1;
		proxy_pass http://192.168.220.200:8000/;
	}
	location /{
		default_type text/html;
    	content_by_lua_file  /usr/local/openresty/nginx/conf/subreq.lua;
	}
    }
}

子請求代碼編寫

local action = ngx.var.request_method;
ngx.req.read_body();

local data = ngx.req.get_body_data();
local args = ngx.req.get_uri_args();
local httpmethod = ngx.req.get_method();

local res ;
if ngx.req.get_method()=="GET" then
res = ngx.location.capture("/sub1"..ngx.var.request_uri,{method=ngx.HTTP_GET,body= data})
else
res = ngx.location.capture("/sub1"..ngx.var.request_uri,{method=ngx.HTTP_POST,body= data})
end

ngx.status=res.status
for k,v in pairs(res.header) do
		ngx.header[k]=v
end	
ngx.print(res.body)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章