繁体   English   中英

在nginx和php上访问被拒绝

[英]access denied on nginx and php

使用nginx web服务器和php。 nginx正在工作,我看到'欢迎来到nginx!' 但在尝试访问php页面时,我得到“访问被拒绝”。 我还安装了php-fastcgi。

这是我的nginx默认conf:

# 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;
}

我在/etc/php-fpm.d/www.conf中security.limit_extensions = .php .php3 .php4 .php5 .htmllisten = /var/run/php5-fpm.sock ,在/中security.limit_extensions = .php .php3 .php4 .php5 .html cgi.fix_pathinfo = 0等/ PHP5 / FPM / php.ini中

我重启了nginx和php5-fpm。

谢谢你的帮助。

如果您有次要位置,请这样做

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;

    }

这两个参数是魔术酱:

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

当nginx和php无法访问文件时,我知道一些可能的场景:

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

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

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

由于作者提到,这个问题只在访问php文件时出现,我想说第一个场景适用于此处。

我相信的情况是nginx作为一个用户运行而php-fpm作为另一个用户运行,只有php-fpm用户被忘记提供读取权限。

请检查您的fastcgi_params文件并将其更改为相应的帖子https://askubuntu.com/questions/164627/nginx-php-fpm-access-denied-error

我用上面的方法解决了我的问题。

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

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

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

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

然后重启PHP sudo systemctl restart php7.0-fpm 这个问题应该修复。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM