nginx: [emerg] unknown directive “stub_status“ in /app/nginx/conf/conf.d/state.conf:7

本文介绍了解决Nginx编译安装后无法正常使用stub_status的问题,通过重新配置和编译安装Nginx,并逐步替换运行中的进程,最终实现了网站实时访问数据的正常获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 发现问题

编译安装Nginx后,想查看网站实时访问数据时,增加参数后报错。
编译安装的文章链接:Nginx编译安装过程

[root@web01 conf.d]# cat state.conf 
server {
       listen        80;
       server_name   state.oldboy.com;
       location  / {
         root  /app/nginx/html/www;
         index index.html;
         stub_status on;
       }
}

在语法检查时,报错如下

[root@web01 conf.d]# nginx -t
nginx: [emerg] unknown directive "stub_status" in /app/nginx/conf/conf.d/state.conf:7
nginx: configuration file /app/nginx-1.16.0/conf/nginx.conf test failed

Nginx编译安装时参数如下

[root@web01 conf.d]# nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
configure arguments: --user=nginx --group=nginx --prefix=/app/nginx-1.16.0

二 解决问题

在网上查找相关数据并对照后,猜测编译时可能少加了几个模块。于是重新编译安装。具体步骤如下:

1)找到编译安装时的nginx软件包,解压

[root@web01 tools]# pwd
/server/tools
[root@web01 tools]# ll
total 1012
drwxr-xr-x 9 nginx nginx     186 Jun 26 19:49 nginx-1.16.0
-rw-r--r-- 1 root  root  1032345 Jul 10  2021 nginx-1.16.0.tar.gz
[root@web01 tools]# cd nginx-1.16.0/
[root@web01 nginx-1.16.0]# ll
total 752
drwxr-xr-x 6 nginx nginx    326 Jun 26 19:31 auto
-rw-r--r-- 1 nginx nginx 296223 Apr 23  2019 CHANGES
-rw-r--r-- 1 nginx nginx 451813 Apr 23  2019 CHANGES.ru
drwxr-xr-x 2 nginx nginx    168 Jun 26 19:31 conf
-rwxr-xr-x 1 nginx nginx   2502 Apr 23  2019 configure
drwxr-xr-x 4 nginx nginx     72 Jun 26 19:31 contrib
drwxr-xr-x 2 nginx nginx     40 Jun 26 19:31 html
-rw-r--r-- 1 nginx nginx   1397 Apr 23  2019 LICENSE
-rw-r--r-- 1 root  root     380 Jul  2 12:10 Makefile
drwxr-xr-x 2 nginx nginx     21 Jun 26 19:31 man
drwxr-xr-x 3 root  root     174 Jul  2 12:12 objs
-rw-r--r-- 1 nginx nginx     49 Apr 23  2019 README
drwxr-xr-x 9 nginx nginx     91 Jun 26 19:31 src

2)配置相关参数

[root@web01 nginx-1.16.0]# ./configure --user=nginx --group=nginx --prefix=/app/nginx-1.16.0 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_slice_module

3)编译,安装

[root@web01 nginx-1.16.0]# make && make install

4)创建软链接和环境变量

[root@web01 tools]# ln -s /app/nginx-1.16.0 /app/nginx
[root@web01 conf.d]# tail -1 /etc/profile
export PATH=/app/nginx/sbin:$PATH
[root@web01 conf.d]# source /etc/profile

5)确保配置文件正确

[root@web01 nginx-1.16.0]# nginx -t
nginx: the configuration file /app/nginx-1.16.0/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx-1.16.0/conf/nginx.conf test is successful

6)查看nginx进程

[root@web01 conf.d]# ps -ef|grep nginx
root      10864      1  0 Jul01 ?        00:00:00 nginx: master process /app/nginx/sbin/nginx
nginx     18057  10864  0 12:25 ?        00:00:00 nginx: worker process
root      18246  15116  0 13:37 pts/1    00:00:00 grep --color=auto nginx

7)发送USR2信号,Nginx会启动一个新版本的master进程和对应工作进程。

[root@web01 conf.d]# kill -USR2 10864
[root@web01 conf.d]# ps aux|grep nginx
root      10864  0.0  0.1  21256  1508 ?        Ss   Jul01   0:00 nginx: master process /app/nginx/sbin/nginx
nginx     18057  0.0  0.1  21256  1732 ?        S    12:25   0:00 nginx: worker process
root      18259  0.1  0.3  45956  3240 ?        S    13:38   0:00 nginx: master process /app/nginx/sbin/nginx
nginx     18260  0.0  0.1  46336  1904 ?        S    13:38   0:00 nginx: worker process
root      18265  0.0  0.0 112704   960 pts/1    R+   13:39   0:00 grep --color=auto nginx

8)向旧的Nginx主进程(master)发送WINCH信号,它会逐步关闭自己的工作进程。

[root@web01 conf.d]# kill -WINCH 10864
[root@web01 conf.d]# ps aux|grep nginx
root      10864  0.0  0.1  21256  1508 ?        Ss   Jul01   0:00 nginx: master process /app/nginx/sbin/nginx
root      18259  0.0  0.3  45956  3240 ?        S    13:38   0:00 nginx: master process /app/nginx/sbin/nginx
nginx     18260  0.0  0.1  46336  1904 ?        S    13:38   0:00 nginx: worker process
root      18271  0.0  0.0 112704   960 pts/1    R+   13:41   0:00 grep --color=auto nginx

9)发送QUIT信息

[root@web01 conf.d]# kill -QUIT 10864
[root@web01 conf.d]# ps aux|grep nginx
root      18259  0.0  0.3  45956  3240 ?        S    13:38   0:00 nginx: master process /app/nginx/sbin/nginx
nginx     18260  0.0  0.1  46336  1904 ?        S    13:38   0:00 nginx: worker process
root      18273  0.0  0.0 112704   960 pts/1    S+   13:41   0:00 grep --color=auto nginx

10)检查相关配置文件和资源是否正确

[root@web01 conf.d]# cat state.conf 
server {
       listen        80;
       server_name   state.oldboy.com;
       location  / {
         root  /app/nginx/html/www;
         index index.html;
         stub_status on;
       }
}

检查站点目录

[root@web01 www]# pwd
/app/nginx/html/www
[root@web01 www]# ll
total 40
-rw-r--r-- 1 root  root 20008 Jun 30 21:38 403.webp
-rw-r--r-- 1 root  root  4362 Jun 30 21:38 404.webp
-rw-r--r-- 1 root  root     0 Jun 30 21:21 a.php
-rw-r--r-- 1 root  root     4 Jun 30 21:17 a.txt
-rw------- 1 nginx root    45 Jun 28 09:50 htpasswd
-rw-r--r-- 1 root  root    31 Jul  2 12:25 index.html
-rw-r--r-- 1 root  root     0 Jun 30 21:26 你好.php
-rw-r--r-- 1 root  root     0 Jun 30 21:26 你好.txt
[root@web01 www]# cat index.html 
10.0.0.7 www.oldboy.com
123456

11)进行访问测试,查看stub_status是否可用

[root@web01 www]# curl state.oldboy.com
Active connections: 1 
server accepts handled requests
 7 7 6 
Reading: 0 Writing: 1 Waiting: 0 

测试成功,的确是编译安装时模块参数没有写全所导致。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值