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 便可以使用固定网址了。