【问题标题】:Deploying Symfony App To Elastic Beanstalk将 Symfony 应用程序部署到 Elastic Beanstalk
【发布时间】:2020-09-02 13:35:17
【问题描述】:

我正在尝试将 Symfony 应用程序部署到 Elastic Beanstalk,但似乎 URL 重写不起作用。

使用平台 PHP 7.4 在 64 位 Amazon Linux 2 上运行,文档根目录为 /public

.platform/nginx/conf.d/nginx.conf

server {
  listen        80;
  root /var/app/current/public;
  index index.php;
  location / {
      # try to serve file directly, fallback to index.php
      try_files $uri /index.php$is_args$args;
  }
   location ~ ^/index\.php(/|$) {
    #fastcgi_pass unix:/var/run/php-fpm.sock;
          fastcgi_pass  php-fpm;
          fastcgi_split_path_info ^(.+\.php)(/.*)$;
        #include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
          fastcgi_param DOCUMENT_ROOT $realpath_root;
   }
  access_log    /var/log/nginx/access.log main;
  client_header_timeout 60;
  client_body_timeout   60;
  keepalive_timeout     60;
  gzip                  off;
  gzip_comp_level       4;
  # Include the Elastic Beanstalk generated locations
  #include conf.d/elasticbeanstalk/*.conf;
}

但服务器找不到domain.com/auth/login 的路径(但适用于domain.com/index.php/auth/login

----------------------------------------
/var/log/nginx/error.log
----------------------------------------
2020/05/16 17:32:48 [warn] 3709#0: conflicting server name "" on 0.0.0.0:80, ignored
2020/05/16 17:32:48 [warn] 3736#0: conflicting server name "" on 0.0.0.0:80, ignored
2020/05/16 17:32:48 [warn] 3740#0: conflicting server name "" on 0.0.0.0:80, ignored
2020/05/16 17:33:29 [error] 3744#0: *2 open() "/var/www/html/public/auth/login" failed (2: No such file or directory), client: 172.31.42.4, server: , request: "GET /auth/login HTTP/1.1", host: "..."

如果我删除了 fastcgi_params 的注释标记 (#),则找不到它。

我在/var/log/eb-engine.log 中发现了一个有趣的“错误”:

2020/05/16 18:21:37.054548 [INFO] Running command /bin/sh -c /usr/sbin/nginx -t -c /var/proxy/staging/nginx/nginx.conf
2020/05/16 18:21:37.064522 [ERROR] nginx: the configuration file /var/proxy/staging/nginx/nginx.conf syntax is ok
nginx: configuration file /var/proxy/staging/nginx/nginx.conf test is successful

【问题讨论】:

  • 不知道为什么这个问题被否决了,这是我的问题的解决方案,所以感谢 Eray 提出这个问题和@Sylchauf

标签: symfony amazon-elastic-beanstalk


【解决方案1】:

要在 AWS Linux 2 上启用 url 重写,您必须添加以下文件(从根目录)。不需要修改 nginx.conf

创建文件:

.platform/nginx/conf.d/elasticbeanstalk/01-rewrite.conf

location / {
  try_files $uri /index.php$is_args$args;
}

然后,重新部署您的环境

【讨论】:

    【解决方案2】:

    因为@Sylchauf 的回答非常有帮助,所以我想在这里扩展一些额外的细节以帮助将来向其他人解释它,因为 AWS 在开始时很难导航。

    以下是 AWS 文档的引用:

    https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html

    配置 nginx Elastic Beanstalk 使用 nginx 作为默认反向代理,将您的应用程序映射到您的 Elastic Load Balancing 负载均衡器。 Elastic Beanstalk 提供了一个默认的 nginx 配置,您可以使用自己的配置完全扩展或覆盖它。

    注意 添加或编辑 nginx .conf 配置文件时,请务必将其编码为 UTF-8。

    要扩展 Elastic Beanstalk 默认 nginx 配置,请将 .conf 配置文件添加到应用程序源包中名为 .platform/nginx/conf.d/ 的文件夹中。 Elastic Beanstalk nginx 配置自动在此文件夹中包含 .conf 文件。

    ~/workspace/my-app/ |-- .平台 | -- nginx | --conf.d | -- myconf.conf -- 其他源文件

    在此处仅添加 location 参数可以使其正常工作,而从 Symfony 文档中添加整个推荐配置在 elasticbeanstock 文件夹中不起作用。

    当这个文件上传后,你可以使用 eb ssh 找到它:

    eb ssh
    cd /etc/nginx/conf.d
    

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 2013-12-15
      • 2021-02-11
      • 2018-12-07
      • 2022-06-11
      • 2018-04-21
      • 2017-04-14
      • 2012-07-04
      • 2017-02-17
      相关资源
      最近更新 更多