【问题标题】:nginx: 403 forbidden when php files in separate directorynginx:当php文件在单独的目录中时禁止403
【发布时间】:2017-10-28 17:36:23
【问题描述】:

我已经成功安装了使用 PHP-FPM 的 nginx,但不幸的是,我在从不同目录加载我的 php 文件时遇到了一些问题。我所有的文件都位于 /var/www/html 的子目录中(例如,所有的 css 文件都位于 /var/www/html/css,所有的 javascript 文件都位于 /var/www/html/js,所有的 php -文件位于 /var/www/html/php)。 据此,我将我的php文件的根目录路径更改为/var/www/html/php:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.php home.php;

    server_name _;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
            root /var/www/html/php;

            include snippets/fastcgi-php.conf;

    #       # With php7.0-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
            # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

不幸的是,当使用我的网络浏览器访问我的 nginx 服务器时,我收到错误 403(禁止访问)。直接访问我的 index.php (http://192.168.2.109/index.php) 时,一切正常。所以,我认为这意味着文件权限是正确的,但 nginx 无法索引 /var/www/html/php 目录。此外,/var/log/nginx/error.log 包括:

2017/05/28 07:49:56 [错误] 13678#13678: *1 "/var/www/html/" 的目录索引被禁止,客户端:192.168.2.101,服务器:_,请求:" GET / HTTP/1.1”,主机:“192.168.2.109”

我已经尝试启用 autoindex 并在“location ~ .php$ {”部分添加 index 说明符,但没有成功。结果是一样的:(

有没有人知道我在这里做错了什么/错过了什么? Nginx 403 error: directory index of [folder] is forbidden 中的所有建议都没有解决我的问题。

【问题讨论】:

  • 很确定你只需要一个默认索引? stackoverflow.com/questions/10002439/…
  • nginx 的配置文件中是否有“DefaultIndex”指令?提供的链接显示了 htaccess 的指令,但我没有使用 htaccess。
  • 这不是 nginx 的事情,而是 Web 服务器配置的事情。我不知道如何使用所有可能的服务器设置。我知道如果您不在 URL 中指定索引,.htaccess 中的 DirectoryIndex 声明将起作用,但我不知道如何让 Web 服务器自动执行此操作。我知道服务器可以设置为抛出一个禁止的目录遍历错误,这就是为什么很多网站在他们不希望人们在其中玩耍的目录中只有一个空白 index.html。
  • 您不能在拆分根方案中使用index 指令。它依赖于同一目录中的文件。

标签: php nginx file-permissions


【解决方案1】:

问题很简单:

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
            root /var/www/html/php;

            include snippets/fastcgi-php.conf;

    #       # With php7.0-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
            # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

据我所知,当您请求 *.php 文件时,您会更改根地址。但是,当您请求“默认索引”时,您请求的是 /,而不是 .php

您需要一个新位置,以根据 / 请求更改根路径。尝试使用这个:

    location = / {
            root /var/www/html/php;

            include snippets/fastcgi-php.conf;

            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

也许,在这个配置上,你需要放一个“rewrite index.php;”,但我不知道,因为我从未测试过这个配置。

不要认为我的新位置顺序 (location = /) 与您的 (location /) 相同,因为我的等号订单仅适用于您请求完全不带任何参数的“主要”位置时。

【讨论】:

    猜你喜欢
    • 2021-02-05
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 2018-12-17
    • 2018-10-16
    • 2016-06-19
    相关资源
    最近更新 更多