百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

Nginx系列:OpenResty 1.25.3.x 编译安装

bigegpt 2024-10-28 12:49 6 浏览

一、简介

本实验不仅仅记录Openresty的编译安装,重点记录了相关nginx模块的安装,以此来增强Openresty的功能。本实验安装的模块有:

  • geoip2:识别ip地址地理信息。
  • IP2Location:识别ip地址地理信息。(可以替代geoip2,与geoip2类似)
  • Modsecurity:开源waf,业界广泛使用的,真的免费。
  • nginx-ssl-fingerprint:ja3指纹,可以提高反爬效果。
  • nginx-module-vts:Nginx虚拟主机流量状态模块,适合一台主机上部署了很多站点的场景。
  • ngx_healthcheck:
    https://github.com/zhouchangxun/ngx_healthcheck_module,健康检查,不仅仅支持http。还支持更多:四层支持的检测类型:tcp / udp / http、七层支持的检测类型:http / fastcgi


PS:您可以简单的理解为Openresty其实就是Nginx,通过lua来扩展Nginx的功能。即使您的环境使用Nginx就可以满足您的需要,我依然建议您尝试使用Openresty代替Nginx,毕竟当您需要扩展其功能时Openresty会极为的方便。


二、实验环境

  1. 操作系统:Almalinux9.4(基本上兼容Centos9 Stream、RockLinux9)
  2. openresty版本:1.25.3.2


三、操作系统环境准备


3.1、时间同步

yum -y install chrony
mv /etc/chrony.conf /etc/chrony.conf.bak
vi /etc/chrony.conf

在 /etc/chrony.conf 文件开头增加下面的内容:

server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
server ntp5.aliyun.com iburst
server ntp6.aliyun.com iburst
server ntp7.aliyun.com iburst

如下图:

启动chronyd服务:

systemctl start chronyd.service
systemctl enable chronyd.service
systemctl status chronyd.service


3.2、关闭selinux

# 执行下面的命令即可
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config


3.3、安装相关依赖

dnf install -y epel-release

dnf install -y gcc-c++ perl readline-devel pcre-devel openssl-devel gcc postgresql-devel curl

dnf -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel unzip patch wget screen libxslt libxslt-devel gd gd-devel perl-ExtUtils-Embed vim bzip2 patch lbzip2


3.4、添加www用户和组

groupadd www
useradd -g www www -s /sbin/nologin -M


四、安装Openresty

为了方便学习的同学,特意将用到的安装包分享给大家,包名:openresty-1.25.3.2.zip,内容如下:

https://www.123865.com/s/Lj5A-uR2jd?提取码:4ztl
https://www.123684.com/s/Lj5A-uR2jd?提取码:4ztl


我实验时将openresty-1.25.3.2.zip解压后的内容上传到了/root/openresty-1.25.3.2,然后设置变量方便下面的操作

OPENRESTY_SOFTWARE="/root/openresty-1.25.3.2"
# 验证
echo $OPENRESTY_SOFTWARE



4.1、安装jemalloc

说明:

  • jemalloc 是一个高效的内存分配库,它被设计用于改善并发程序和高负载场景下的内存管理。与操作系统提供的默认内存分配器(例如 malloc)相比,jemalloc 在减少内存碎片、提高多线程性能、优化内存分配速度等方面具有显著优势。
  • github地址:
    https://github.com/jemalloc/jemalloc
  • 实验测试证明如果仅yum -y install jemalloc安装,nginx在编译--with-ld-opt="-ljemalloc"时无法通过


cd $OPENRESTY_SOFTWARE/modules
tar xf jemalloc-5.3.0.tar.bz2
cd jemalloc-5.3.0
./configure
gmake && gmake install
echo '/usr/local/lib' >> /etc/ld.so.conf.d/local.conf
ldconfig /usr/local/lib
ldconfig -v | grep jemalloc


4.2、geoip2 动态识别库

说明:

  • 用于使用geoip识别ip地址的地理信息。
  • github地址:
    https://github.com/maxmind/libmaxminddb/releases
  • 使用geoip需要libmaxminddb 对 mmdb 的高效访问,因此首先需要安装 libmaxminddb 的动态识别库。


cd $OPENRESTY_SOFTWARE/modules
tar xf libmaxminddb-1.11.0.tar.gz
cd libmaxminddb-1.11.0
./configure
gmake && gmake install
ldconfig /usr/local/lib


4.3、安装IP2Location C库

说明:

IP2Location类似geoip,也是用于识别ip地址地理信息的。相当于让自己多了一个选择,有人认为IP2Location优于geoip。

IP2Location也是有免费版的,而且很好用。

  • 参考:
    https://www.ip2location.com/development-libraries/ip2location/nginx
  • 下载ip2location-nginx:
    https://github.com/ip2location/ip2location-nginx
  • 下载ip2location C库:
    https://github.com/chrislim2888/IP2Location-C-Library
cd $OPENRESTY_SOFTWARE/modules/ip2location
tar xf IP2Location-C-Library-8.6.1.tar.gz
cd IP2Location-C-Library-8.6.1
autoreconf -i -v --force
./configure
gmake && gmake install
cd data
# 时间较长,耐心等待
perl ip-country.pl
# 然后执行:
ldconfig /usr/local/lib



4.4、安装Modsecurity

说明:

要使得nginx结合Modsecurity,①首先安装Modsecurity库;②再次安装ModSecurity-nginx模块。

也就是说openresty通过ModSecurity-nginx来调用Modsecurity库。

  • github地址:
    https://github.com/SpiderLabs/ModSecurity


4.4.1、安装Modsecurity所需的依赖

dnf -y install -y git wget

dnf -y install -y gcc-c++ flex bison yajl lua curl-devel curl zlib-devel pcre-devel pcre2-devel libxml2-devel ssdeep-devel libtool autoconf automake make 

#启用 PowerTools (CRB) 仓库,进而安装doxygen 
# 如果是Almalinux8 需要执行:dnf config-manager --set-enabled powertools
dnf config-manager --set-enabled crb
dnf -y install doxygen lua-devel yajl-devel lmdb lmdb-devel 


4.4.2、安装Modsecurity

cd $OPENRESTY_SOFTWARE/ModSecurity
git checkout -b v3/master origin/v3/master
git submodule init
git submodule update

sh build.sh
./configure
# 有多少核的cpu就-j多少,加快编译。
make -j4
make install


# modsecurity库安装到了 /usr/local/modsecurity/lib
echo "/usr/local/modsecurity/lib" | sudo tee /etc/ld.so.conf.d/modsecurity.conf
ldconfig
# 验证
ldconfig -p | grep modsecurity



4.5、准备nginx模块

cd $OPENRESTY_SOFTWARE/modules/ip2location/
tar xf ip2location-nginx-8.6.0.tar.gz

cd $OPENRESTY_SOFTWARE/modules/
tar xf openssl-1.1.1w.tar.gz
tar xf pcre-8.45.tar.gz
tar xf zlib-1.3.1.tar.gz
tar xf ngx_cache_purge-2.3.tar.gz
unzip ngx_healthcheck_module-master.zip
tar xf nginx-module-vts-0.2.2.tar.gz
tar xf ngx_http_geoip2_module-3.4.tar.gz

# git下载ModSecurity-nginx仓库即可,我已经在安装包中准备好了
git clone https://github.com/SpiderLabs/ModSecurity-nginx

# JA3指纹,我已经在安装包中准备好了
git clone https://github.com/phuslu/nginx-ssl-fingerprint


4.6、导入补丁

# 解压OpenResty
cd $OPENRESTY_SOFTWARE/
tar xf openresty-1.25.3.2.tar.gz
cd openresty-1.25.3.2/bundle/nginx-1.25.3/
  
# 导入模块 ngx_healthcheck_module
patch -p1 < ../../../modules/ngx_healthcheck_module-master/nginx_healthcheck_for_nginx_1.19+.patch
echo $?
  
# 导入模块:nginx-ssl-fingerprint
# https://github.com/phuslu/nginx-ssl-fingerprint?tab=readme-ov-file
patch -p1 < ../../../modules/nginx-ssl-fingerprint/patches/nginx-1.25.patch
echo $?
  
# 导入模块:nginx-ssl-fingerprint的openssl补丁
# 我现在还是在openresty-1.25.3.x/bundle/nginx-1.25.3/ 目录下,但是这个补丁是给openssl打的,这里为了操作顺畅,直接添加参数-d指定打补丁的目录
# 且注意,打补丁的openssl的版本号。不要打错了。我这里默认使用的是openssl1.1.1w
patch -p1 -d ../../../modules/openssl-1.1.1w < ../../../modules/nginx-ssl-fingerprint/patches/openssl.OpenSSL_1_1_1-stable.patch
echo $?


4.7、编译安装Openresty

cd $OPENRESTY_SOFTWARE/openresty-1.25.3.2
./configure \
--with-luajit \
--with-http_iconv_module \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_perl_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_auth_request_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-threads \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-http_slice_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-pcre=../modules/pcre-8.45 \
--with-zlib=../modules/zlib-1.3.1 \
--with-openssl=../modules/openssl-1.1.1w \
--with-http_perl_module=dynamic \
--add-module=../modules/ngx_cache_purge-2.3 \
--add-module=../modules/ngx_healthcheck_module-master \
--add-dynamic-module=../modules/ngx_http_geoip2_module-3.4 \
--add-dynamic-module=../modules/ip2location/ip2location-nginx-8.6.0 \
--add-module=../modules/nginx-ssl-fingerprint \
--add-module=../modules/ModSecurity-nginx \
--add-module=../modules/nginx-module-vts-0.2.2 \
--with-ld-opt="-L/usr/local/lib" \
--with-ld-opt="-ljemalloc"

你可以根据自己的实际情况调整某些模块为“动态模块”,这样带需要的时候再使用。动态模块,静态模块各有优缺点,请自行斟酌。

推荐Openresty的默认安装位置保持默认的/usr/local/openresty,除非你有特殊需求再自行修改编译选项。--prefix=PATH

make && make install


4.8、创建启动脚本

vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
LimitCORE=infinity
LimitNOFILE=1024000
LimitNPROC=1024000
PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
systemctl status nginx


4.9、设置Openresty环境变量

cat>/etc/profile.d/openresty.sh<<EOF
export PATH=\$PATH:/usr/local/openresty/bin:/usr/local/openresty/luajit/bin
EOF
source /etc/profile


总结

到此openresty编译安装完毕。

这里不记录ip2location、modsecurity之类的使用,之后单独笔记记录如何使用。方便区分安装和使用,尽量不重复记录。

相关推荐

悠悠万事,吃饭为大(悠悠万事吃饭为大,什么意思)

新媒体编辑:杜岷赵蕾初审:程秀娟审核:汤小俊审签:周星...

高铁扒门事件升级版!婚宴上‘冲喜’老人团:我们抢的是社会资源

凌晨两点改方案时,突然收到婚庆团队发来的视频——胶东某酒店宴会厅,三个穿大红棉袄的中年妇女跟敢死队似的往前冲,眼瞅着就要扑到新娘的高额钻石项链上。要不是门口小伙及时阻拦,这婚礼造型团队熬了三个月的方案...

微服务架构实战:商家管理后台与sso设计,SSO客户端设计

SSO客户端设计下面通过模块merchant-security对SSO客户端安全认证部分的实现进行封装,以便各个接入SSO的客户端应用进行引用。安全认证的项目管理配置SSO客户端安全认证的项目管理使...

还在为 Spring Boot 配置类加载机制困惑?一文为你彻底解惑

在当今微服务架构盛行、项目复杂度不断攀升的开发环境下,SpringBoot作为Java后端开发的主流框架,无疑是我们手中的得力武器。然而,当我们在享受其自动配置带来的便捷时,是否曾被配置类加载...

Seata源码—6.Seata AT模式的数据源代理二

大纲1.Seata的Resource资源接口源码2.Seata数据源连接池代理的实现源码3.Client向Server发起注册RM的源码4.Client向Server注册RM时的交互源码5.数据源连接...

30分钟了解K8S(30分钟了解微积分)

微服务演进方向o面向分布式设计(Distribution):容器、微服务、API驱动的开发;o面向配置设计(Configuration):一个镜像,多个环境配置;o面向韧性设计(Resista...

SpringBoot条件化配置(@Conditional)全面解析与实战指南

一、条件化配置基础概念1.1什么是条件化配置条件化配置是Spring框架提供的一种基于特定条件来决定是否注册Bean或加载配置的机制。在SpringBoot中,这一机制通过@Conditional...

一招解决所有依赖冲突(克服依赖)

背景介绍最近遇到了这样一个问题,我们有一个jar包common-tool,作为基础工具包,被各个项目在引用。突然某一天发现日志很多报错。一看是NoSuchMethodError,意思是Dis...

你读过Mybatis的源码?说说它用到了几种设计模式

学习设计模式时,很多人都有类似的困扰——明明概念背得滚瓜烂熟,一到写代码就完全想不起来怎么用。就像学了一堆游泳技巧,却从没下过水实践,很难真正掌握。其实理解一个知识点,就像看立体模型,单角度观察总...

golang对接阿里云私有Bucket上传图片、授权访问图片

1、为什么要设置私有bucket公共读写:互联网上任何用户都可以对该Bucket内的文件进行访问,并且向该Bucket写入数据。这有可能造成您数据的外泄以及费用激增,若被人恶意写入违法信息还可...

spring中的资源的加载(spring加载原理)

最近在网上看到有人问@ContextConfiguration("classpath:/bean.xml")中除了classpath这种还有其他的写法么,看他的意思是想从本地文件...

Android资源使用(android资源文件)

Android资源管理机制在Android的开发中,需要使用到各式各样的资源,这些资源往往是一些静态资源,比如位图,颜色,布局定义,用户界面使用到的字符串,动画等。这些资源统统放在项目的res/独立子...

如何深度理解mybatis?(如何深度理解康乐服务质量管理的5个维度)

深度自定义mybatis回顾mybatis的操作的核心步骤编写核心类SqlSessionFacotryBuild进行解析配置文件深度分析解析SqlSessionFacotryBuild干的核心工作编写...

@Autowired与@Resource原理知识点详解

springIOCAOP的不多做赘述了,说下IOC:SpringIOC解决的是对象管理和对象依赖的问题,IOC容器可以理解为一个对象工厂,我们都把该对象交给工厂,工厂管理这些对象的创建以及依赖关系...

java的redis连接工具篇(java redis client)

在Java里,有不少用于连接Redis的工具,下面为你介绍一些主流的工具及其特点:JedisJedis是Redis官方推荐的Java连接工具,它提供了全面的Redis命令支持,且...