【问题标题】:Nginx give access to subdirectoryNginx 允许访问子目录
【发布时间】:2016-02-02 03:14:41
【问题描述】:

我们在 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;
}

【问题讨论】:

  • 尝试使用 location \stats {
  • 当你访问/stats时,你请求到达location /stat。然后指令try_files 起作用,如果没有找到匹配$uri $uri/ 的静态文件,则将处理/index.php?$args。然后location ~ \.php$ 做它必须做的事情。
  • semm0,不幸的是,这没有帮助。弗拉迪斯拉夫,我不确定我是否关注你,抱歉。

标签: wordpress nginx


【解决方案1】:

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

如果您使用的不是多站点 WordPress,您可以安全地删除多余的重写规则。

如果您正在使用多站点 WordPress,则需要重新设计。

【讨论】:

  • 所以我完全错过了那行:/谢谢,这行得通!
猜你喜欢
  • 1970-01-01
  • 2014-01-20
  • 2019-03-03
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多