openresty安装

一、安装
1.安装依赖
yum install pcre-devel openssl-devel gcc curl postgresql-devel

2.官网下载源码包
http://openresty.org/cn/download.html

  1. 新建项目目录结构
    mkdir work
    cd work
    mkdir {conf,logs}

4.源码编译
./configure --prefix=/usr/local/openresty/ --with-http_stub_status_module --with-luajit --without-http_redis2_module --with-http_iconv_module --with-http_postgres_module --with-stream

gmake && gmake install

5.添加环境变量
vim /etc/profile
PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH

6.配置文件编写
vim conf/nginx.conf # 内容就是nginx配置文件格式

7.启动
nginx -p pwd/ -c conf/nginx.conf

二、openresty特性
动态路由功能
1.nginx配置文件编辑
cd work/conf
vim nginx.conf
worker_processes 1;
error_log logs/error.log info;
events {
worker_connections 1024;
}
http {

upstream apache.org {
    server apache.org;
}

upstream nginx.org {
    server nginx.org;
}

server {
    listen 9999;

    location = /redis {
        internal;
        set_unescape_uri $key $arg_key;
        redis2_query get $key;
        redis2_pass 127.0.0.1:6379;
    }

    location / {
        set $target '';
        access_by_lua '
            local key = ngx.var.http_user_agent
            local res = ngx.location.capture(
                "/redis", { args = { key = key }}
            )

            print("key: ", key)

            if res.status ~= 200 then
                ngx.log(ngx.ERR, "redis server returned bad status: ", res.status)
                ngx.exit(res.status)
            end

            if not res.body then
                ngx.log(ngx.ERR, "redis returned empty body")
                ngx.exit(500)
            end

            local parser = require "redis.parser"
            local server, typ = parser.parse_reply(res.body)
            if typ ~= parser.BULK_REPLY or not server then
                ngx.log(ngx.ERR, "bad redis response: ", res.body)
                ngx.exit(500)
            end

            print("server: ", server)
            ngx.var.target = server
        ';

        proxy_pass http://$target;

        #default_type text/html;
        #content_by_lua_block {
        #    ngx.say("<p>hello, world</p>")
        #}
    }
}

}

2.redis设置路由映射
redis> set foo apache.org
redis> set bar nginx.org

  1. redis可以通过设置key为路由地址,value为后端server地址,不需要更改配置文件实现动态路由

转载于:https://blog.51cto.com/haoyonghui/2377398

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值