简体   繁体   English

Elastic Beanstalk - 升级后将 .htaccess 替换为 nginx

[英]Elastic Beanstalk - replace .htaccess for nginx after upgrade

I upgraded my PHP Elastic Beanstalk instance from Amazon Linux 2/3.2.1 to Amazon Linux/2.9.17我将我的 PHP Elastic Beanstalk 实例从 Amazon Linux 2/3.2.1 升级到 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:在以前的版本中,为了删除 URL 中的文件扩展名,我的包中有一个 .htaccess 文件,如下所示:

www.example.com/page.php?var=123 becomes www.example.com/page?var=123 www.example.com/page.php?var=123变为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.升级后, www.example.com/page.php?var=123 /page.php?var=123 可以正常使用,但www.example.com/page?var=123不可用。 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.我从其他堆栈溢出问题中了解到,随着 Elastic Beanstalk 平台的升级,Apache 被替换为 Nginx,这就是为什么不再考虑 .htaccess 的原因。

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.我对 nginx 的了解为零,所以我尝试在线研究如何对 nginx 应用相同的行为。但我所做的是行不通的。 See below what I have done:请参阅下面我所做的:

I created a new config file in.ebextensions\nginx\conf.d\myconf.conf我在 .ebextensions\nginx\conf.d\myconf.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?我是否只应用了文件中需要的部分配置内容?

Based on the comments.根据评论。

Amazon Linux 2 EB platform uses nginx as default. Amazon Linux 2 EB 平台默认使用 nginx。 However, Tomcat, Node.js, PHP , and Python do also support Apache. 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 :由于此选项不是默认选项,因此您必须在.ebextentions启用它:

option_settings:
  aws:elasticbeanstalk:environment:proxy:
    ProxyServer: apache

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.platform\conf.d\elasticbeanstalk\myconf.conf and the contents of that file be让它与 Elastic Beanstalk 一起工作的唯一细微差别是将 conf 文件保存在.platform\conf.d\elasticbeanstalk\myconf.conf 中,该文件的内容是

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然后在 a.platform/00_myconf.config 文件中

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.原因是弹性 beantalk 服务将 .conf 文件中的所有内容添加到现有的服务器 {} 块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM