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

CakePHP v4.5部署AWS Elastic Beanstalk后非根路由404及配置失败问题

AWS Elastic Beanstalk部署CakePHP 4.5路由404问题解决方法

问题症状

  • 根路由(/)可正常访问,其他路由均返回404 Not Found
  • Nginx日志显示尝试访问物理文件(如/var/www/html/webroot/painel)而非转发至CakePHP入口文件
  • 添加自定义Nginx配置后部署失败,报错无法找到fastcgi_params文件

解决方案

1. 创建正确的Nginx配置文件

在项目根目录下创建路径.platform/nginx/conf.d/elasticbeanstalk/,并在该目录下新建cakephp.conf文件,写入以下内容:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

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

2. 确认文档根目录设置

保持AWS环境变量中已配置的文档根目录为/webroot,无需修改。

3. 重新部署应用

将新增的配置文件提交到代码仓库后,触发Elastic Beanstalk重新部署,Nginx将正确处理动态路由请求。

问题原因解析

  • 初始404:默认Nginx配置未将非静态文件请求转发至CakePHP的index.php,而是直接查找物理文件。
  • 配置后部署失败:直接在.platform/nginx/conf.d/下添加自定义配置会破坏Elastic Beanstalk的默认配置结构,导致fastcgi_params路径无法被正确解析。将配置放在conf.d/elasticbeanstalk/目录下,会被默认配置正确加载,避免路径错误。

内容的提问来源于stack exchange,提问作者Maike Da Silva Castilho

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 19:54:58