Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Nginx 偽靜態配置手把手教學:從規則檔建立到生效驗證

編輯:關於伺服器

目錄結構慣例

/alidata/server/nginx/
├── conf/
│   ├── rewrite/          # 偽靜態規則檔目錄
│   │   ├── phpwind.conf
│   │   ├── discuz.conf
│   │   └── wordpress.conf
│   └── vhosts/           # 網站虛擬主機設定
│       └── yoursite.conf

步驟 1:確認或建立規則檔

cd /alidata/server/nginx/conf/rewrite
ls
# 若無所需程式規則,建立 新程式.conf
vi 新程式.conf
# 貼上對應 Rewrite 規則

步驟 2:在虛擬主機設定中引入規則

vi /alidata/server/nginx/conf/vhosts/yoursite.conf

在 server { } 區塊內加入:

include rewrite/新程式.conf;

步驟 3:語法測試與熱重載

/alidata/server/nginx/sbin/nginx -t
# 輸出 syntax is ok / test is successful 表示通過
/alidata/server/nginx/sbin/nginx -s reload

常見程式規則範例

WordPress

location / {
    try_files $uri $uri/ /index.php?$args;
}

Discuz! X3

rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$3&page=$4 last;
rewrite ^(.*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^(.*)/space-(username|uid)-([0-9]+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^(.*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
rewrite ^(.*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;

除錯技巧

  • nginx -T 輸出完整合併後配置,檢查 include 是否生效
  • 錯誤日誌 /alidata/server/nginx/logs/error.log 查看 rewrite 錯誤
  • 瀏覽器開發者工具 Network 面板觀察 301/302/404 狀態碼
Copyright © Windows教程網 All Rights Reserved