使用 nginx 配置项的好习惯
目录
❓ 问题二、nginx: [emerg] unexpected "==" in condition in
❓ 问题三、nginx: [emerg] unknown "request_url" variable
前言
工作久了之后就会发现,使用任何技术栈和技术框架都需要具备一个良好的使用习惯。今天,本人就来分享一个关于 nginx 使用的好习惯。
正文
在使用 nginx 的过程中,我们经常操作的内容就是修改 nginx 配置文件。由于 nginx 缺少统一的后台管理界面,在这个过程中会很容易写错语法和配置项。如果每次都靠启动进程或者服务来验证,成本和开销都比较大。因此,我们要养成一个良好的使用习惯避免类似的问题。一般来说,nginx 以单体进程或者容器服务的形式存在。接下来,我们以单一进程为例进行说明。
第一步、语法检查
我们修改完配置后,先检查语法是否正确。检查命令如下:
/usr/sbin/nginx -t
如果输出如下信息就表示配置正确,我们就可以重启 nginx 进程了:
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
但是,需要注意的是语法检查通过并不意味着进程就可以正常重启,有可能会遇到执行问题,比如端口占用,磁盘空间不够等问题。
第二步、重启进程或服务
语法检查通过后,重启 nginx 进程。重启命令如下:
/usr/sbin/nginx -s reload
第三步、检查进程或者服务状态
最后,查看 nginx 进程启动情况,命令如下:
ps -aux | grep nginx
输出结果:
root 7019 0.0 0.0 126172 5528 ? Ss 2021 0:00 nginx: master process nginxwww-data 19151 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19152 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19153 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19154 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19155 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19156 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19157 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19158 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19159 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19160 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19161 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19162 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19163 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19164 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19165 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processwww-data 19166 0.0 0.0 126172 3572 ? S 18:12 0:00 nginx: worker processroot 19168 0.0 0.0 14220 1060 pts/0 S+ 18:12 0:00 grep --color=auto nginx
输出上述结果说明进程已经正常启动了。
常见问题
❓ 问题一、Address already in use
如果出现如下报错信息:
2022/05/14 09:57:48 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)2022/05/14 09:57:48 [emerg] 1#1: bind() to 0.0.0.0:443 failed (98: Address already in use)nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)2022/05/14 09:57:48 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)2022/05/14 09:57:48 [emerg] 1#1: bind() to 0.0.0.0:443 failed (98: Address already in use)nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)2022/05/14 09:57:48 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)2022/05/14 09:57:48 [emerg] 1#1: bind() to 0.0.0.0:443 failed (98: Address already in use)nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)2022/05/14 09:57:48 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)2022/05/14 09:57:48 [emerg] 1#1: bind() to 0.0.0.0:443 failed (98: Address already in use)nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)2022/05/14 09:57:48 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)2022/05/14 09:57:48 [emerg] 1#1: bind() to 0.0.0.0:443 failed (98: Address already in use)nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)2022/05/14 09:57:48 [emerg] 1#1: still could not bind()nginx: [emerg] still could not bind()
大概率是进程或者容器服务重复启动了,可以查看对应的端口和进程服务是不是已经存在了。
❓ 问题二、nginx: [emerg] unexpected "==" in condition in
很明显是语法错误,配置文件通过单“=”号用于判断,而不是双“==”号。
❓ 问题三、nginx: [emerg] unknown "request_url" variable
很明显是语法错误,猜测你是想用“request_uri”。
作者简介:😄大家好,我是 Data-Mining(liuzhen007),是一位典型的音视频技术爱好者,前后就职于传统广电巨头和音视频互联网公司,具有丰富的音视频直播和点播相关经验,对 WebRTC、FFmpeg 和 Electron 有非常深入的了解,😄公众号:玩转音视频。同时也是 CSDN 博客专家(博客之星)、华为云享专家(共创编辑、十佳博主)、51CTO 社区编辑、InfoQ 签约作者,欢迎关注我分享更多干货!😄
版权声明: 本文为 InfoQ 作者【liuzhen007】的原创文章。
原文链接:【http://xie.infoq.cn/article/35e7d02ca3682065732a43bbd】。文章转载请联系作者。
评论