【网关开发】1.编译Openresty 服务器初始化搭建

引言

是一款基于 NGINX 和 LuaJIT 的 Web 平台,公司采用的Openresty搭建的接入层网关,用lua实现的waf防火墙,节点的负载均衡,流量染色,流量规则等功能,后续会逐步的将功能进行拆解记录。
本篇是从原始openresty代码进行搭建。并且会引入一些扩展模块。

相关地址

中文官网:http://openresty.org/cn/
代码存放的git地址:https://github.com/zhaoshoucheng/openresty

开始搭建

工程目录结构

最开始的工程都是非常简单的。

openresty_src 存放openresty的源码,可以按照不同的版本存放不同的目录中
other_nginx_bundles 存放的扩展的模块,这里我们扩展可以先主要关注nginx-sticky-module-ng 和 nginx_upstream_check_module 两个模块,以后的所有扩展模块都会放在这里
(nginx-sticky-module-ng 用来会话保持,nginx_upstream_check_module用来上游服务的健康监测)
pkg 存放着已经安装(configure,make,make install)好的文件,源码安装时没有这个目录,这个目录方便展示编译好后启动配置,在“./configure --prefix=” 指向路径
--global_conf 存放全局的配置
--openresty_conf 存放各种域名服务相关的配置
shell 文件存放编译相关的shell 文件

开始编译

目录存放在/data/openresty 中
build.sh 文件

#!/bin/bash

DIR="/data/openresty"
OPENSSL_VERSION="1.1.0k"

openrestydir=$DIR"/openresty_src/1.15.8.1"
otherbundles=$DIR"/other_nginx_bundles"
cd $openrestydir
./configure --prefix=/data/pkg/openresty --user=nginx --group=nginx --with-http_v2_module --with-http_sub_module --with-http_realip_module --with-http_stub_status_module --with-luajit --add-module=$otherbundles/nginx-sticky-module-ng  --add-module=$otherbundles/nginx_upstream_check_module --add-module=$otherbundles/ngx_stream_upstream_check_module --with-openssl=$otherbundles/openssl/$OPENSSL_VERSION --with-pcre=$otherbundles/pcre/pcre-8.39 --with-zlib=$otherbundles/zlib/zlib-1.2.10

make

make install

注意配置 pkg的路径,--add-module增加扩展模块

可能遇到的问题。

1.configure 阶段,
./configure: error: the ngx_http_upstream_check_module addon error.
ngx_http_upstream_check_module 地址:https://github.com/yaoweibin/nginx_upstream_check_module
这里倒腾了很多次,经过测试,其实使用ngx_http_upstream_check_module的master版本就可以,这里注意openresty不能太高,如果直接用新的源码不需要进行pitch
直接./configure就可以
pitch操作(非必需):(build.sh文件中)

cd $DIR/openresty_src/1.15.8.1/bundle/nginx-1.15.8
patch -p0 < $DIR/other_nginx_bundles/nginx_upstream_check_module/check_1.11.5+.patch

这里以前一直在纠结要在哪个目录下进行patch,主要是不理解patch原理,以为需要在openresty的目录下, 其实要进到nginx目录下。找到目录bundle/nginx-1.15.8 执行。
如果路径不对,会报nginx can't find file to patch at input line 错误
2.make 阶段
报错:openresty ngx_http_upstream_rr_peer_t 没有名为check_index 的成员
或者 error: ‘ngx_http_upstream_rr_peer_t’ has no member named ‘check_index’
会发生在nginx-sticky-module-ng 模块里。这里应该是 nginx-sticky-module-ng 需要ngx_http_upstream_check_module的内容,需要pitch一下。

cd $DIR/other_nginx_bundles/nginx-sticky-module-ng
patch -p0 < $DIR/other_nginx_bundles/nginx_upstream_check_module/nginx-sticky-module.patch

在执行./configure应该就可以了。如果还有什么问题,欢迎讨论。

配置启动

一般会将pkg中openresty 文件和配置文件分开,这样如果打包版本可以直接删除原文件重新打新包。
主配置文件 nginx.conf
在http层添加include

http {
    include /data/pkg/global_conf/apis.conf;
    include /data/pkg/openresty_conf/*.conf;
}

各个服务配置
server_test.com.conf


server {    
    listen 9090;
    #replace
    server_name server_test.com;


    location / {
  proxy_pass http://server_test;
}
}

upstream.conf (注意探活配置nginx_upstream_check_module 模块会用)

upstream server_test {
server 10.218.57.37:8090;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD /ping HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}

全局配置,这里主要配置nginx_upstream_check_module健康监测接口
global_conf/apis.conf

# 对外接口相关配置
server {
  listen       9091;
  server_name  localhost;
  client_body_buffer_size 2m;
  
  root   html;
  index  index.html index.htm;

  location = /status {
    check_status;
    access_log  off;
  }
}

启动nginx 服务
/data/pkg/openresty/nginx/sbin/nginx -c /data/pkg/openresty/nginx/conf/nginx.conf

测试

访问:http://10.218.57.114:9090/

探活接口:http://10.218.57.114:9091/status
我们只有一个服务

后续

这块是openresty基础服务,后续我们逐渐会向这个服务中添加各个阶段的lua模块,实现相应的防火墙、流量染色、流量规则等功能。

posted @ 2022-09-08 20:53  zscbest  阅读(660)  评论(1编辑  收藏  举报