Openresty的支持lua脚本拓展的版本--lua脚本编写实现nginx广播

安装好Openresty后进入openresty的./nginx/conf/nginx.conf修改配置文件。
如下:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       8090;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
	location /hx/  {
		default_type 'application/json';
		--lua脚本代码块
		content_by_lua_block {
		--从openstry库中获取json序列化的包使用
			local cjson = require "cjson"
			ngx.req.read_body()
			local body_data = ngx.req.get_body_data()
			local post_data = body_data
			--打日志到./ngixn/logs/error.log文件(./以openstry安装目录为准)
			ngx.log(ngx.ERR,"获取到的请求参数为: ",post_data)
			local parent_uri = ngx.var.uri
			ngx.log(ngx.ERR,"父请求的请求路径为: ",parent_uri)
			--替换路径/hx=>/sit2...
			local sit2_path = string.gsub(parent_uri, "/hx", "/sit2")
			local sit_path = string.gsub(parent_uri, "/hx", "/sit")
			ngx.log(ngx.ERR,"=====开始访问sit,请求子请求sit的路径为: ",sit_path,"=====")
			--发送子请求()需要下面配置了location模块才有用(注意location的配置规则)
			local res = ngx.location.capture(sit_path,{
                                method = ngx.HTTP_POST,
                                body = post_data,
                                headers = {
                                        ['Content-Type'] = "application/json",
                                        ["Content-Length"] = #post_data
                                        }
                                    }
                                )
			ngx.log(ngx.ERR,"=====开始访问sit2,请求子请求sit2的路径为: ",sit2_path,"=====")
                        local res2 = ngx.location.capture(sit2_path,{
                                method = ngx.HTTP_POST,
                                body = post_data,
                                headers = {
                                        ['Content-Type'] = "application/json",
                                        ["Content-Length"] = #post_data
                                        }
                                    }
                               )
			ngx.log(ngx.ERR,"sit2 res2: ",cjson.encode(res2.body))
			ngx.status = res.status
			ngx.log(ngx.ERR,"sit res: ",cjson.encode(res.body))
			ngx.say(res.body)
		}
	}

	location /sit/ {
		default_type 'application/json';
		proxy_pass http://12.99.108.178:9001/;
	}

       location  /sit2/ {
		default_type 'application/json';
        	proxy_pass http://12.103.47.108:9001/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}

修改后启动配置文件。配置文件中的注释都可以删除掉。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值