Deploying Symfony App To Elastic Beanstalk

Viewed 848

I'm trying to deploy a Symfony application to Elastic Beanstalk but it looks like URL rewriting is not working.

Using platform PHP 7.4 running on 64bit Amazon Linux 2 and the document root is /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;
}

But server cannot find the path for domain.com/auth/login (but works for 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: "..."

If I remove the comment mark (#) for fastcgi_params, it's failing to find it.

I found an interesting 'Error' in /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
2 Answers

To enable url rewriting on AWS Linux 2, you must add the following file (from root directory). You don't need to modify nginx.conf

Create the file :

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

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

Then, redeploy your env

Because the answer from @Sylchauf helped out so much, I wanted to extend some additional details here to help explain it to someone else in the future, as AWS is pretty tough to navigate when getting started.

Below is a quote for the AWS documentation:

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

Configuring nginx Elastic Beanstalk uses nginx as the default reverse proxy to map your application to your Elastic Load Balancing load balancer. Elastic Beanstalk provides a default nginx configuration that you can extend or override completely with your own configuration.

Note When you add or edit an nginx .conf configuration file, be sure to encode it as UTF-8.

To extend the Elastic Beanstalk default nginx configuration, add .conf configuration files to a folder named .platform/nginx/conf.d/ in your application source bundle. The Elastic Beanstalk nginx configuration includes .conf files in this folder automatically.

~/workspace/my-app/ |-- .platform | -- nginx | -- conf.d | -- myconf.conf -- other source files

Adding just the location parameter here allowed for this to work, while adding the entire recommended configuration from the Symfony docs did not work in the elasticbeanstock folder.

When this file is uploaded, you can find it by using eb ssh:

eb ssh
cd /etc/nginx/conf.d
Related