如何为WordPress永久链接配置Nginx
时间:2020-01-09 10:39:01 来源:igfitidea点击:
如何在WordPress博客下配置永久链接?
如何使用虚拟主机为WordPress永久链接配置Nginx?
永久链接不过是用于链接到博客内容的网址。
每个博客帖子或cms帖子的URL应该是永久的,并且永远不会更改。
因此,名称为永久链接。
WordPress能够为您的博客文章和档案创建自定义URL结构。
WordPress永久链接有三种基本类型:
- 默认:https://www.theitroad.local/faq/?p = 2284
- 漂亮地使用mod_rewrite:https://www.theitroad.local/faq/bash-for-loop/
- 使用PHP PATHINFO几乎很漂亮:https://www.theitroad.local/faq/index.php/bash-for-loop/
在"设置">"永久链接"面板中,您可以选择以下更常见的永久链接结构之一:
为WordPress永久链接配置Nginx:选择永久链接结构
如何为WordPress永久链接配置Nginx(Pretty永久链接)
使用文本编辑器(例如nano命令或vim命令)编辑您的nginx.conf文件,运行:
$ sudo vi /etc/nginx/site-enabled/theitroad.local.conf
在"服务器块"中编辑/添加/添加以下位置块:
location / { try_files $uri $uri/ /index.php?$args; }
如果您的WordPress博客位于/faq /子目录中,请尝试:
## ## note path to /faq/index.php ## location /faq/ { try_files $uri $uri/ /faq/index.php?$args; }
保存并关闭文件。
重新启动/重新加载您的nginx服务器,运行:
$ sudo systemctl reload nginx
或者
$ sudo /usr/sbin/nginx -s reload
这是我的示例配置文件:
# Upstream to abstract backend connection(s) for PHP. upstream php { server unix:/run/php/php7.0-fpm.sock; } server { server_name www.theitroad.local; root /var/www/html; location /faq/ { try_files $uri $uri/ /faq/index.php?$args; } #Add trailing slash to */wp-admin requests. rewrite /faq/wp-admin$ $scheme://$host$uri/ permanent; # Directives to send expires headers and turn off 404 error logging. location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ { access_log off; log_not_found off; expires max; } # Pass all .php files onto a php-fpm/php-fcgi server. index index.php; location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default) include /etc/nginx/fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass php; } }
Nginx和WordPress永久链接完整示例
这是我的个人博客的配置,该博客在Ubuntu Linux 16.04 LTS和PHP 7.x.x上运行:
## Upstream to abstract backend connection(s) for PHP ## upstream php { server unix:/run/php/php7.0-fpm.sock; } ## my server ip address ## server { set_real_ip_from 10.147.164.1; real_ip_header X-Forwarded-For; listen 10.147.164.3:80; server_name theitroad.com www.theitroad.com; root /home/lighttpd/theitroad.com/http; ## WordPress Perm links config ## location / { try_files $uri $uri/ /index.php?$args; } ## Add trailing slash to */wp-admin requests. rewrite /wp-admin$ $scheme://$host$uri/ permanent; ## Deal with sitemap wordpress plugin urls ## rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=" last; rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$ "/index.php?xml_sitemap=params=;zip=true" last; rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html$ "/index.php?xml_sitemap=params=;html=true" last; rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$ "/index.php?xml_sitemap=params=;html=true;zip=true" last; # Directives to send expires headers and turn off 404 error logging. location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ { access_log off; log_not_found off; expires max; } ## Okay, Pass all .php files onto a php-fpm/php-fcgi server. index index.php; location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } ## Setting works on Ubuntu/Debian Linux ### This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default) include /etc/nginx/fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass php; } }