1. 下载 Nginx 源码包
从 Nginx 官方网站 下载最新版本的源码包。
例如,下载 Nginx 1.26.1:
wget https://nginx.org/download/nginx-1.25.1.tar.gz
2. 解压源码包
使用以下命令解压下载的源码包:
tar -xzvf nginx-1.26.1.tar.gz
解压后会生成一个目录,例如 nginx-1.26.1
。
3. 安装依赖
在编译 Nginx 之前,需要确保系统已安装必要的依赖工具和库。
在 CentOS/RHEL 上:
yum groupinstall "Development Tools"
yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libxslt libxslt-devel
4. 启用所有内置模块
Nginx 的内置模块可以通过 ./configure
命令的选项启用。你可以使用 --with-<module>
来启用特定模块,或者使用 --add-module
来添加第三方模块。
查看所有内置模块
运行以下命令查看所有可用的内置模块:
./configure --help | grep with-
启用所有内置模块(根据需求启用)
以下是一个示例命令,启用了大多数常见的内置模块:
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_ssl_preread_module \
--with-threads \
--with-file-aio \
--with-http_perl_module \
--with-debug
我这里只安装几个
待会需要这个stub_status模块监控nginx指标
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module
5. 添加第三方模块(可选)
Nginx 支持通过 --add-module
选项添加第三方模块。以下是一些常用的第三方模块:
1. ngx_brotli(Brotli 压缩模块)
-
下载模块源码:
git clone https://github.com/google/ngx_brotli.git cd ngx_brotli git submodule update --init cd ..
-
在
./configure
中添加模块:--add-module=./ngx_brotli
2. ngx_cache_purge(缓存清理模块)
-
下载模块源码:
git clone https://github.com/FRiCKLE/ngx_cache_purge.git
-
在
./configure
中添加模块:--add-module=./ngx_cache_purge
6. 编译和安装
-
编译 Nginx:
make
-
安装 Nginx:
make install
7. 验证安装
-
检查 Nginx 版本和模块:
/usr/local/nginx/sbin/nginx -V
-
修改nginx配置文件
添加这一段,获取nginx指标

-
启动 Nginx:
/usr/local/nginx/sbin/nginx
-
访问 Nginx 默认页面:
打开浏览器,访问
http://<服务器IP>/nginx_status
,如果看到 Nginx 欢迎页面,说明安装成功。

8. 设置开机自启
为了让 Nginx 在系统启动时自动运行,可以将其配置为系统服务。
-
创建系统服务文件:
vi /etc/systemd/system/nginx.service
-
添加以下内容:
[Unit] Description=The Nginx HTTP and reverse proxy server After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
-
启用并启动服务:
systemctl enable nginx systemctl start nginx
接下来安装nginx-exporter

解压并移动目录
tar -zxvf nginx-prometheus-exporter_1.4.1_linux_amd64.tar.gz
mv nginx-prometheus-exporter /usr/local/nginx-exporter
使用以下命令启动nginx-export
--web.listen-address=":9903" 表示nginx-export监听9903端口
-nginx.scrape-uri=http://127.0.0.1:80/nginx_status 表示nginx-export监听nginx的地址
cd /usr/local/prometheus/nginx-exporter && nohup /usr/local/prometheus/nginx-exporter/nginx-prometheus-exporter --web.listen-address=":9903" -nginx.scrape-uri=http://127.0.0.1:80/nginx_status &
修改prometheus配置文件,添加nginx监控信息

- job_name: 'Nginx'
file_sd_configs:
- files:
- /usr/local/prometheus/nginx_exporter_targets.json
refresh_interval: 5m # 每隔 5 分钟重新加载一次文件
创建监控nginx的json
[root@prometheus prometheus]# cat /usr/local/prometheus/nginx_exporter_targets.json
[
{
"targets": ["192.168.158.183:9903"],
"labels": {
"project": "beijing",
"env": "prod"
}
}
]
kill掉prometheus进程

重新启动
cd /usr/local/prometheus/ && nohup /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml &
查看prometheus的web界面发现已经监控上了

添加grafana监控面板



这个面板适合于通过 Nginx Exporter 暴露的指标。支持显示存活状态、活动连接数、请求速率、错误率等。