我有一些PHP脚本,它返回内容types为“image / jpeg”的jpeg图像(1×1像素):
// return image $image_name = 'img/pixel.jpg'; $image = fopen($image_name, 'rb'); header('Content-Length: ' . filesize($image_name)); header('Content-Type: image/jpeg'); fpassthru($image);
这个脚本在php5-fpm模块上运行在nginx / 1.2.1上。 问题是,匹配“ location〜\ .php $ ”请求的所有响应都有Content-Type头文件“ text / html; charset = UTF-8 ”,忽略我的php函数头('Content-Type:image / jpeg') 。 因此,我得到了与“文本/ HTML”内容types的JPEG图片。
以下是我的虚拟主机的简化configuration:
server { listen 80; server_name localhost default_server; set $main_host "localhost"; root /var/www/$main_host/www; location / { root /var/www/$main_host/www/frontend/web; try_files $uri /frontend/web/index.php?$args; location ~* ^/(.+\.(css|js|jpg|jpeg|png|gif|bmp|ico|mov|swf|pdf|zip|rar))$ { try_files $uri /frontend/web/$1?$args; } } location /admin { alias /var/www/$main_host/www/backend/web; try_files $uri /backend/web/index.php?$args; location ~* ^/admin/(.+\.php)$ { try_files $uri /backend/web/$1?$args; } location ~* ^/admin/(.+\.(css|js|jpg|jpeg|png|gif|bmp|ico|mov|swf|pdf|zip|rar))$ { try_files $uri /backend/web/$1?$args; } } location ~ \.php$ { try_files $uri /frontend/web$uri =404; include fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.www.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
你确定它是nginx,而不是PHP添加Content-type: text/html吗? 这似乎不是从你的粘贴configuration的方式。 这可能是你有其他的PHP代码,这是设置它第一。 尝试改变你的PHP头调用看起来像这样:
header('Content-Type: image/jpeg', true);
第二个参数会覆盖其他之前的特定头文件的调用。
您可能还需要查看$upstream_http_content_type ,这是一个包含PHP发出的Content-type头的nginxvariables。 如果你需要一个丑陋的黑客攻击,你可以在你的nginxconfiguration中使用if语句。