[英]symfony 6.2 using api-platform dynamic routes work @localhost but not when deployed on AWS using elastic beanstalk
Using symfony 6.2 with api-platform/core and successfully created several dynamic routes based on #[ApiResources] annotations on Entity classes.将 symfony 6.2 与 api-platform/core 结合使用,并基于实体类上的#[ApiResources] 注释成功创建了多个动态路由。 The localhost environment (mac) http://localhost:8001/api/docs successfully displays the docs and all the endpoints work; localhost环境(mac)http://localhost:8001/api/docs成功显示文档,所有端点工作; However, when deployed to an AWS instance the /api routes all get a 404 error.但是,当部署到 AWS 实例时,/api 路由都会出现 404 错误。 When I set the document root to /public I do successfully get the default symfony web page.当我将文档根目录设置为 /public 时,我确实成功地获得了默认的 symfony 网页。
The AWS deployment: AWS部署:
Platform & Solution Stack Name: PHP 8.1 AL2 version 3.5.2 (64bit Amazon Linux 2 v3.5.2 running PHP 8.1)平台和解决方案堆栈名称:PHP 8.1 AL2 版本 3.5.2(运行 PHP 8.1 的 64 位 Amazon Linux 2 v3.5.2)
Language: PHP 8.1.13语言:PHP 8.1.13
Composer: 2.3.5作曲:2.3.5
Proxy Server: nginx 1.22.0代理服务器:nginx 1.22.0
The config/packages and config/routes where not altered from the initial composer generated install config/packages 和 config/routes 没有从最初的 composer 生成的安装中改变
app/config/routes/api_plaform.yaml应用程序/配置/路由/api_plaform.yaml
api_platform:
resource: .
type: api_platform
prefix: /api
app/config/routes.yaml应用程序/配置/routes.yaml
controllers:
resource:
path: ../src/Controller/
namespace: App\Controller
type: attribute
Each Entity.php class has this kind of #[ApiResource] Annotation:每个 Entity.php 类都有这种 #[ApiResource] 注释:
#[ORM\Entity(repositoryClass: ProjectRepository::class)]
#[ORM\EntityListeners(['App\EventListener\ProjectChangedNotifier'])]
#[ApiResource(
normalizationContext: ['groups' => ['read']],
denormalizationContext: ['groups' => ['write']]
)]
class Project
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
...
Any ideas on how to debug the routes on the AWS instance?关于如何调试 AWS 实例上的路由的任何想法?
The solution to my issue is realizing the AWS default elastic beanstalk php.conf does not provide a location definition needed by a symfony app when using api-platform.我的问题的解决方案是实现 AWS 默认弹性 beanstalk php.conf 不提供使用 api 平台时 symfony 应用程序所需的位置定义。 I solved the issue by providing my own nginx config file that does provides for the proper locations:我通过提供我自己的 nginx 配置文件解决了这个问题,该文件确实提供了正确的位置:
#/etc/nginx/conf.d/elasticbeanstalk/php.conf
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
location ~* \.(?:ico|css|js|gif|webp|jpe?g|png|svg|woff|woff2|eot|ttf|mp4)$ {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
access_log off;
expires 1y;
add_header Pragma public;
add_header Cache-Control "public";
}
location ~ ^/index.php(/|$) {
include /etc/nginx/fastcgi_params;
fastcgi_pass php-fpm;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
AWS elastic beanstalk provides a mechanism to update this file using the.platform folder in your develop directory. AWS elastic beanstalk 提供了一种机制来使用开发目录中的 .platform 文件夹更新此文件。
Checkout this excellent github example by Alexander Schranz of the elastic beanstalk.ebextentions and.platform for a symfony app.查看Alexander Schranz提供的用于 symfony 应用程序的 elastic beanstalk.ebextentions and.platform 的优秀github示例。 It provides for the proper nginx configuration and also several useful deployment scripts needed by most symfony apps.它提供了正确的 nginx 配置以及大多数 symfony 应用程序所需的几个有用的部署脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.