Nginx命令大全

摘要

Nginx命令大全 Nginx 1. 版本与帮助 nginx -v # 简洁查看版本 nginx -V # 版本 + 编译参数(查看内置模块) nginx -h # 帮助信息 2. 配置检查 & 重载(生产最常用) nginx -t # 校...

Nginx命令大全

Nginx

1. 版本与帮助

nginx -v                            # 简洁查看版本
nginx -V                            # 版本 + 编译参数(查看内置模块)
nginx -h                            # 帮助信息

2. 配置检查 & 重载(生产最常用)

nginx -t                            # 校验配置语法
nginx -T                            # 校验配置,并输出全部生效配置(找配置路径神器)
nginx -s reload                     # 平滑重载,不中断连接【首选】
nginx -s quit                       # 优雅停止,等待连接处理完毕
nginx -s stop                       # 快速强制停止

3. systemctl 服务管理(Ubuntu / CentOS)

systemctl start nginx               # 启动
systemctl stop nginx                # 停止
systemctl restart nginx            # 重启
systemctl status nginx              # 查看运行状态
systemctl enable nginx              # 设置开机自启
systemctl disable nginx             # 取消开机自启

4. 日志查看(搭配Linux命令)

tail -f /var/log/nginx/access.log   # 实时访问日志
tail -f /var/log/nginx/error.log    # 实时错误日志
grep "502" /var/log/nginx/error.log # 过滤502报错
grep "404" /var/log/nginx/access.log# 过滤404

5. 信号方式控制(了解即可,优先 -s)

ps -ef | grep nginx                 # 拿到master进程PID
kill -HUP 主进程号                   # 等价 nginx -s reload
kill -QUIT 主进程号                 # 等价 nginx -s quit
kill -TERM 主进程号                 # 等价 nginx -s stop

6. 调试测试

curl -I [http://127.0.0.1](http://127.0.0.1)            # 只看响应头,校验转发、状态码
curl [http://127.0.0.1](http://127.0.0.1)

7. Docker 容器中 Nginx 操作

# 进入nginx容器
docker exec -it nginx bash
# 容器内校验配置
nginx -t
# 容器内平滑重载
nginx -s reload
# 复制容器内配置到宿主机
docker cp nginx:/etc/nginx/nginx.conf ./nginx.conf
# 宿主机配置传到容器
docker cp ./nginx.conf nginx:/etc/nginx/

8. 常用配置路径参考

主配置:/etc/nginx/nginx.conf
站点配置目录:/etc/nginx/conf.d/
日志目录:/var/log/nginx/

9. 常用配置片段(不是命令,写配置高频)

# 反向代理
proxy_pass [http://127.0.0.1:8080](http://127.0.0.1:8080);
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

# gzip压缩
gzip on;
gzip_types text/plain application/json text/css application/javascript;