WordPress 可以自行設定固定網址 (Permalinks) , 以可讀性較高的格式呈現, 如果沒有設定, 預設會使用類似 ?p=123 結尾的網址。
一般上如果在 Apache 上安裝 WordPress, WordPress 內建的 .htaccess 會使用 mod_rewrite 自動進行對應。
但 Nginx 並不支援 mod_rewrite, 需要編輯 Nginx 的設定檔。
開啟網站的 Nginx 設定檔, 以下假設設定檔在 /etc/nginx/conf.d/opencli.com.conf:
$ sudo vi /etc/nginx/conf.d/opencli.com.conf
找到 server { 內加入以下幾行:
1 2 3 |
location / { try_files $uri $uri/ /index.php?$args; } |
上面的 location / 設定 WordPress 在根目錄, 如果 WordPress 安裝在副目錄, 例如 /blog 下, 要改成這樣:
1 2 3 |
location /blog/ { try_files $uri $uri/ /blog/index.php?$args; } |
儲存後重新載入 Nginx:
$ sudo systemctl reload nginx
這樣 Nginx 下的 WordPress 便可以使用固定網址了。