Nginx 兩大高頻運維操作:自定義錯誤頁重導與配置語法驗證/熱重載。本文提供極簡可用片段,無冗長理論,拿來即用。
error_page 404 = @homepage;
location @homepage {
rewrite .* / permanent; # 301 永久重導,傳遞權重
}
= @homepage 觸發命名 location,避免循環重導。permanent 返回 301,搜尋引擎更新索引;若需 302 臨時重導用 redirect。
# 1. 語法檢查 (修改配置後必跑)
/usr/local/nginx/sbin/nginx -t
# 輸出:nginx: the configuration file ... syntax is ok / test is successful
# 2. 熱重載 (不中斷連線,Worker 平滑換代)
/usr/local/nginx/sbin/nginx -s reload
原理: Master 進程讀取新配置,啟動新 Worker 處理新請求,舊 Worker 處理完現有連線後優雅退出,零停機。
| 信號 | 命令 | 作用 |
|---|---|---|
| reload | nginx -s reload | 熱重載配置 |
| reopen | nginx -s reopen | 重新打開日誌文件 (配合 logrotate) |
| stop | nginx -s stop | 快速關閉 (強制終止) |
| quit | nginx -s quit | 優雅關閉 (待連線處理完) |
收藏備查,運維提效利器。