OpenResty 安装,openresty安装
OpenResty 安装,openresty安装
1. 准备工作
安装Centos -7.0操作系统
下载openresty
地址:https://openresty.org/download/openresty-1.13.6.2.tar.gz
2. 依赖包安装
安装Openresty 之前首先需要安装依赖包pcre-devel openssl-devel readline-devel gcc,安装依赖包在机器可以联网时,我们可以直接采用yum –y install pcre-devel openssl-devel readline-devel gcc。在机器不能联网时,我们建议首先搭建YUM本地源,然后在执行上边的命令进行依赖包的安安装。(搭建参照地址:https://blog.csdn.net/qqlk123/article/details/66973925)
3. 配置、编译、安装Openresty
a) 首先将下载的openresty压缩包拷贝到centos 7 中,并解压文件,命令为 tar –zxvf openresty-1.13.6.2.tar.gz
b) 进入解压好的openresty目录,利用configure 命令配置openresty的安装模块以及安装路径
./configure--prefix=/opt/openresty --with-luajit --without-http_redis2_module--with-http_iconv_module
c) 当正确配置完成之后,会出现两个指令一个是gmake 一个是gmake install ,首先利用命令gmake 对配置之后的文件进行编译,编译完成之后利用gmake install将编译后文件安装。
4. nginx.conf+lua配置示例
#woker进程数,要与当前所属服务器的核心数相同
worker_processes 1;
events {
#每个worker的连接数,默认是1024,根据实际负载情况以及服务器硬件动态调整
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#lua的模块相当于Java中的各种相关依赖jar
lua_package_path"/opt/openresty/lualib/http/?.lua;;";
gzip on;
#当设置自定义headers是启用
underscores_in_headers on;
#dns 解析配置
resolver 8.8.8.8 ipv6=off;
server {
listen 80;
location /oa {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Server $host;
default_type "text/html";
#lua脚本访问控制,拦截http请求,处理相关业务
access_by_lua_file/opt/openresty/nginx/lua/auth1.lua;
#最重要代理的地址
proxy_pass http://app.com:8081/test3;
}
location /logout{
#proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Server $host;
default_type "text/html";
access_by_lua_file/opt/openresty/nginx/lua/logout.lua;
proxy_pass http://app.com:8181/test4;
}
}
}
5. lua 脚本开发示例(自定义额外头部参数)
local cjson = require "cjson"
local http = require"resty.http"
local met = ngx.var.request_method
ngx.req.set_header("user_memberOf","教师");
ngx.req.set_header("user_alias","哈哈哈");
ngx.req.set_header("logout_url","http://www.baidu.com");
评论暂时关闭