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

Magento 2健康检查文件未找到:能否使用/health_check.php作为检查路径?

Magento 2 自定义健康检查路径配置方案

问题根源

Magento 2 默认以pub/index.php作为前端请求入口,所有外部请求都会被重定向到该文件处理。直接访问/health_check.php时,服务器未正确将请求映射到pub目录下的对应文件,因此返回「file not found」错误。

解决方法

方法1:调整服务器配置(推荐)

根据你使用的服务器类型,修改配置让/health_check.php请求指向pub目录下的文件:

Nginx 配置修改

在Magento的Nginx配置文件中,添加以下规则(放在location ~ (index|get|static|report|404|503)\.php$规则之前):

location /health_check.php {
    root /path/to/your/magento/pub;
    try_files $uri =404;
    fastcgi_pass   fastcgi_backend;
    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=756M \n max_execution_time=18000";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

修改后重启Nginx:

sudo systemctl restart nginx

Apache 配置修改

在Magento根目录的.htaccess文件中添加规则:

RewriteRule ^health_check.php$ pub/health_check.php [L]

或在Apache虚拟主机配置中添加别名:

Alias /health_check.php /path/to/your/magento/pub/health_check.php
<Directory /path/to/your/magento/pub>
    AllowOverride All
    Require all granted
</Directory>

修改后重启Apache:

sudo systemctl restart apache2

方法2:创建软链接(临时快速方案)

在Magento项目根目录下,创建指向pub目录中health_check.php的软链接:

ln -s pub/health_check.php health_check.php

需确保服务器允许跟随软链接(Nginx默认支持,Apache需开启FollowSymLinks)。

方法3:使用Magento内置路由(框架贴合方案)

若不想单独处理静态文件,可创建自定义Magento控制器实现健康检查:

  • 创建自定义模块(如Vendor/HealthCheck)
  • 配置路由和控制器,让其返回200状态码
  • 将AWS目标组健康检查路径设为/health-check(对应自定义路由)
    该方案更符合Magento架构,但开发成本略高。

验证配置

修改完成后,直接访问domain-name.com/health_check.php,若返回200状态码及预期内容,即可在AWS目标组中更新健康检查路径。

内容的提问来源于stack exchange,提问作者Wira Xie

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 22:25:23