skip to Main Content

I upgraded my PHP Elastic Beanstalk instance from Amazon Linux 2/3.2.1 to Amazon Linux/2.9.17

In the previous version I had a .htaccess file in my bundle in order to remove file extensions in URLs like this:

www.example.com/page.php?var=123 becomes www.example.com/page?var=123

After the upgrade, www.example.com/page.php?var=123 works fine, but www.example.com/page?var=123 does not work. As a result the navigation in my app is broken.

I learned from other stack overflow questions that with this upgrade of Elastic Beanstalk platform, Apache was replaced with Nginx which is why the .htaccess is not anymore taken into account.

I have zero knowledge of nginx, so I tried researching online how to apply the same behavior with nginx. But what I have done is not working. See below what I have done:

I created a new config file in .ebextensionsnginxconf.dmyconf.conf

Here is the content of the file:

server {
      location / {
          try_files $uri $uri.html $uri/ @extensionless-php;
          index index.html index.htm index.php;
      }

      location ~ .php$ {
          try_files $uri =404;
      }

      location @extensionless-php {
          rewrite ^(.*)$ $1.php last;
      }
  }

Am I only applying part of the config content needed in the file ?

2

Answers


  1. Based on the comments.

    Amazon Linux 2 EB platform uses nginx as default. However, Tomcat, Node.js, PHP, and Python do also support Apache. Since this option is not default, you have to enable it in your .ebextentions:

    option_settings:
      aws:elasticbeanstalk:environment:proxy:
        ProxyServer: apache
    
    Login or Signup to reply.
  2. The answer to the question you seek is very close to what you have already found. The only slight differences to get it to work with Elastic Beanstalk is to save the conf file in .platformconf.delasticbeanstalkmyconf.conf and the contents of that file be

    location / {
        try_files $uri $uri.html $uri/ @extensionless-php;
        index index.html index.htm index.php;
    }
    
    location ~ .php$ {
        try_files $uri =404;
    }
    
    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;
    }
    

    Simply without the server bracket. And then in a .platform/00_myconf.config file

    container_commands:
      01_reload_nginx:
        command: "service nginx reload"
    

    The reason being that the elastic beanstalk service adds everything in the .conf file to an exiting server { } block.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search