Nginx 原生不支援 Apache 的分散式配置文件 .htaccess,但通過 include 指令可實現「類 .htaccess」體驗:在項目目錄維護 rewrite 規則,Nginx 主配置引入,實現偽靜態、訪問控制、密碼保護等功能。本文詳細講解引入機制、規則轉換、權限陷阱與最佳實踐。
# Nginx 主配置 http/Server/Location 上下文均可使用
server {
listen 80;
server_name example.com;
root /var/www/example.com;
# 引入項目目錄下的 .htaccess (任意文件名)
include /var/www/example.com/.htaccess;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
⚠️ 關鍵限制:include 在**配置加載時**靜態展開,**不支援運行時動態讀取**。修改 .htaccess 後**必須 reload Nginx** 生效。若需 Apache 風格的即時生效,請改用 Lua (OpenResty) 或直接編寫 Nginx 原生 rewrite。
# Apache .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archiver/((fid|tid)-[\w-]+\.html)$ archiver/index.php?$1 [L]
RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2 [L]
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page%3D$3&page=$2 [L]
RewriteRule ^profile-(username|uid)-(.+)\.html$ viewpro.php?$1=$2 [L]
RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2 [L]
RewriteRule ^tag-(.+)\.html$ tag.php?name=$1 [L]
# Nginx rewrite 規則 (放入 .htaccess)
# 注意:Nginx 無 RewriteEngine/RewriteBase,直接寫 rewrite 指令
# 正則捕獲用 $1 $2...,標誌位 last/break/redirect/permanent
# 歸檔
rewrite ^(.*)/archiver/((fid|tid)-[\w-]+\.html)$ $1/archiver/index.php?$2 last;
# 論壇列表分頁
rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
# 帖子分頁
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
# 用戶資料
rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;
# 空間
rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;
# 標籤
rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;
| Apache | Nginx | 說明 |
|---|---|---|
RewriteRule | rewrite | 指令名不同 |
[L] (Last) | last | 標誌位寫法不同,Nginx 為 last/break/redirect/permanent |
%{REQUEST_FILENAME} | $request_filename | 變量前綴不同 |
RewriteCond | if (!-f $request_filename) { ... } | Nginx 用 if 判斷 (慎用),推薦 try_files |
RewriteBase / | 無需對應 | Nginx 以 location 為基準 |
正則捕獲 $1 | 正則捕獲 $1 | 相同 |
.htaccess 即時生效 | nginx -s reload 生效 | 本質區別 |
htaccess2nginx (Python 腳本,GitHub 可獲取)| Apache .htaccess 功能 | Nginx 實現方式 |
|---|---|
密碼保護 AuthType Basic | auth_basic "Restricted"; auth_basic_user_file /path/.htpasswd; |
IP 白名單/黑名單 Allow/Deny | allow 192.168.1.0/24; deny all; |
自定義錯誤頁 ErrorDocument 404 /404.html | error_page 404 /404.html; |
MIME 類型 AddType | types { application/wasm wasm; } 在 http 上下文 |
PHP 開關 php_flag | 不支援,需 php-fpm pool 配置或 fastcgi_param |
chown www-data:www-data .htaccess && chmod 644 .htaccessnginx -t 失敗、reload 失敗 → **上線前必須 nginx -t 驗證**return 200 "DEBUG: $1"; 或使用 ngx_lua 記錄核心靜態化規則 → 主配置 location/rewrite (性能最高、可見性最好)
業務頻變 SEO 規則 → 受控目錄 .htaccess + CI/CD 自動 reload
密碼/IP/錯誤頁 → Nginx 原生指令 (無需 .htaccess)
遷移期雙寫 → 逐條驗證、灰度發布、監控錯誤率
合理利用 include 機制,在 Nginx 高性能架構下兼顧「配置靈活性」與「運維可控性」。
Linux 串口編程完整指南:術語、API、配置流程與設備節點差異
Python 腳本自動更新 Google Hosts (Windows 適用)
解決 NTP 錯誤:no server suitable for synchronization found
RedHat Linux 手動編譯安裝 vsftpd 完整教學:從源碼到生產環境配置
Linux 部署 SSH 服務實戰:告別 Telnet 明文傳輸,構建加密遠程運維通道
Linux 下使用網路加速器 netpas:VirtualBox + Windows XP 虛擬機 + NAT32 + 3proxy 完整實作