为啥 AWS Elastic Beanstalk Python 优先插入“静态”规则在所有其他规则之前?

Posted

技术标签:

【中文标题】为啥 AWS Elastic Beanstalk Python 优先插入“静态”规则在所有其他规则之前?【英文标题】:Why does AWS Elastic Beanstalk Python insert a 'static' rule ahead of all others in priority?为什么 AWS Elastic Beanstalk Python 优先插入“静态”规则在所有其他规则之前? 【发布时间】:2015-04-26 03:46:45 【问题描述】:

我的 Python 应用程序的“静态”路由规则在我的 AWS Elastic Beanstalk 应用程序中表现异常(其他任何地方都没有),似乎覆盖了所有其他规则。

例如,使用下面的两个函数,在我的开发机器和其他地方的测试服务器以及 AWS 上,routes 将静态规则列在最后,match_route 显示其他非静态规则匹配以 ' 开头的路径静止的/...'。正如预期的那样,如果我在我的非 AWS 机器上导航到路径以 static/... 开头的页面,则匹配我的其他(非静态)规则之一。但是()在 AWS-EB 上,会为此类路径调用服务器的静态规则!

AWS-EB 为什么以及如何在所有其他规则之前“插入”这条规则?如何在 AWS 上禁用此行为,或在我的非 AWS 系统中复制它?


application.url_map.host_matching = True
# ...

def routes(verbose, wide):
    """List routes supported by the application"""
    for rule in sorted(app.url_map.iter_rules()):
        if verbose:
            fmt = ":45s :30s :30s" if wide else ":35s :25s :25s"
            line = fmt.format(rule, rule.endpoint, ','.join(rule.methods))
        else:
            fmt = ":45s" if wide else ":35s"
            line = fmt.format(rule)

        print(line)

def match_route(host, match):
    """Match a route for a given host"""
    if match is not None:
        urls = app.url_map.bind(host)
        try:
            m = urls.match(match, "GET")
            z = '()'.format(m[0], ','.join(["=''".format(arg, m[1][arg]) for arg in m[1]] +
                                               ["host=''".format(host)]))
            return z
        except NotFound:
            return

【问题讨论】:

【参考方案1】:

这是 /etc/httpd/conf.d/wsgi.conf 中 Apache 服务器配置的结果,其中包含

Alias /static/ /opt/python/current/app/static/

如果您删除或注释掉该行,服务器将不再“拦截”以 'static' 开头的路径。

然而,实现这一点比人们想象的要复杂一些,因为wigs.conf 文件被(重新)创建after files 被上传并commands 被执行。

解决此问题的一种方法是使用部署后挂钩修改文件,确保之后重新启动网络服务器:

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/remalias.sh" :
    mode: "00775"
    owner: root
    group: root
    content: |
      sed -i.backup -e 's/^Alias\s[/]static[/]\s[a-z/]*$//g' /etc/httpd/conf.d/wsgi.conf
      service httpd restart

【讨论】:

以上是关于为啥 AWS Elastic Beanstalk Python 优先插入“静态”规则在所有其他规则之前?的主要内容,如果未能解决你的问题,请参考以下文章

在 AWS Elastic Beanstalk 和 EKS 上部署了一个 laravel 应用程序 相同的数据库 RDS 为啥在 Elastic Beanstalk 中获得快速响应

AWS Elastic Beanstalk - 为啥我要使用 leader_only 命令?

为啥 AWS Route 53 主机重定向到 Elastic Beanstalk 服务器?

为啥 AWS Elastic Beanstalk Python 优先插入“静态”规则在所有其他规则之前?

为啥我的 Rails 应用程序无法部署到 AWS Elastic Beanstalk?

为啥在 AWS Elastic BeanStalk 中将 Web 服务器更改为 nginx 时我的网页标题显示“Apache”