编译 OpenResty 并集成 VTS 模块

编译 OpenResty 并集成 VTS 模块

OpenResty 默认安装包不包含 VTS(Virtual Host Traffic Status)模块,如果你需要监控每个虚拟主机的流量、连接数、响应状态码等指标,就需要重新编译 OpenResty 并集成 VTS。

准备编译环境

以下步骤在 CentOS 7 容器中进行:

1
docker run --name=openresty_centos7 -itd -v ~/centos7:/root centos:7

创建编译用户并安装依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
sudo useradd makerpm
sudo groupadd mock
sudo usermod -a -G mock makerpm
sudo passwd makerpm
sudo su - makerpm

yum install epel-release
yum install -y gcc gcc-c++ make pcre zlib openssl \
pcre-devel zlib-devel openssl-devel curl wget git \
rpm-build redhat-rpm-config rpmdevtools yum-utils \
perl perl-Data-Dumper libtool ElectricFence \
systemtap-sdt-devel valgrind-devel ccache \
perl-ExtUtils-MakeMaker

添加 OpenResty 官方源并安装其依赖库:

1
2
3
4
5
6
7
wget https://openresty.org/package/centos/openresty.repo
mv openresty.repo /etc/yum.repos.d/
yum check-update
yum install -y openresty-zlib-asan-devel openresty-pcre-asan-devel \
openresty-zlib-devel openresty-openssl-debug-devel \
openresty-pcre-devel openresty-openssl111-devel \
openresty-openssl111-debug-devel openresty-openssl111-asan-devel

下载源码与打包工具

1
2
3
4
5
6
7
8
9
git clone https://github.com/openresty/openresty-packaging.git

mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros
cp ~/openresty-packaging/rpm/SOURCES/* ~/rpmbuild/SOURCES/
cp ~/openresty-packaging/rpm/SPECS/*.spec ~/rpmbuild/SPECS

wget https://openresty.org/download/openresty-1.19.9.1.tar.gz
mv openresty-1.19.9.1.tar.gz ~/rpmbuild/SOURCES/

集成 VTS 模块

下载 VTS 源码并修改 SPEC 文件:

1
2
3
4
5
git clone https://github.com/vozlt/nginx-module-vts.git
mv nginx-module-vts /usr/local

cd ~/rpmbuild/SPECS
vi openresty.spec

configure 参数中加入 VTS 模块路径:

1
--add-module=/usr/local/nginx-module-vts \

注意:等号前后不要加空格,否则会导致编译错误。

编译 RPM

1
2
spectool -g -R openresty.spec
rpmbuild -ba openresty.spec

编译完成后,RPM 包位于 ~/rpmbuild/RPMS/x86_64/

1
openresty-1.19.9.1-6.el7.x86_64.rpm

部署与配置

备份现有配置后安装:

1
2
yum remove openresty
rpm -ivh ~/rpmbuild/RPMS/x86_64/openresty-*.rpm

nginx.conf 中启用 VTS:

1
2
3
4
5
6
7
8
9
10
http {
vhost_traffic_status_zone;

server {
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
}

重启 OpenResty 后访问 http://your-server/status 即可看到各虚拟主机的实时流量统计。

源码编译方式(可选)

如果不想打 RPM,也可以直接源码编译:

1
2
3
4
5
6
7
8
9
10
11
12
./configure \
--prefix=/home/deployer/app/openresty \
--with-pcre=../pcre-8.40 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-OpenSSL_1_0_2i \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-stream \
--add-module=../nginx-module-vts

make
make install

小结

OpenResty 官方 RPM 不包含 VTS,需要手动修改 SPEC 文件重新打包。核心步骤:准备环境 → 下载 VTS → 修改 SPEC 加 --add-module → 编译 → 部署配置。VTS 提供的 /status 页面是排查线上流量问题的利器。