wordpress伪静态,大家都是用下面的配置文件来实现。
location / {
if (-f $/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}}
这是rewrite 通过加载外置的PCRE规则来实现的,外置的东西,效率肯定不高的。
今天给诸位介绍一种通过核心模块里面的try_files指令来实现伪静态,而且只需要一行配置代码:
try_files $uri $uri/ /index.php?$args;
完整的配置文件如下:
# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}
server {
## Your website name goes here.
server_name domain.tld;
## Your only path reference.
root /var/www/wordpress;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|CSS|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
注意php.ini文件里面的cgi.fix_pathinfo应该设为0,安全性更高。
相关解释:
log_not_found指令
语法: log_not_found on | off; 默认值: on; 配置段: http, server, location
启用或者禁用404错误日志,这个指令可以用来禁止nginx记录找不到robots.txt和favicon.ico这类文件的错误信息。
try_files
这个指令可以按照参数顺序检查文件是否存在,以及返回第一个被找到的文件名,以”/”结尾的表示一个目录。如果文件没有找到,将被内部重定向到最后一个参数上。最后的参数是一个后备的URI,它必须存在,否则会报内部错误。
WordPress
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
