Nginx - 常用操作
linux環境
- 重啟 Nginx
nginx -s reload - 快速關閉 Nginx,不保存相關信息
nginx -s stop - 平穩關閉 Nginx,保存相關信息 nginx -s quit
- 測試 Nginx nginx -t
- 為 Nginx 指定一個配置文件,啟動一個 nginx process nginx -c file_name
- 重新開啟日誌
nginx -s reopen - 查詢 Nginx 服務狀態
sudo service nginx status sudo service nginx restart sudo service nginx start - 測試配置文件
sudo nginx -t -c /etc/nginx/nginx.conf - 啟動指定nginx.conf文件 (前端項目啟動)
nginx -c /path/to/nginx.conf - 查詢 nginx 進程訊息 ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|pid)'
macos環境
- 查看 nginx 資訊
brew info nginx - 創建連結cmd
ln -s /usr/local/Cellar/nginx/1.23.1/bin/nginx /usr/local/sbin/nginx
問題排查 MacOS
- macos service 運行的背景服務 brew services list
- 用htop 查詢nginx process,kill process kill pid
- 查詢nginx process id ps -ef | grep nginx
nginx: [error] invalid PID number "" in "/run/nginx.pid"
- sudo nginx -c /etc/nginx/nginx.conf
nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/
- 刪除 /etc/nginx/sites-available/default
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
fuser -k 80/tcp
ps -A | grep nginx
kill -9 pid1
kill -9 pid2
sudo netstat -tulpn
open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory) 編輯 /etc/nginx/nignx.cnf
pid /logs/nginx.pid -> /usr/local/nginx/logs/nginx.pid;
創建 /usr/local/nginx/logs;
nginx -s reload