You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel应用Prometheus指标采集遇Content-Type不兼容问题求助

解决Laravel Prometheus指标Content-Type不匹配问题

针对你遇到的Nginx暴露/prometheus端点返回text/html而非Prometheus期望的text/plain问题,可按以下步骤排查解决:

1. 调整Nginx配置强制设置响应头

PHP-FPM通过FastCGI返回的响应头可能被Nginx默认规则覆盖,需在/prometheus的location块中明确指定Content-Type:

location /prometheus {
    fastcgi_pass php-fpm:9000; # 替换为你的PHP-FPM服务地址
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

    # 隐藏PHP-FPM返回的默认Content-Type,避免冲突
    fastcgi_hide_header Content-Type;
    # 强制设置正确的响应头,always参数确保错误状态码下也生效
    add_header Content-Type 'text/plain; version=0.0.4' always;
}

修改后重启Nginx生效。

2. 确保Laravel中间件正确绑定

如果已添加自定义中间件但未生效,检查以下几点:

  • 中间件是否正确注册:在app/Http/Kernel.php的routeMiddleware数组中添加:
    'prometheus.content-type' => \App\Http\Middleware\SetPrometheusContentType::class,
    
  • 路由是否绑定中间件:给/prometheus路由单独指定中间件:
    Route::get('/prometheus', [PrometheusMetricsController::class, 'output'])
        ->middleware('prometheus.content-type');
    
  • 清除路由缓存:执行php artisan route:clear避免缓存导致中间件未加载。

3. 直接在控制器中返回指定头

若中间件仍不生效,可跳过中间件,直接在控制器方法中设置响应头:

public function output()
{
    // 生成Prometheus指标逻辑,例如用laravel-prometheus包获取指标
    $metrics = app('prometheus')->render();
    
    return response($metrics, 200)
        ->header('Content-Type', 'text/plain; version=0.0.4');
}

4. 排查PHP-FPM默认MIME类型

若以上方法都无效,检查php.ini中的default_mimetype配置,若设置为text/html,可在Laravel入口文件public/index.php开头临时修改:

// 仅对/prometheus请求生效
if ($_SERVER['REQUEST_URI'] === '/prometheus') {
    ini_set('default_mimetype', 'text/plain');
}

内容的提问来源于stack exchange,提问作者Mohamed Jouhari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.01 15:23:10