操作环境

  • 硬件:华为云服务器
  • 操作系统:Rocky Linux 9.0 64bit
  • 服务器软件:nginx 1.24.0,php 8.2.6,openssl 3.0.7,WordPress6.2.2
  • 客户端软件:MobaXterm_Personal 23.1

问题描述

最近,我在调试网站修改nginx.conf后,用指令 systemctl restart nginx重启nginx服务失败,用指令 systemctl status nginx查看发现 nginx: [emerg] unknown directive "?" in /usr/local/nginx/conf/nginx.conf:169的报错信息。

详情如下:

[root@hecs-288529 conf]# systemctl restart nginx
[2023-06-02 21:01:38]  
Job for nginx.service failed because the control process exited with error code.
[2023-06-02 21:01:38]  See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.
[2023-06-02 21:01:38]  [root@hecs-288529 conf]# systemctl status  nginx
[2023-06-02 21:01:47]  

?nginx.service - nginx
[2023-06-02 21:01:47]       Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
[2023-06-02 21:01:47]      Drop-In: /usr/lib/systemd/system/nginx.service.d
[2023-06-02 21:01:47]               php-fpm.conf
[2023-06-02 21:01:47]       Active: failed (Result: exit-code) since Fri 2023-06-02 21:01:38 CST; 9s ago
[2023-06-02 21:01:47]     Duration: 12min 43.518s
[2023-06-02 21:01:47]      Process: 20110 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=1/FAILURE)
[2023-06-02 21:01:47]          CPU: 5ms
[2023-06-02 21:01:47]  
[2023-06-02 21:01:47]  Jun 02 21:01:38 hecs-288529 systemd[1]: Starting nginx...
[2023-06-02 21:01:47]  Jun 02 21:01:38 hecs-288529 nginx[20110]: nginx: [emerg] unknown directive "?" in /usr/local/nginx/conf/nginx.conf:169
[2023-06-02 21:01:47]  Jun 02 21:01:38 hecs-288529 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
[2023-06-02 21:01:47]  Jun 02 21:01:38 hecs-288529 systemd[1]: nginx.service: Failed with result 'exit-code'.
[2023-06-02 21:01:47]  Jun 02 21:01:38 hecs-288529 systemd[1]: Failed to start nginx.
[2023-06-02 21:01:47]  

解决方案

经仔细检查nginx.conf文件内容,没有发现问题。

 

怀疑是文件编码格式问题,用SSH客户端软件MobaXterm_Personal默认的编辑器重新打开文件,调整文件格式为 UNIX,编码修改为 ANSI后问题解决。

修改文件格式
修改文件编码