一、安装
1.安装依赖
yum install pcre-devel openssl-devel gcc curl postgresql-devel
2.官网下载源码包
http://openresty.org/cn/download.html
- 新建项目目录结构
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
- redis可以通过设置key为路由地址,value为后端server地址,不需要更改配置文件实现动态路由
转载于:https://blog.51cto.com/haoyonghui/2377398