How to configure Nginx to solve the 404 error for WordPress permalinks

最近因为需要加快小站的访问速度,找到了一款名为WP Super Cache的插件,效果特别不错,但是需要更改网站的固定链接,但是更换之后发现主页到是还可以访问,但是文章页全部为404的Error。这里记录下来解决方案,以供后人借鉴。

Recently, there was an idea produced in my mind when our website was found too slow to access that a plug-in named WP Super Cache could reduce the access time significantly. Whereas, a new problem caught me that my overall web pages ,except index page , showed 404 and couldn't be accessed when a change in permalinks was asked for by this plug-in. The solution has been recorded for the later and I hope it useful to solve the analogue problem.

  1. 编辑服务器以下路径的文件:

    Find this file in your server and edit it:

$ sudo nano /etc/nginx/sites-enabled/default
  1. 在server块区内添加以下内容:

    Append this information in server block:

location / {
            try_files $uri $uri/ /index.php?$args; 
}
  1. 如果你的WordPress位置不是在/var/www/html,而是自定义的目录(例如:/var/www/html/yuhualixin/),则需要更改位置:

    You might need to change your path when your website is not in /var/www/html but in other user-defined directory e.g. /var/www/html/yuhualixin/ :

##
## note path to /yuhualixin/index.php 
##
location /yuhualixin/ {
            try_files $uri $uri/ /yuhualixin/index.php?$args; 
}
  1. 重启Nginx服务:

    Restart the service of nginx:

sudo systemctl reload nginx

或者 or

sudo service nginx restart

参考 Reference:

https://www.cyberciti.biz/faq/how-to-configure-nginx-for-wordpress-permalinks/

Last modified: 2020年3月30日

Comments

Write a Reply or Comment

Your email address will not be published.