操作环境

  • 硬件:华为云服务器
  • 操作系统:Rocky Linux 9.0 64bit
  • 服务器软件:nginx 1.24.0,wordpress 6.2.2
  • wordpress插件:W3 Total Cache 2.3.2


问题描述


wordpress安装插件W3 Total Cache 2.3.2并开启压缩后,根据插件提示重启nginx,发现nginx报错且无法启动。报错信息为
“gzip” directive is not allowed here in /usr/local/nginx/conf/nginx.conf:151。

1.用命令systemctl restart nginx重启nginx报错信息

错误信息如下:


[root@hecs-288529 ~]# systemctl restart nginx
[2023-05-28 23:51:10]
Job for nginx.service failed because the control process exited with error code.
[2023-05-28 23:51:10] See “systemctl status nginx.service” and “journalctl -xeu nginx.service” for details.

2.用命令systemctl status nginx.service查看服务状态


说明:此命令中的.service可以省略。

命令systemctl status nginx.service输出信息:


[2023-05-28 23:51:54] [root@hecs-288529 ~]# systemctl status nginx.service
[2023-05-28 23:52:00] Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
[2023-05-28 23:52:00] Drop-In: /usr/lib/systemd/system/nginx.service.d
[2023-05-28 23:52:00] php-fpm.conf
[2023-05-28 23:52:00] Active: faile (Result: exit-code) since Sun 2023-05-28 23:51:10 CST; 49s ago
[2023-05-28 23:52:00] Duration: 26min 48.651s
[2023-05-28 23:52:00] Process: 2095 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=1/FAILURE)
[2023-05-28 23:52:00] CPU: 9ms
[2023-05-28 23:52:00]
[2023-05-28 23:52:00] May 28 23:51:10 hecs-288529 systemd[1]: Starting nginx…
[2023-05-28 23:52:00] May 28 23:51:10 hecs-288529 nginx[2095]: nginx: [emerg] “gzip” directive is not allowed here in /usr/local/nginx/conf/nginx.conf:151
[2023-05-28 23:52:00] May 28 23:51:10 hecs-288529 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
[2023-05-28 23:52:00] May 28 23:51:10 hecs-288529 systemd[1]: nginx.service: Failed with result ‘exit-code’.
[2023-05-28 23:52:00] May 28 23:51:10 hecs-288529 systemd[1]: Failed to start nginx.

3.用命令journalctl -xeu nginx.service查看


[root@hecs-288529 ~]# journalctl -xeu nginx.service
[2023-05-28 23:52:48] The process’ exit code is ‘exited’ and its exit status is 1.
[2023-05-28 23:52:48] May 28 23:51:10 hecs-288529 systemd[1]: nginx.service: Failed with result ‘exit-code’.
[2023-05-28 23:52:48] Subject: Unit failed
[2023-05-28 23:52:48] Defined-By: systemd
[2023-05-28 23:52:48] Support: https://access.redhat.com/support
[2023-05-28 23:52:48]
[2023-05-28 23:52:48] The unit nginx.service has entered the ‘failed’ state with result ‘exit-code’.
[2023-05-28 23:52:48] May 28 23:51:10 hecs-288529 systemd[1]: Failed to start nginx.
[2023-05-28 23:52:48] Subject: A start job for unit nginx.service has failed
[2023-05-28 23:52:48] Defined-By: systemd
[2023-05-28 23:52:48] Support: https://access.redhat.com/support
[2023-05-28 23:52:48]
[2023-05-28 23:52:48] A start job for unit nginx.service has finished with a failure.
[2023-05-28 23:52:48]
[2023-05-28 23:52:48] The job identifier is 892 and the job result is failed.
[2023-05-28 23:52:48] May 28 23:52:19 hecs-288529 systemd[1]: Starting nginx…
[2023-05-28 23:52:48] Subject: A start job for unit nginx.service has begun execution
[2023-05-28 23:52:48] Defined-By: systemd
[2023-05-28 23:52:48] Support: https://access.redhat.com/support
[2023-05-28 23:52:48]
[2023-05-28 23:52:48] A start job for unit nginx.service has begun execution.
[2023-05-28 23:52:48]
[2023-05-28 23:52:48] The job identifier is 957.
[2023-05-28 23:52:48] May 28 23:52:19 hecs-288529 nginx[2106]: nginx: [emerg] “gzip” directive is not allowed here in /usr/local/nginx/conf/nginx.conf:151
[2023-05-28 23:52:48] May 28 23:52:19 hecs-288529 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
[2023-05-28 23:52:48] Subject: Unit process exited
[2023-05-28 23:52:48] Defined-By: systemd
[2023-05-28 23:52:48] Support: https://access.redhat.com/support
[2023-05-28 23:52:48]
[2023-05-28 23:52:48] An ExecStart= process belonging to unit nginx.service has exited.
[2023-05-28 23:52:48]
[2023-05-28 23:52:48] The process’ exit code is ‘exited’ and its exit status is 1.
[2023-05-28 23:52:48] May 28 23:52:19 hecs-288529 systemd[1]: nginx.service: Failed with result ‘exit-code’.
[2023-05-28 23:52:48] Subject: Unit failed
[2023-05-28 23:52:48] Defined-By: systemd
[2023-05-28 23:52:48] Support: https://access.redhat.com/support
[2023-05-28 23:52:48]
[2023-05-28 23:52:48] The unit nginx.service has entered the ‘failed’ state with result ‘exit-code’.
[2023-05-28 23:52:48] May 28 23:52:19 hecs-288529 systemd[1]: Failed to start nginx.
[2023-05-28 23:52:48] Subject: A start job for unit nginx.service has failed
[2023-05-28 23:52:48] Defined-By: systemd
[2023-05-28 23:52:48] Support: https://access.redhat.com/support
[2023-05-28 23:52:48]
[2023-05-28 23:52:48] A start job for unit nginx.service has finished with a failure.
[2023-05-28 23:52:48]
[2023-05-28 23:52:48] The job identifier is 957 and the job result is failed.
[2023-05-28 23:52:48] lines 117-162/162 (END)

lines 117-162/162 (END)


4.查看nginx.conf配置信息


发现配置文件尾部被W3 Total Cache插件自动添加了以下内容,第二行gzip on;的行号是151。

#BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon application/json application/vnd.ms-access video/webm application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint image/svg+xml application/x-shockwave-flash image/tiff application/x-font-ttf audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel;
location ~ .(css|htc|less|js|js2|js3|js4)$ {
expires 31536000s;
etag on;
if_modified_since exact;
try_files $uri $uri/ /index.php?$args;
}
location ~ .(html|htm|rtf|rtx|txt|xsd|xsl|xml)$ {
etag on;
if_modified_since exact;
try_files $uri $uri/ /index.php?$args;
}
location ~ .(asf|asx|wax|wmv|wmx|avi|avif|avifs|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|webp|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|webm|mpp|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|_ttf|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
expires 31536000s;
etag on;
if_modified_since exact;
try_files $uri $uri/ /index.php?$args;
}
add_header Referrer-Policy “no-referrer-when-downgrade”;

#END W3TC Browser Cache

5.检查nginx的gzip模块安装情况


先用命令cd /usr/local/nginx/sbin进入安装目录下sbin子目录(安装目录不同的,请自行修改),再用命令./nginx -V查看配置参数(注意:此处要用大写V),发现没有配置gzip模块,问题找到。

[2023-05-29 00:03:38] [root@hecs-288529 ~]# cd /usr/local/nginx/sbin
[root@hecs-288529 sbin]# ls
[2023-05-29 00:04:10]
nginx nginx.old
[2023-05-29 00:04:10] [root@hecs-288529 sbin]# ./nginx -v
[2023-05-29 00:04:18]
nginx version: nginx/1.24.0
[2023-05-29 00:04:18] [root@hecs-288529 sbin]# ./nginx -V
[2023-05-29 00:04:24]
nginx version: nginx/1.24.0
[2023-05-29 00:04:24] built by gcc 11.3.1 20221121 (Red Hat 11.3.1-4) (GCC)
[2023-05-29 00:04:24] built with OpenSSL 3.0.7 1 Nov 2022
[2023-05-29 00:04:24] TLS SNI support enabled
[2023-05-29 00:04:24] configure arguments: –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –user=nginx –group=nginx


解决方法


1.重新配置nginx安装参数


先用命令cd进入nginx源代码目录,本机为/usr/local/src/nginx-1.24.0,再用命令configure重新配置参数,增加gzip模块,详细指令如下。

[2023-05-29 00:06:14] [root@hecs-288529 local]# cd src
[2023-05-29 00:06:18]
[root@hecs-288529 src]# ls
[2023-05-29 00:06:20]
mysql80-community-release-el9-1.noarch.rpm nginx-1.24.0.tar.gz redis-5.3.7 redis-7.2-rc2
[2023-05-29 00:06:20] nginx-1.24.0 package.xml redis-5.3.7.tgz redis-7.2-rc2.tar.gz
[2023-05-29 00:06:20] [root@hecs-288529 src]# cd nginx-1.24.0
[2023-05-29 00:06:34]
[root@hecs-288529 nginx-1.24.0]# ls
[2023-05-29 00:06:35]
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[2023-05-29 00:06:35] [root@hecs-288529 nginx-1.24.0]# ./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –user=nginx –group=nginx –with-http_gzip_static_module


2.重新编译安装


用命令make && make install 重新编译安装。

3.重新启动nginx服务


用命令reboot重启服务器(也可用命令systemctl restart nginx重启nginx服务),再使用命令systemctl status nginx查看nginx服务状态。发现问题依旧。

[root@hecs-288529 ~]# systemctl status nginx
[2023-05-29 00:12:26]

?nginx.service – nginx
[2023-05-29 00:12:27] Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
[2023-05-29 00:12:27] Drop-In: /usr/lib/systemd/system/nginx.service.d
[2023-05-29 00:12:27] php-fpm.conf
[2023-05-29 00:12:27] Active: failed (Result: exit-code) since Mon 2023-05-29 00:11:39 CST; 47s ago
[2023-05-29 00:12:27] Process: 652 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=1/FAILURE)
[2023-05-29 00:12:27] CPU: 10ms
[2023-05-29 00:12:27]
[2023-05-29 00:12:27] May 29 00:11:39 hecs-288529 systemd[1]: Starting nginx…
[2023-05-29 00:12:27] May 29 00:11:39 hecs-288529 nginx[652]: nginx: [emerg] “gzip” directive is not allowed here in /usr/local/nginx/conf/nginx.conf:151
[2023-05-29 00:12:27] May 29 00:11:39 hecs-288529 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
[2023-05-29 00:12:27] May 29 00:11:39 hecs-288529 systemd[1]: nginx.service: Failed with result ‘exit-code’.
[2023-05-29 00:12:27] May 29 00:11:39 hecs-288529 systemd[1]: Failed to start nginx.
[2023-05-29 00:12:27]


4.修改nginx.conf配置文件


仔细检查nginx.conf配置文件,发现W3 Total Cache插件自动添加的内容所加的位置不对,它是直接添加在文件尾部,没有加到server字段的{}内。将内容移动到server字段的{}内,保存修改,重新启动nginx服务,恢复正常。再次使用命令systemctl status nginx查看nginx服务状态,输出如下。

[root@hecs-288529 ~]# systemctl status nginx
[2023-05-29 00:21:58]

nginx.service – nginx
[2023-05-29 00:21:58] Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
[2023-05-29 00:21:58] Drop-In: /usr/lib/systemd/system/nginx.service.d
[2023-05-29 00:21:58] php-fpm.conf
[2023-05-29 00:21:58] Active: active (running) since Mon 2023-05-29 00:21:54 CST; 3s ago
[2023-05-29 00:21:58] Process: 1704 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
[2023-05-29 00:21:58] Main PID: 1705 (nginx)
[2023-05-29 00:21:58] Tasks: 3 (limit: 23205)
[2023-05-29 00:21:58] Memory: 3.3M
[2023-05-29 00:21:58] CPU: 12ms
[2023-05-29 00:21:58] CGroup: /system.slice/nginx.service
[2023-05-29 00:21:58] 1705 “nginx: master process /usr/local/nginx/sbin/nginx”
[2023-05-29 00:21:58] 1706 “nginx: worker process”
[2023-05-29 00:21:58] 1707 “nginx: worker process”
[2023-05-29 00:21:58]
[2023-05-29 00:21:58] May 29 00:21:54 hecs-288529 systemd[1]: Starting nginx…
[2023-05-29 00:21:58] May 29 00:21:54 hecs-288529 systemd[1]: Started nginx.