在nginx和php上访问被拒绝

时间:2014-02-20 13:53:09

标签: php nginx

使用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,在/etc/php5/fpm/php.ini中激活了cgi.fix_pathinfo = 0

我重启了nginx和php5-fpm。

感谢您的帮助。

4 个答案:

答案 0 :(得分:9)

在您拥有次要位置的情况下这样做

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;

答案 1 :(得分:1)

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

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

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

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

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

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

如果您尝试使用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。该问题应该得到解决。

有关更多信息,请参见此更详细的指南:Fix "access denied" error when parsing HTML as PHP with Nginx