使用 nginx 配置项的好习惯

举报
liuzhen007 发表于 2022/05/26 22:41:27 2022/05/26
【摘要】 目录前言正文第一步、语法检查第二步、重启进程或服务第三步、检查进程或者服务状态常见问题❓ 问题一、Address already in use❓ 问题二、nginx: [emerg] unexpected "==" in condition in❓ 问题三、nginx: [emerg] unknown "request_url" variable前言工作久了之后就会发现,使用任何技术栈和技术...

目录


前言

正文

第一步、语法检查

第二步、重启进程或服务

第三步、检查进程或者服务状态

常见问题

❓ 问题一、Address already in use

❓ 问题二、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 ok
nginx: 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 nginx
www-data 19151  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19152  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19153  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19154  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19155  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19156  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19157  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19158  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19159  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19160  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19161  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19162  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19163  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19164  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19165  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
www-data 19166  0.0  0.0 126172  3572 ?        S    18:12   0:00 nginx: worker process
root     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 签约作者,欢迎关注我分享更多干货!😄 

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200