Nginx将.php文件作为下载而不是执行它们

213
我正在安装一个网站到Droplet(Digital Ocean)中。我遇到了一个问题,就是无法正确地安装NGINX和PHP。我按照教程https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04进行操作,但当我尝试运行一些.php文件时,它只是下载这些文件...例如: http://5.101.99.123/info.php 是可以工作的,但是如果我进入主页 http://5.101.99.123 ,它会下载我的index.php:/ 有什么建议吗?
-rw-r--r--  1 agitar_user www-data   418 Jul 31 18:27 index.php
-rw-r--r--  1 agitar_user www-data    21 Aug 31 11:20 info.php

My /etc/nginx/sites-available/default

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/html;
        index index.html index.htm index.php;

        # Make site accessible from http://localhost/
        server_name agitarycompartir.com;

               location ~ \.php$ {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    ## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #               # With php5-cgi alone:
    #               fastcgi_pass 127.0.0.1:9000;
    #               # With php5-fpm:
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
            }
  

              location / {
                    
                    try_files $uri $uri/ =404;
                    # Uncomment to enable naxsi on this location
                    # include /etc/nginx/naxsi.rules
            }

...

Other "locations" are commented on...

.


6
没错...你没有正确地配置Nginx与PHP-FPM。但由于你没有向我们展示任何配置文件,这就是我们能告诉你的全部了。 - Brad
位置块以location some-regex-here {开始。 正则表达式和标志指定服务器如何处理匹配的资源。 你在问题中展示的PHP位置块指示Nginx应用这些规则来处理每个匹配的请求/资源。 听起来PHP没有遵循这些规则。 假设文件被加载了,另一个位置块在PHP块之前捕获了PHP文件是有道理的。(顺便说一句,测试文件是否被加载的快速方法是制造语法错误并重新启动服务器。) - Brad
检查一下,如果我在我的网站上进行查询...(http://5.101.99.123/?=imitacion&submit=Ir)或者http://5.101.99.123/?= :S :S - Apeiron
3
大约搜了一个小时,找到了这个链接http://askubuntu.com/a/601996/89455——如果之前配置有问题,请尝试清除缓存——这里有效! - tm_lv
1
请参考以下PHP7相关的问题/答案:https://dev59.com/KFgQ5IYBdhLWcg3wfULp - Peter Krauss
显示剩余4条评论
28个回答

3
我的解决方案是添加了

标签。

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

例如,将其添加到我的自定义配置文件中:etc/nginx/sites-available/example.com.conf

将其添加到/etc/nginx/sites-available/default并没有起作用。


3
上面的答案好像注释掉了我达到的解决方案太多了。这是我的文件内容:
/etc/nginx/sites-available/default
location ~ \.php$ {
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

希望这能帮助一些在周日下午感到沮丧的人(笑)。

3

对于任何遇到 PHP 7 相同问题的人,以下是我在 CentOS 7 上使 nginx 正确执行 php 文件所做的操作,这样以防有人遇到相同问题:

  • 按照Digital Ocean上的文档逐步进行。

  • 打开 /etc/nginx/conf.d/default.conf(默认情况下我没有 sites-enabled 或者 sites-available,可以根据实际情况编辑)。

  • location 参数编辑如下:

default.conf

location ~ \.php$ {
    try_files $uri =404;
    #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

    #instruct nginx execute php7 files instead download them :D
    fastcgi_pass unix:/var/run/php-fpm/www.sock;

    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
  • 重新启动Nginx和PHP服务:sudo systemctl restart php-fpmsudo systemctl restart nginx

  • 最后但最重要的是,清除浏览器缓存或在隐身模式(Chrome)私密浏览(Firefox)等中运行...

希望这有帮助,愉快编码!


2
在我的个案中,以下是最终有效的重写规则:
我将nginx的重写规则更改为以下内容...
   location /vendors { rewrite ^/vendors/?$ /vendors.php break; } 

最初的回答变成了...
  location /vendors { rewrite ^/vendors/?$ /vendors.php last; }

显然,没有使用last关键字,请求没有重新启动,因此它从未触及.php位置段,并被简单地解释为下载 -


2

在我的情况下,我没有使用/etc/nginx/sites-available/default,而是使用了一个不同的服务器块配置文件(例如example.com),我解决这个问题的唯一方法是删除默认服务器块配置文件的符号链接:

$ rm /etc/nginx/sites-enabled/default

然后重新加载Nginx:

$ sudo systemctl reload nginx

2
记录一下,我发现我的php-fpm没有在运行,我使用service php7.2-fpm start命令进行修复。最初的回答。

2
我现在用这段代码解决了我的问题(更改你的IP):

我解决了我的问题,使用以下代码(更改您的IP):

location / {
access_log off;
    log_not_found  off;
    client_max_body_size    2000m;
    client_body_buffer_size 512k;
    proxy_buffering on;
    proxy_send_timeout 300s;
    proxy_read_timeout 300s;
    proxy_buffer_size 64k;
    proxy_buffers 32 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    proxy_connect_timeout 300s;
    proxy_http_version 1.1;
    proxy_set_header Range "";
    proxy_pass   https://123.123.123.123:444;
    proxy_set_header   Host   $host;
    proxy_set_header   X-Real-IP  $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_redirect     off;
}

1

我在Mac上使用Homebrew安装了PHP,但是在我的情况下,php-fpm服务没有运行。

brew services list

enter image description here

服务已启动,PHP脚本开始执行。

brew services start php

我的nginx服务器位置块中的fastcgi设置

location ~ \.php$ {
  ...
  include        fastcgi_params;
  fastcgi_pass   127.0.0.1:9000;
  ...
}

1
我一直遇到同样的问题,解决方法是在这个服务器块中添加一个块,如果您有CSS加载问题,请将其放在其他位置块上面。我将其添加到了我的sites-available conf文件中。
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info  ^(.+\.php)(/.+)$;
fastcgi_index            index.php;
fastcgi_pass             unix:/var/run/php/php7.3-fpm.sock;
include                  fastcgi_params;
fastcgi_param   PATH_INFO       $fastcgi_path_info;
fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

1
如果其他方法都没有帮助您,或许您之前安装了带有info.php测试文件的apache2。只需清除本地主机的应用数据(缓存、Cookie)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接