nginx发起http请求

配置

可以用proxypass

配置文件如下:

worker_processes  2;        #nginx worker 数量
error_log logs/error.log;   #指定错误日志文件路径
events {
    worker_connections 1024;
}

http {
    upstream md5_server{
        server 127.0.0.1:81;    
        keepalive 20; 
    }

    server {
        listen    1024;

        location /test {
            content_by_lua_block {
                ngx.req.read_body()
                local args, err = ngx.req.get_uri_args()



                local res = ngx.location.capture('/spe_md5',
                    {
                        method = ngx.HTTP_POST,
                        body = args.data
                    }
                )

                if 200 ~= res.status then
                    ngx.exit(res.status)
                end




        ngx.say(args.data)
        ngx.say(res.body)
                if args.key == res.body then
                    ngx.say("valid request")
                else
                    ngx.say("invalid request")
                end
            }
        }

        location /spe_md5 {
            proxy_pass http://md5_server;
            #For HTTP, the proxy_http_version directive should be set to “1.1” and the “Connection” 
            #header field should be cleared.(from:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive)
            proxy_http_version 1.1;
            proxy_set_header Connection "";
        }
    }# server port 1024 

    server {
        listen    81;
        location /spe_md5 {
            content_by_lua_block {
                ngx.req.read_body()
                local data = ngx.req.get_body_data()
                ngx.print(ngx.md5(data)) 
            }
        }
    }
}

配置了一个81端口的上游服务器,并在/test下用capture做了一个子查询,最后返回md5值

测试

curl -v -X GET 'localhost:1024/test?key=5d41402abc4b2a76b9719d911017c592&data=hello'

参考

openresty最佳实践

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值