1.安装
(1)在线安装依赖包的方式
# 安装依赖库:
yum install -y pcre-devel openssl-devel gcc curl
# 下载版本
wget https://openresty.org/download/openresty-1.15.8.1.tar.gz
# 解压
tar -xzvf openresty-1.15.8.1.tar.gz
# 进入解压目录
cd openresty-1.15.8.1/
# 检查配置环境, 生成 Makefile,默认安装到/usr/local/openresty:
./configure
# 编译安装
gmake && gmake install
(2)离线安装
1.准备openssl 、prce 、zlib 以及源码包
下载openresty
https://openresty.org/download/openresty-1.21.4.1.tar.gz
下载pcre
http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-8.45.tar.gz
下载openssl
https://www.openssl.org/source/openssl-1.1.1o.tar.gz
下载zlib
zlib我使用的是zlib-1.2.12.tar.gz版本的下面链接最高版本是1.2.11
https://jaist.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
2.安装
安装pcre
cd /usr/local/src
mkdir /usr/local/pcre
tar zxvf pcre-8.45.tar.gz
cd pcre-8.45
./configure --prefix=/usr/local/pcre
make
make install
安装openssl
cd /usr/local/src
mkdir /usr/local/openssl
tar zxvf openssl-1.1.1o.tar.gz
cd openssl-1.1.1o
./config -fPIC shared zlib --prefix=/usr/local/openssl
make
make install
安装zlib
cd /usr/local/src
mkdir /usr/local/zlib
tar zxvf zlib-1.2.12.tar.gz
cd zlib-1.2.12
./configure --prefix=/usr/local/zlib
make
make install
安装OpenResty
groupadd www
useradd -g www www -s /bin/false
cd /usr/local/src
tar zxvf openresty-1.21.4.1.tar.gz
cd openresty-1.21.4.1
./configure --prefix=/usr/local/openresty --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_v2_module --with-luajit --with-pcre-jit --with-http_realip_module --with-http_geoip_module --with-openssl=/usr/local/src/openssl-1.1.1o --with-zlib=/usr/local/src/zlib-1.2.12 --with-pcre=/usr/local/src/pcre-8.45
gmake
gmake install
注意:(1)–with-openssl=/usr/local/src/openssl-1.1.1o --with-zlib=/usr/local/src/zlib-1.2.12 --with-pcre=/usr/local/src/pcre-8.45指向的是源码包解压的路径,而不是安装的路径,否则会报错
(2)如果报了http_geoip_module相关的错 执行 yum install GeoIP-devel.x86_64命令 或者去掉./configure 命令中的–with-http_geoip_module
(3)如果报了gzip相关的错 执行 yum install gzip-devel 命令
3.启动停止服务
安装完成后再/usr/local/会生成一个openresty目录
检验配置
/usr/local/openresty/bin/openresty -t
启动
/usr/local/openresty/bin/openresty
停止
/usr/local/openresty/bin/openresty -s stop
#重新加载配置
/usr/local/openresty/bin/openresty -s reload
4.使用lua脚本
一、在/usr/local/openresty/ 目录下新建lua文件夹
mkdir /usr/local/openresty/lua
二、新建个lua文件 vim test.lua 加入如下内容
ngx.req.read_body()
local data = ngx.req.get_body_data()
local cjson = require("cjson")
local json = cjson.decode(data)
local strParam = json["strParam"]
local strParamJson = cjson.decode(strParam)
local regioncode = strParamJson["F_SIZE_ID"]
local server_one_url = string.format('%s%s','http://192.168.43.110:8081',ngx.var.uri)
--服务器二
local server_two_url = 'http://127.0.0.1:8082'
//获取真实ip
local headers=ngx.req.get_headers()
local ip=headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0"
--http post 请求方法
local function http_post(url, data)
local https = require ("resty.http")
local http = https:new()
local res, err = http:request_uri(url, {
method = 'POST',
body = data,
headers = {
['Content-Type'] = 'application/json';
--把真实ip传进去 后台就能获取得到真实ip
['X-Forwarded-For'] = ip;
}
})
if res.status == 200 then
ngx.say(res.body)
else
ngx.say('服务器错误!')
end
end
if regioncode ~= nil and regioncode == '520203' then
http_post(server_one_url, data)
else
http_post(server_two_url, data)
end
5.nginx.conf 文件
#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 {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ~ ^/(getRecord|getPage|test1|test2) {
#修改为直接返回text模式,而不是返回文件。默认配置在极速模式下得浏览器会形成下载文件
default_type text/html;
#关闭缓存模式,每次都读取lua文件,不使用已经缓存的lua文件(修改nginx.conf文件还是要重启的)
lua_code_cache off;
#在返回节点加载lua文件
content_by_lua_file /usr/local/openresty/lua/bdc.lua;
}
}
6.启动openresty测试
后台代码
@RestController
public class Test {
@RequestMapping("/test1")
public String test1(Map<String,String> map) {
return "11111" ;
}
}
测试的时候会报https相关的错,那是因为resty.http相关的依赖包不存在需要手动添加 http.lua http_connect.lua http_headers.lua依赖包
下载地址 :
https://github.com/ledgetech/lua-resty-http
把lib/resty下的三个依赖包 放到/usr/local/openresty/lualib/resty下即可