Nginx命令最全详解(29个最常用命令)
Nginx是大型架构的必备技能,今天就重点详解Nginx常用命令@mikechen
作者:mikechen
文章来源:mikechen.cc
Nginx版本
查看 Nginx 版本:
nginx -v
查看详细版本信息、与编译参数:
nginx -V
查找 Nginx 安装路径:
which nginx
Nginx服务
启动 Nginx:(默认读取 /etc/nginx/nginx.conf)
nginx
强制停止(立即关闭所有进程)
nginx -s stop
优雅停止(处理完当前连接后关闭)
nginx -s quit
平滑重载配置(不影响正在处理的请求)
nginx -s reload
检查配置文件语法是否正确
nginx -t
检查指定配置文件语法
nginx -t -c /etc/nginx/nginx.conf
使用 systemd 管理 Nginx(推荐)
systemctl restart nginxsystemctl status nginx
Nginx配置
使用指定配置文件启动 Nginx:
nginx -c /usr/local/nginx/conf/nginx.conf
查看 Nginx 进程状态(master、 和 worker):
ps aux | grep nginx
查看端口监听状态(如:80/443)
netstat -tlnp | grep nginx
手动向主进程发送重载信号:
kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
Nginx管理
实时查看访问日志:
tail -f /var/log/nginx/access.log
实时查看错误日志:
tail -f /var/log/nginx/error.log
cat /var/log/nginx/error.log | grep [关键字]
排查错误关键词:
cat error.log | grep timeout
Nginx性能
使用 Apache Bench 压测 Nginx(n次并发c个):
ab -n 1000 -c 100 http://localhost/
查看响应头,确认 Nginx 正常工作:
curl -I http://localhost
让 Nginx 以前台方式运行(容器中常用):
nginx -g "daemon off;"
查看80端口被哪个进程占用:
lsof -i :80
Nginx高级
nginx -V 2>&1 | grep --color -o http_stub_status_module
检查是否编译了某模块:
nginx -V 2>&1 | grep --color -o http_ssl_module
指定工作目录运行(含日志/临时目录。。。等):
nginx -p /custom/nginx/
nginx -g "pid /tmp/nginx.pid;"
临时覆盖配置项运行 Nginx:
nginx -g "pid /tmp/nginx.pid; worker_processes 4;"
Nginx命令
查找配置文件:
find / -name nginx.conf
在容器中测试配置,是否正确:
docker exec nginx-container nginx -t
测试正向代理是否生效
curl -x localhost:8888 https://mikechen.cc
以上
本篇已收于mikechen原创超30万字《阿里架构师进阶专题合集》里面。