在 nginx 和 php 上访问被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21909860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:32:10  来源:igfitidea点击:

access denied on nginx and php

phpnginx

提问by Hasan

Using nginx web server and php. nginx is working, I see 'Welcome to nginx!' but I get 'access denied' when trying to access a php page. I also installed php-fastcgi.

使用 nginx 网络服务器和 php。nginx 正在工作,我看到“欢迎使用 nginx!” 但是当我尝试访问一个 php 页面时,我得到了“访问被拒绝”的消息。我还安装了 php-fastcgi。

Here is my nginx default conf:

这是我的 nginx 默认配置:

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

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    root   /usr/share/nginx/html;
    fastcgi_index  index.php;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include fastcgi_params;
   # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    include        fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
    deny  all;
}

I activited security.limit_extensions = .php .php3 .php4 .php5 .htmland listen = /var/run/php5-fpm.sockin /etc/php-fpm.d/www.conf and cgi.fix_pathinfo = 0in /etc/php5/fpm/php.ini

我活化状态security.limit_extensions = .php .php3 .php4 .php5 .html,并listen = /var/run/php5-fpm.sock在/etc/php-fpm.d/www.conf和cgi.fix_pathinfo = 0在/etc/php5/fpm/php.ini

I restarted nginx and php5-fpm.

我重新启动了 nginx 和 php5-fpm。

Thanks for helping.

谢谢你的帮助。

采纳答案by Pian0_M4n

Do like this where you have your secondary location

在你有第二个位置的地方这样做

location / {

        try_files $uri $uri/ =404;  

        root /path/to/your/www;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;

        fastcgi_param   PATH_INFO         $fastcgi_path_info;
            fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name;

    }

These 2 parameters are the magic sauce:

这两个参数是神奇的酱汁:

fastcgi_param   PATH_INFO         $fastcgi_path_info;
fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name;

回答by Obinwanne Hill

In the event that you are trying to use NGINX to parse HTML as PHP and you are getting an Access deniederror, you need to change a PHP configuration setting.

如果您尝试使用 NGINX 将 HTML 解析为 PHP 并且遇到Access denied错误,则需要更改 PHP 配置设置。

On Ubuntu 16, the file you need to update is in /etc/php/7.0/fpm/pool.d/www.conf.

在 Ubuntu 16 上,您需要更新的文件位于/etc/php/7.0/fpm/pool.d/www.conf.

Go to the line where it says ;security.limit_extensions = .php .php3 .php4 .php5 .php7

转到它说的那一行 ;security.limit_extensions = .php .php3 .php4 .php5 .php7

Replace that with this security.limit_extensions = .php .html. Notice that the leading semi-colon has been removed.

用这个替换它security.limit_extensions = .php .html。请注意,前导分号已被删除。

Then restart PHP sudo systemctl restart php7.0-fpm. The issue should be fixed.

然后重启 PHP sudo systemctl restart php7.0-fpm。问题应该得到解决。

For more information please see this more detailed guide: Fix "access denied" error when parsing HTML as PHP with Nginx

有关更多信息,请参阅此更详细的指南:使用 Nginx 将 HTML 解析为 PHP 时修复“访问被拒绝”错误

回答by liepumartins

I know some possible scenarios when nginx and php cannot access files:

我知道 nginx 和 php 无法访问文件时的一些可能情况:

  1. Most likely php-fpmprocess is run by user that does not have read permission on corresponding .php files. This gives plain error Access denied.

  2. nginxprocess does not have read and traverse permissions on rootdirectory containing the sites files. This gives 403 Forbiddenerror.

  3. php-fpmprocess cannot traverse the absolute path to rootdirectory. This gives File not founderror.

  1. 最有可能的php-fpm进程是由对相应 .php 文件没有读取权限的用户运行的。这给出了明显的错误Access denied.

  2. nginx进程对root包含站点文件的目录没有读取和遍历权限。这给出了403 Forbidden错误。

  3. php-fpm进程无法遍历root目录的绝对路径。这给出了File not found错误。

Since author mentions, that problem appears only when accessing php files, I would say that first scenario applies here.

由于作者提到,该问题仅在访问 php 文件时出现,我会说第一种情况适用于此处。

I believe the case is that nginxis run as one user and php-fpmas another, only the php-fpmuser has been forgotten to give read access.

我相信这种情况是nginx作为一个用户运行,另一个用户运行php-fpm,只有php-fpm用户被忘记授予读取访问权限。

回答by Xianlin

Please check your fastcgi_params file and change it accordingly to this post https://askubuntu.com/questions/164627/nginx-php-fpm-access-denied-error

请检查您的 fastcgi_params 文件并根据此帖子进行相应更改 https://askubuntu.com/questions/164627/nginx-php-fpm-access-denied-error

I solved my problem by using the above method.

我通过使用上述方法解决了我的问题。