前提:nginx 一般安装路径
/usr/sbin/nginx,也有的在/usr/local/nginx/sbin/nginx可以用whereis nginx查看程序路径
一、nginx 原生命令(直接执行 nginx 二进制)
# 启动 nginx
nginx
# 停止(优雅停止,处理完现有连接再退出)
nginx -s quit
# 强制立刻停止
nginx -s stop
# 重载配置(修改 nginx.conf 后必须执行,不中断业务)
nginx -s reload
# 测试配置文件语法是否正确(非常重要!改配置先测)
nginx -t
# 测试配置并打印详细配置路径
nginx -T
# 查看 nginx 版本
nginx -v
# 查看版本+编译参数
nginx -V
二、systemd 系统服务命令(CentOS7+/Ubuntu16+,主流)
大多数系统安装完会注册 nginx.service,推荐日常用这套:
# 启动
systemctl start nginx
# 停止
systemctl stop nginx
# 重启(会短暂断流)
systemctl restart nginx
# 重载配置(平滑生效,业务不中断,优先用这个,不要 restart)
systemctl reload nginx
# 设置开机自启
systemctl enable nginx
# 取消开机自启
systemctl disable nginx
# 查看运行状态
systemctl status nginx
# 查看 nginx 日志
journalctl -u nginx -f
三、查找关键文件路径
# 找 nginx 主程序
whereis nginx
# 查看主配置文件位置
nginx -t
# 默认主配置
cat /etc/nginx/nginx.conf
# 站点配置目录(很多版本放在这里)
ls /etc/nginx/conf.d/
# 查看 nginx 进程
ps -ef | grep nginx
# 查看端口监听
ss -tlnp | grep nginx
netstat -tlnp | grep nginx
四、日志操作
# 访问日志
tail -f /var/log/nginx/access.log
# 错误日志(排错重点)
tail -f /var/log/nginx/error.log
✅ 日常标准操作流程(改虚拟主机 / 反向代理)
修改配置文件
nginx -t校验语法,确保 successsystemctl reload nginx平滑重载,不要直接 restart
❗注意:
restart会断开现有连接;生产环境修改配置一律用reload。
常见排错
nginx -t报错:配置文件语法写错,看报错行号修改启动失败:端口被占用 80/443,检查其他程序占用
status nginx 报错:看
error.log获取真实原因
本文原创作者:易君召,详见:https://www.yijunzhao.cc/about,转载请注明出处。
原文链接
欢迎访问