从nginx上的php返回图片

时间:2014-04-21 02:09:41

标签: php nginx

以下似乎是使用php从数据库返回图像的非常标准的代码。 当我使用php cli(和一个硬编码名称)运行代码时,我从我的数据库中获得了正确的内容(无法理解的blob,当保存到文件时可以静态显示)。但是,当我尝试通过浏览器访问相同内容时,没有任何内容出现。 chrome中的开发工具报告标题大小为183 B,内容大小为0.

<?php
$conn = odbc_connect('MySQL','','');
$query = "SELECT img, length(img) as length from imgs WHERE name = '".$_GET['name']."'";
$result = odbc_exec($conn, $query);

$row = odbc_fetch_array($result);
header("content-type: image/jpeg");
header("Content-Length: ".$row['length']);
echo $row['img'];
?>

我想也许这与我的nginx配置文件

有关
server {
    location / {
        root /var/www/html;
    }

    location /images/ {
        root /data;
    }

    location ~ /.*\.php$ {
        if ($fastcgi_script_name ~ /(.*\.php)$) {
            set $valid_fastcgi_script_name $1;
        }
        fastcgi_pass    unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME    /var/www/cgi-bin/$valid_fastcgi_script_name;
        include         fastcgi_params;
    }
}

有什么想法吗?感谢。

2 个答案:

答案 0 :(得分:0)

我的猜测是你需要在将内容发送到浏览器后刷新fastcgi请求。 您应该使用fastcgi_finish_request来刷新请求。 见http://www.php.net/manual/en/function.fastcgi-finish-request.php

答案 1 :(得分:0)

原来我被SELinux再次挫败了。非常感谢