openresty安裝

1、安裝
1.安裝依賴
yum install pcre-devel openssl-devel gcc curl postgresql-develhtml

2.官網下載源碼包
http://openresty.org/cn/download.htmlnginx

  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-streamredis

gmake && gmake installsql

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

6.配置文件編寫
vim conf/nginx.conf # 內容就是nginx配置文件格式vim

7.啓動
nginx -p pwd/ -c conf/nginx.conf後端

2、openresty特性
動態路由功能
1.nginx配置文件編輯
cd work/conf
vim nginx.conf
worker_processes 1;
error_log logs/error.log info;
events {
worker_connections 1024;
}
http {curl

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>")
        #}
    }
}

}ide

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

  1. redis能夠經過設置key爲路由地址,value爲後端server地址,不須要更改配置文件實現動態路由
相關文章
相關標籤/搜索