openresty的使用

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下即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值