【问题标题】:NGINX shows script code instead of executeNGINX 显示脚本代码而不是执行
【发布时间】:2014-02-03 02:28:43
【问题描述】:

我在使用 NGINX 时遇到了一个问题,这让我发疯了。我在 VirtualBox 上有 Ubuntu 12.04.3 LTS。我按照以下说明安装了 NGINX-MYSQL-PHP:

  1. 安装php5-fpm;
  2. /etc/php5/fpm/php.ini中设置cgi.fix_pathinfo=0
  3. /etc/php5/fpm/pool.d/www.conf 中设置 listen = var/run/php5-fpm.sock

然后我在/etc/nginx/sites-available/default中设置

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

localhostinfo.php 都正确执行。现在我想建立一个本地网站并尝试进行一些重定向。所以我在 /usr/share/nginx/www 中创建了一个名为 mywebsite 的新文件夹,在里面放了一个带有 echo 指令的 index.php,我重定向到的 php 脚本,在 sites-available 中添加了一个新配置文件,并在 sites-enabled 中添加了一个链接,然后禁用 application/octet-在 nginx.conf 中流 以避免下载脚本文件。所以我将我的新配置文件设置如下:

server {
    listen   80;

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

    server_name www.mysite.com;

    location / {
      try_files $uri $uri/ /index.html;
      if (!-e $request_filename){
        rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/$ /senseddata.php break;
        rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/lasthour/$ /senseddata.php break;
        rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/daily/$ /senseddata.php break;
      }
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
       root /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.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;
       try_files $uri =404;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
    deny all;
   }
}

那么到底发生了什么? Index.php 正确执行。重写做得很好,但脚本没有执行。我只看到我编写的整个代码显示在浏览器中。有什么问题?提前致谢。

【问题讨论】:

  • 你的 nginx 是独立工作还是使用 apache 反向代理?看起来您的 nginx 没有有效的 php 处理程序
  • 运行sudo service php5-fpm status 会得到什么?
  • 感谢您的回答。 NGINX 单独工作并且 php5-fpm 状态正在运行。顺便说一句,我解决了在重写行中写 LAST insted of BREAK 的问题。我不知道这两个词之间的区别(也许有人可以解释)但它解决了问题。这很好。非常感谢。
  • 我有同样的问题,我的配置文件与他的回答中提到的@pasha-proton 相同,仍然不适合我。

标签: php nginx rewrite


【解决方案1】:

还要检查 PHP 文件中的开始标签。如果您使用<? 而不是<?php 并且short tag 不可用,PHP 代码将不会被解析,您会看到源代码。

【讨论】:

    【解决方案2】:

    尝试将fastcgi_split_path_info ^(.+\.php)(/.+)$;添加到location ~ \.php$并重启​​nginx。

    我的配置示例

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

    【讨论】:

    • 我有同样的问题,我的配置文件和你提到的一样,仍然无法正常工作
    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多