【问题标题】:Nginx 403 forbidden using Location directive on PHP fileNginx 403 禁止在 PHP 文件上使用 Location 指令
【发布时间】:2017-03-29 20:44:40
【问题描述】:

这是我的服务器块:

server {
    listen       80;
    server_name  localhost;

    root /www/html;
    index index.php;

    location = / {
        autoindex on;
        index index.php;
        try_files $uri $uri/ =404;
    }

    location ~*\.(css|js|gif|jpe?g|png)$ {
        root /www/html/shared;
        rewrite ^/releases(.*)$ $1 last;
        expires 5m;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

现在,当我尝试访问“localhost/”时,这样做只会显示“403 Forbidden”错误。但是,如果我直接访问它——“localhost/index.php”,该页面工作得非常好。我无法为我的生活找出原因。 error.log 既没有给我任何有用的信息,也没有给我错误代码——只是页面被“禁止”了。 这是抛出的错误:

2017/03/29 14:37:35 [error] 11980#8840: *94 directory index of "C:/www/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"

这是一个基于 Windows 的 nginx 服务器。

【问题讨论】:

  • 尝试:location / { ... },不带=。另外,删除autoindex on;
  • 试了一下,但它似乎没有改变输出。

标签: php nginx


【解决方案1】:

发现答案有点令人失望,但我会回答,所以如果有人遇到这个愚蠢的问题,他们可以解决它。

php 正在寻找前缀为“/www/html”而不是“/www/html/”的文件,因此当它想要我的索引时,它试图加载“/www/htmlindex.php”而不是“/www/html/index.php”。

您可以通过以下方式解决此问题

 fastcgi_index /index.php;

或在根目录的末尾添加 /。

【讨论】:

    猜你喜欢
    • 2018-12-17
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 2014-08-26
    • 2017-02-24
    相关资源
    最近更新 更多