#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 {

    upstream master {
        server 10.1.1.100:9200;
        server 10.1.1.100:9200 backup;
        keepalive 15;
    }

    upstream slave {
        server 10.1.1.100:9200;
        keepalive 15;
    }

    server {
        server_name localhost 127.0.0.1 10.1.1.225;
        listen 8888;

        location / {

            #if ($request_method = "GET") {
                #proxy_pass http://master; # get直接走upstream master
            #}

            content_by_lua_block {

                local function getFile(file_name)
                    local f = assert(io.open(file_name, 'r'))
                    local string = f:read("*all")
                    f:close()
                    return string
                end

                ngx.req.read_body()
                ngx.log(ngx.ERR, '$$$' ..ngx.var.request_uri .. '$$$')

                local request_body = ngx.req.get_body_data()
                if nil == request_body then
                    local file_name = ngx.req.get_body_file()
                    if file_name then
                        request_body = getFile(file_name)
                    end
                end

                local master_resp, slave_resp
                local req_method_constant = ngx['HTTP_' .. ngx.req.get_method()]
                local arry = {method = req_method_constant, body = request_body}

                if req_method_constant == ngx.HTTP_POST or req_method_constant == ngx.HTTP_PUT or req_method_constant == ngx.HTTP_DELETE then
                    master_resp, slave_resp = ngx.location.capture_multi{
                        {'/slave' .. ngx.var.request_uri, arry},
                        {'/master' .. ngx.var.request_uri, arry}
                    }
                else
                    master_resp = ngx.location.capture_multi{
                        {'/master' .. ngx.var.request_uri, arry}
                    }
                end

                --[[
                    使用master的响应头
                --]]
                for k,v in pairs(master_resp.header) do
                    ngx.header[k] = v
                end
                ngx.say(master_resp.body)
            }
        }

        location ~* ^/master {
            internal;
            log_subrequest on;
            rewrite ^/master(.*)$ $1 break;
            proxy_pass http://master;
            access_log logs/master.log;
        }

        location ~* ^/slave {
            internal;
            log_subrequest on;
            rewrite ^/slave(.*)$ $1 break;
            proxy_pass http://slave;
            access_log logs/slave.log;
        }
    }
}

  

OpenResty(nginx_lua_module)做ES代理以及备份ES数据的更多相关文章

  1. es快照和备份

    注册前要注意配置文件加上 path.repo: ["/data/es_backup"] 然后重启es 不然会报错doesn't match any of the locations ...

  2. nginx做正向代理(Centos7,支持http和https)

    默认的情况下,使用nginx做正向代理可以解析http请求, 对于诸如baidu.com这样的https请求,nginx默认并不支持,不过我们可以借助第三方模块来实现. 1.先说默认情况下的代理配置 ...

  3. 用apache做为代理下载本地pdf文件

    有一些公司会用apache做为代理,下载服务器上的pdf文件.以下是apache做为代理的配置 一. 环境 centos6.5  192.168.69.3 二. yum安装apache 服务 [zxj ...

  4. 使用ssh正向连接、反向连接、做socks代理的方法

     ssh -L 219.143.16.157:58080:172.21.163.32:8080 用户名@localhost -p 10142  在 219.143.16.157机器执行   将ssh隧 ...

  5. nginx做反向代理并防盗链

    nginx做反向代理真的非常简单,只需设置location+proxy_pass即可. 防盗链配置有些复杂,需要注意的地方: 在防盗链的location中需要再设置一下proxy_pass(在这里走了 ...

  6. 用nginx做反向代理来访问防外链图片

    用nginx做反向代理来访问防外链图片 女儿的博客从新浪搬到wordpress后,发现原来博客上链接的新浪相册的图片都不能访问了,一年的博客内容,一个个去重新上传图片,修正链接也是个大工程.还是得先想 ...

  7. 【Nginx】使用Nginx做反向代理时,关于被代理服务器相应的超时设置

    > 参考的优秀文章 Module ngx_http_proxy_module > 设置等待被代理服务器的最大响应时间 使用Nginx做反向代理时,因被代理服务器因业务确实复杂,需时较久,往 ...

  8. Nginx做反向代理总是被系统kill

    公司使用Nginx做反向代理,以前都挺正常的,最近不知怎么回事总是无端被系统kill,而在nginx错误日志中也没有信息输出. 网上查了很多资料,也没什么靠谱的回答,唯一觉得有点关联的就是linux ...

  9. nginux做反向代理配置文件

    做反向代理的配置文件最好单独创建一个文件,然后在主配置文件中使用 include nginx-test.config;  这样的方式来导入. 配置代码如下: ## Basic reverse prox ...

随机推荐

  1. 使用 Entity Framework Core 时,通过代码自动 Migration

    一 介绍 在使用 Entity Framework Core (下面就叫 EF Core 吧)进行开发时,如果模型有变动,我们要在用 EF Core 提供的命令行工具进行手工迁移,然后再运行程序.但是 ...

  2. MVC项目

    MVC 项目分析: 1.AssemblyInfo.cs  保存管理文件发布的一些属性和版本号 2.Content文件夹(保存相关的样式文件.CSS),scripts文件夹(保存相关的),fonts文件 ...

  3. 五、HTML判断输入长度,体会字体颜色变化

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  4. 自定义ViewGroup

    ViewGroup --->A ViewGroup is a special view that can contain other views (called children.) The v ...

  5. linux 登录档配置分析

    登录档的重要性 解决系统方面的错误: 解决网络服务的问题: 过往事件记录簿: Linux 常见的登录档档名 /var/log/cron: 你的 crontab 排程有没有实际被进行? 进行过程有没有发 ...

  6. RPC学习----Thrift快速入门和Java简单示例

    一.什么是RPC? RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. RPC协议 ...

  7. Codeforces 740A. Alyona and copybooks 模拟

    A. Alyona and copybooks time limit per test: 1 second memory limit per test: 256 megabytes input: st ...

  8. VBA中常用技巧

    常量定义 Public Const i as Integer = 1 自定义类型 Type mytype i   as Integer b  as  Boolean s  as  String end ...

  9. Python执行命令行

    背景 我们知道,虽然会破坏平台独立性,但是有的时候需要在代码里面调用命令行来获取一些信息,那么了解在 Python 中如何执行命令行至关重要 使用介绍 Python 中使用命令行可以通过 subpro ...

  10. [OSI]网络7层模型的理解

    应用层:HTTP 应用层 表现层:编码 表现层 会话层:端口 会话层 传输层:TCP/UDP 协议 传输层 网络层:IP 标记  +-- 网络层 --+  网络层 数据链路层:计算机Mac地址标记 | ...