首页 文章

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

提问于
浏览
109

我正在一个小滴(数字海洋)安装一个网站 . 我有一个问题,正确安装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

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

...其他“位置”被评论(#)

22 回答

  • 6

    我遇到了同样的问题是什么解决了这个服务器块也有这个块高于其他位置块如果你有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;
    }
    
  • 104

    试试这个:

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

    取消注释两个侦听行,使nginx侦听端口80 IPv4和IPv6 .

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

    单独保留server_name

    # Make site accessible (...)
    server_name localhost;
    

    将index.php添加到索引行

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

    取消注释位置〜\ .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;
    }
    

    编辑/etc/php5/fpm/php.ini并确保cgi.fix_pathinfo设置为0

    重启nginx和php5-fpm(如果你不知道怎么做,重启服务器)


    我一周前刚刚开始使用Linux,所以我真的希望能帮到你 . 我正在使用nano文本编辑器来编辑文件 . 如果没有,请运行apt-get install nano . 谷歌就此了解更多 .

  • 11

    您需要将此添加到/ etc / nginx / sites-enabled / default以在Nginx Server上执行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;
        }
    }
    
  • 3

    我有类似的问题,由 emptying the browser cache 解决(也适用于不同的浏览器) .

  • 0

    Update nginx config /etc/nginx/sites-available/default or your config file

    如果你使用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

  • 1

    我有同样的问题,没有一个答案解决了这个问题 .

    我跑了:

    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
    

    所以我进入配置文件,最后一行是

    #}
    

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

  • 0

    这适合我 .

    1) MyApp file

    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) Configure cgi.fix_pathinfo

    将cgi.fix_pathinfo设置为0

    地点:

    PHP5 /etc/php5/fpm/php.ini

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


    3) Restart services

    FPM

    php5 sudo service php5-fpm restart

    php7 sudo service php7.0-fpm restart

    NGINX

    sudo service nginx restart
    
  • 3

    如果任何建议的答案无效,请尝试以下方法:

    1.在etc / php5 / fpm / pool.d中修复www.conf:

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

    2.修复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取决于您的配置,normaly是localhost,file_name.php是在/ usr / share /中创建的文件的名称nginx / html) .

    我正在使用Ubuntu 14.04

  • 2

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

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

    所以这就是最终在我的情况下作为重写规则的罪魁祸首
    我更改了nginx重写规则如下..

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

    成为...

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

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

  • 23

    我看到上面有很多解决方案,很多都适合我,但我不明白他们在做什么,并担心只是复制粘贴代码,特别是fastcgi . 所以这是我的2美分,

    • nginx是 web browser (而不是 application browser )因此,它只能提供静态页面 .

    • 无论何时,我们尝试渲染/返回一个.php文件,例如index.php,nginx不知道.php文件(或者就任何扩展而言,除了像.html,.js等选择之外的任何扩展名 . 静态文件)

    • 因此,为了运行其他类型的文件,我们需要一些介于nginx和应用程序之间的东西(这里是php应用程序) . 这就是通用网关接口(CGI)的用武之地 . 它是管理这种通信的软件 . CGI可以用任何可能的语言Python(uWSGI),PHP(FPM)甚至C实现.FastCGI基本上是CGI的升级版本,它比CGI快得多 .

    对于一些像Apache这样的服务器,内置支持来解释PHP,因此不需要CGI .

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

  • 0

    上面的答案似乎对我达成的解决方案评论太多了 . 这就是我的文件喜欢:

    在/ etc / nginx的/网站可用/默认

    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:

  • 37

    我的解决方案是添加

    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 我使用的是不同的服务器块配置文件(例如example.com),我能够解决此问题的唯一方法是删除默认的服务器块配置文件符号链接:

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

    然后重新加载Nginx:

    $ sudo systemctl reload nginx
    
  • 6

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

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

    之后它停止下载php文件 .

  • 0

    对于任何遇到与PHP 7相同问题的人来说,这就是我在CentOS 7中正确执行nginx执行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) 等中运行...

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

  • 0

    Uncomment the .php location in /etc/nginx/sites-available/default

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

    如果其他任何事情对你没有帮助 . 也许早些时候你用info.php测试文件安装了apache2 . 只需清除localhost的App Data(缓存,cookie)即可 .

  • 1

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

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

  • 0

    首先,您必须在浏览器中删除缓存

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

    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 restart nginx

    然后去浏览器并享受...

  • 0

    对我来说,这是一行:fastcgi_pass unix:/var/run/php5-fpm.sock;

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

  • 0

    我正准备去解决这个问题,对我而言,问题是Cloudflare缓存了php文件并一直让我下载它 .

    我的修复是清除Cloudflare上的缓存 .

相关问题