【问题标题】:Nginx serves .php files as downloads, instead of executing themNginx 提供 .php 文件作为下载,而不是执行它们
【发布时间】:2014-10-24 19:25:12
【问题描述】:

我正在使用 Droplet (Digital Ocean) 安装网站。我在使用 PHP 正确安装 NGINX 时遇到问题。我做了一个教程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

我的 /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
            }

...

其他“位置”已注释...

.

【问题讨论】:

  • 是的...您没有正确设置带有 Nginx 的 PHP-FPM。这就是我们可以告诉您的全部内容,因为您没有向我们展示您的任何配置。
  • 你想看哪个文件?谢谢@Brad
  • 您将配置放入哪个文件(或多个文件)。相关部分是...您在 Nginx 配置中设置 PHP-FPM 的位置。
  • 在谷歌搜索了一个小时后发现了这个askubuntu.com/a/601996/89455 - 如果您在尝试清除缓存之前配置错误 - 在这里工作!
  • stackoverflow.com/q/42664080/287948查看 PHP7 的类似问题/答案

标签: php wordpress nginx location fastcgi


【解决方案1】:

试试这个:

  1. 编辑/etc/nginx/sites-available/default

  2. 取消注释两个监听行以使 nginx 在端口 80 IPv4 和 IPv6 上监听。

    listen   80; ## listen for ipv4; this line is default and implied
    listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
    
  3. 别管server_name

    # Make site accessible (...)
    server_name localhost;
    
  4. index.php 添加到index

    root /usr/share/nginx/www;
    index index.php index.html index.htm;
    
  5. 取消注释location ~ \.php$ {}

    # pass the PHP scripts to FastCGI server listening on (...)
    #
    location ~ \.php$ {
            try_files $uri =404;
            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;
    }
    
  6. 编辑/etc/php5/fpm/php.ini 并确保cgi.fix_pathinfo 设置为0

  7. 重启nginx和php5-fpm sudo service nginx restart && sudo service php5-fpm restart


我一周前才开始使用 Linux,所以我真的希望能在这方面对您有所帮助。我正在使用 nano 文本编辑器来编辑文件。如果没有,请运行 apt-get install nano。谷歌上它以了解更多信息。

【讨论】:

  • service nginx restart && service php5-fpm restart
  • nginx -s reload
  • 用 PHP7-fpm 是:服务 php7.0-fpm 重启
  • @Joy,尝试了您的建议,但没有奏效。不得不使用 fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  • 对于php7.2:unix:/var/run/php5-fpm.sock;转换为unix:/var/run/php/php7.2-fpm.sock;(再嵌套/php
【解决方案2】:

我遇到了类似的问题,通过清空浏览器缓存解决了(也适用于不同的浏览器)。

【讨论】:

  • 我认为重启和隐身模式对我有帮助。非常感谢我们能做的菜鸟。
  • 非常感谢。你拯救了我的一天)也为隐身+1!
  • 这对我有用,也是在尝试了很多其他建议的解决方案后数小时。
  • 我的网站在 IE 和 chrome 上工作,而在 mozilla 中它下载了网站而不是执行它,这对我来说很奇怪。这是 Mozilla 的缓存错误。
  • 你节省了我的时间,兄弟。
【解决方案3】:

你需要将这个添加到 /etc/nginx/sites-enabled/default 以在 Nginx 服务器上执行 php 文件:

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

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

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

【讨论】:

  • 什么是 SCRIPT_FILENAME ?
  • 如果您使用的是 PHP 7.0,那么这是正确的:fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  • @AndrewFox- 我的文件名是php7.0-fpm.pid 而不是/var/run/php/ 中的php7.0-fpm.sock 这是什么意思?
  • @vsync 试试看。 sudo service php7.0-fpm restart。如果 sock 丢失,则表明 php 进程正在运行。
  • @Pathros - 我发现 wordpress(在我的情况下)在另一个文件夹中有服务器配置文件,这些是需要编辑的 /etc/nginx/conf.d/wordpress_https.conf
【解决方案4】:

我在上面看到了很多解决方案,其中很多对我来说都是正确的,但我不明白他们在做什么,并且担心只是复制粘贴代码,特别是 fastcgi。所以这是我的 2 美分,

  1. nginx 是一个网络服务器(而不是一个应用服务器),因此它只能提供静态页面。
  2. 每当我们尝试渲染/返回 .php 文件,例如 index.php,nginx 不知道该怎么做,因为它无法理解 .php 文件(或就此而言,任何扩展名,除了少数几个像 .html、.js 等是静态文件的扩展名)
  3. 因此,为了运行其他类型的文件,我们需要一些位于 nginx 和应用程序(这里是 php 应用程序)之间的东西。这就是通用网关接口 (CGI) 的用武之地。它是一个管理这种通信的软件。 CGI 可以用任何可能的语言 Python (uWSGI)、PHP (FPM) 甚至 C 来实现。FastCGI 基本上是 CGI 的升级版本,比 CGI 快得多。

对于某些服务器,如 Apache,内置支持解释 PHP,因此不需要 CGI。

This digital ocean link,很好地解释了安装 FPM 的步骤,我没有编写解决 php 文件下载而不是渲染问题所需的步骤,因为其他答案恕我直言很好。

【讨论】:

  • 很好的解释器。谢谢。
  • nginx 是网络 SERVER 不是浏览器,请修复它。
【解决方案5】:

更新 nginx 配置 /etc/nginx/sites-available/default 或您的配置文件

如果你使用的是 php7 使用这个

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;      
    }

如果你使用的是 php5 使用这个

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

完整详情请访问此处Detail here

【讨论】:

  • 我试过这对我不起作用,我使用的是 ubuntu 14.04 和 php 7.0 我该如何解决这个问题?
  • 以上共享链接“完整详细信息”已损坏
  • include snippets/fastcgi-php.conf;是什么意思?
【解决方案6】:

我遇到了同样的问题,但没有一个答案能解决问题。

我跑了:

sudo nginx -t

在 /etc/nginx/sites-available/default 测试配置文件。

它给了我这些错误:

nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/default:115
nginx: configuration file /etc/nginx/nginx.conf test failed

所以我进入了配置文件,在最后一行有

#}

我取消注释,再次运行测试命令,它工作了

【讨论】:

  • 除了提供解决问题的建议外,还包含了测试命令语法。
【解决方案7】:

这对我有用。

1) MyApp 文件

vi /etc/nginx/sites-available/myApp

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

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

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

PHP5 用户

改变

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

fastcgi_pass unix:/var/run/php5-fpm.sock;

2) 配置 cgi.fix_pathinfo

将 cgi.fix_pathinfo 设置为 0

地点:

PHP5 /etc/php5/fpm/php.ini

PHP7 /etc/php/7.0/fpm/php.ini


3) 重启服务

FPM

php5sudo service php5-fpm restart

php7sudo service php7.0-fpm restart

NGINX

sudo service nginx restart

【讨论】:

  • fastcgi_pass unix:/run/php/php7.0-fpm.sock;是不正确的。应该是/var/run/php/php7.0-fpm.sock;并且您需要使用已安装的任何内容更新指定的 php 版本,例如:/var/run/php/php7.2-fpm.sock;
【解决方案8】:

对我来说,在 /index.php 的末尾添加 ?$query_string 很有帮助,如下所示:

location / {
        try_files $uri $uri/ /index.php?$query_string;
}

【讨论】:

    【解决方案9】:
    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
    
        root /var/www/html;
        index index.php index.html index.htm;
    
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
    }
    

    上面的 sn-ps 在 php7.2 的情况下对我有用

    【讨论】:

    • 这对我来说是真正的解决方案。
    【解决方案10】:

    如果任何建议的答案都不起作用,试试这个:

    1.fix www.conf in etc/php5/fpm/pool.d:

    listen = 127.0.0.1:9000;(delete all line contain listen= )

    2.fix usr/local/nginx/conf中的nginx.conf:

    remove server block server{} (if exist) in block html{} because we use server{} in default (config file in etc/nginx/site-available) which was included in nginx.conf.

    3。修复 etc/nginx/site-available 中的默认文件

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

    4.重启nginx服务

    sudo 服务 nginx 重启

    5.重启php服务

    服务 php5-fpm 重启

    6.享受

    在 /usr/share/nginx/html 中创建任何 php 文件并在“server_name/file_name.php”中运行 (server_name 取决于您的配置,通常是 localhost,file_name.php 是在 /usr/share/nginx/html 中创建的文件的名称)。

    我使用的是 Ubuntu 14.04

    【讨论】:

      【解决方案11】:

      上面的答案似乎对我达到的解决方案进行了太多评论。这是我的文件的样子:

      /etc/nginx/sites-available/默认

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

      希望这可以帮助一些在周日下午感到沮丧的人(c:

      【讨论】:

        【解决方案12】:

        对于任何与 PHP 7 有相同问题的人,这是我为使 nginx 在 CentOS 7 中正确执行 php 文件所做的,张贴在这里,以防万一有人遇到同样的问题:

        • Digital Ocean上一步一步地按照这个文档。

        • 打开/etc/nginx/conf.d/default.conf(默认我没有启用站点也没有站点可用,您可以相应地进行编辑)。

        • 编辑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

        • 最后但最重要的是,清除浏览器缓存或在incognito (Chrome)Private Browsing (Firefox) 等中运行...

        希望这个有用且快乐的编码

        【讨论】:

          【解决方案13】:

          我的解决方案是添加

              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 对我不起作用。

          【讨论】:

            【解决方案14】:

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

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

            然后重新加载 Nginx:

            $ sudo systemctl reload nginx
            

            【讨论】:

              【解决方案15】:

              所以这就是在我的案例中最终起作用的重写规则的罪魁祸首
              我改变了nginx重写规则如下..

                 location /vendors { rewrite ^/vendors/?$ /vendors.php break; } 
              

              变成……

                location /vendors { rewrite ^/vendors/?$ /vendors.php last; }
              

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

              【讨论】:

                【解决方案16】:

                我现在用这段代码解决了我的问题(更改你的 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;
                }
                

                【讨论】:

                  【解决方案17】:

                  Ubuntu 16.04 对我有用,而 php7 删除了这一行

                  fastcgi_split_path_info ^(.+\.php)(/.+)$;

                  之后它停止下载 php 文件。

                  【讨论】:

                    【解决方案18】:

                    取消注释 /etc/nginx/sites-available/default 中的 .php 位置

                    sudo vi /etc/nginx/sites-available/default:

                    location ~ \.php$ {
                                include snippets/fastcgi-php.conf;
                    
                                # With php5-cgi alone:
                        #       fastcgi_pass 127.0.0.1:9000;
                                # With php5-fpm:
                                fastcgi_pass unix:/var/run/php5-fpm.sock;
                        }
                    

                    【讨论】:

                      【解决方案19】:

                      如果还有什么对你没有帮助的话。也许更早你安装了带有 info.php 测试文件的 apache2。只需清除本地主机的应用数据(缓存、cookie)即可。

                      【讨论】:

                        【解决方案20】:

                        检查你的 nginx 配置文件扩展名是 *.conf。
                        例如:/etc/nginx/conf.d/myfoo.conf

                        我也遇到了同样的情况。在我将我的配置文件从 myfoo 重命名为 myfoo.conf 后,它修复了。 重命名后不要忘记重启nginx。

                        【讨论】:

                          【解决方案21】:

                          首先你必须在浏览器中Remove cache

                          然后打开终端并运行以下命令:

                          sudo apt-get install php-gettext
                          sudo nano /etc/nginx/sites-available/default
                          

                          然后在default文件中添加如下代码:

                          server {
                              listen 80 default_server;
                              listen [::]:80 default_server ipv6only=on;
                          
                              root /usr/share/nginx/html;
                              index index.php index.html index.htm;
                          
                              server_name localhost;
                          
                              location / {
                                  try_files $uri $uri/ =404;
                              }
                          
                              error_page 404 /404.html;
                              error_page 500 502 503 504 /50x.html;
                              location = /50x.html {
                                  root /usr/share/nginx/html;
                              }
                          
                              location ~ \.php$ {
                                  try_files $uri =404;
                                  fastcgi_split_path_info ^(.+\.php)(/.+)$;
                                  fastcgi_pass unix:/var/run/php5-fpm.sock;
                                  fastcgi_index index.php;
                                  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                                  include fastcgi_params;
                              }
                          }
                          

                          如果有任何不匹配,请通过以下命令从终端更正并重新启动 Nginx

                          sudo systemctl 重启 nginx

                          然后转到浏览器并享受...

                          【讨论】:

                            【解决方案22】:

                            对我来说是这样的: fastcgi_pass unix:/var/run/php5-fpm.sock;

                            必须是: fastcgi_pass unix:/run/php5-fpm.sock;

                            【讨论】:

                              【解决方案23】:

                              我正准备想办法解决这个问题,对我来说,问题是 Cloudflare 缓存了 php 文件并一直让我下载它。

                              我的解决方法是清除 Cloudflare 上的缓存。

                              【讨论】:

                              • 也许你想告诉我们怎么做?
                              【解决方案24】:

                              我遇到了同样的问题,如果你有 css 不加载问题,那么这个服务器块也有这个块在其他位置块之上。我添加到我的站点可用的 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;
                              }
                              

                              【讨论】:

                                【解决方案25】:

                                还有一件事要检查:如果您在设置 PHP 之前设置了 HTTPS 访问——我使用了 certbot——你需要在 /etc/nginx/sites 中进行更改-available/default 两次,因为会有两个服务器块(一个侦听端口 80,一个侦听端口 443)。

                                (我主要为电子邮件设置此服务器,当我第一次安装 nginx 时并没有使用 PHP,只是为了更轻松地运行 certbot。)

                                【讨论】:

                                  【解决方案26】:

                                  为了记录,我发现我的 php-fpm 没有运行,我用service php7.2-fpm start 修复了它。

                                  【讨论】:

                                    【解决方案27】:

                                    我长期以来一直在努力解决这个问题,这些步骤对我有用。

                                    第 1 步:所有 PHP 文件的位置块配置

                                    location ~ \.php$ {
                                            try_files $uri /index.php =404;
                                            fastcgi_pass unix:/run/php/php7.3-fpm.sock;
                                            fastcgi_index index.php;
                                            include fastcgi_params;
                                    }
                                    

                                    第 2 步:在配置文件中添加 fastcgi_param 我们只需要打开 /etc/nginx/fastcgi_params 文件并在文件末尾添加以下行。

                                    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                                    

                                    然后重启服务,

                                    systemctl restart php7.3-fpm
                                    systemctl restart nginx
                                    

                                    【讨论】:

                                      【解决方案28】:

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

                                      brew services list
                                      

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

                                      brew services start php
                                      

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

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

                                      【讨论】:

                                        猜你喜欢
                                        • 2017-07-28
                                        • 2017-03-11
                                        • 2019-04-22
                                        • 2014-06-07
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 1970-01-01
                                        相关资源
                                        最近更新 更多