Nginx提供对子目录的访问权限

时间:2016-01-30 09:09:21

标签: wordpress nginx

我们正在Nginx服务器上运行Wordpress网站,我现在正尝试在/ stats文件夹中安装Piwik。

这是安装时附带的默认Nginx配置,我添加了" / stats"阻止自己,但它不起作用 - 每当我去mysite.com/stats而不是去那个文件夹时,它都会被WordPress渲染。

期望的行为是/ stats子目录(以及其中的所有文件和目录)只是由PHP解析,就像没有Nginx规则的默认安装一样

我错过了什么?

server_name  _;
port_in_redirect off;

client_header_buffer_size 4k;
client_body_buffer_size 128k;
client_max_body_size 16m;

root   /var/www/html;
index  index.html index.php;

charset utf-8;

log_not_found off;

gzip_static on;
gzip_types text/css application/javascript text/xml;
gzip_vary on;
gzip on;


# redirect server error pages to the static page /50x.html
#
error_page   500 503 504 /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

location @rewrite {
    rewrite ^.*$ /index.php?$args;
}

error_page  404 @rewrite;

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# WP Multisite rewrites
rewrite /([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 last;
rewrite /([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 last;

location / {
    try_files $uri $uri.gz $uri/ @rewrite;
}

location ~ \.sql$ {
    rewrite ^.*$ /index.php?$args;
}

# We do not want to run php from wp uploads
location ~* /(?:uploads|files)/.*\.php$ {
    rewrite ^.*$ /index.php?$args;
}

location ~ \.php$ {
    fastcgi_split_path_info  ^(.+\.php)(/.+)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    fastcgi_pass             127.0.0.1:9000;
    fastcgi_index            index.php;
    include                  fastcgi_params;
    fastcgi_param            SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors off;
}

location /stats {
    try_files $uri $uri/ /index.php?$args;
    index index.php;
}

location = /favicon.ico {
    access_log off;
    expires 2w;
    add_header Cache-Control public;
    try_files $uri @rewrite;
}

location ~* \.(js|css|jpg|jpeg|png|gif|ico|woff|woff2|ttf|otf|eot|pdf|xml|mp4|ogg|mp3|mov|wmv|avi|cur|rtf|txt|swf)$ {
    add_header Cache-Control public;
    add_header Access-Control-Allow-Origin *;
    expires 2w;
    try_files $uri $uri.gz;
}

1 个答案:

答案 0 :(得分:1)

多站点WordPress的规则,尤其是这一个:rewrite /([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 last;会将任何/stats/index.php URI重定向回WordPress的/index.php

如果您没有使用多站点WordPress,则可以安全地删除冗余重写规则。

如果 使用多站点WordPress,则需要进行一些重新设计。