问题描述
我的Python应用程序的静态路由规则奇怪的行为在我的AWS弹性魔豆的应用程序(和别的地方),出现覆盖所有其他规则。
例如,使用下面的两个功能,对我的两个开发机器和其他地方,并在AWS上测试服务器,路线
列出静态规则最后,与 match_route
显示其他非静态规则匹配开头为静态/ ...'的路径。和预期的一样,如果我浏览到一个页面,以静态/...
在我的非AWS机开始的路径,我的一个其他(非静态)规则匹配。然而(的只有的)的AWS-EB,服务器的静态规则调用了这样的路径!
为什么要和AWS-EB是怎样插入这一规则领先于所有其他的?如何我要么禁用AWS这种行为,或复制它在我的非AWS系统?
application.url_map.host_matching = TRUE
#...
高清路线(详细,宽):
列出路由所支持的应用
在排序(app.url_map.iter_rules())规则:
如果详细:
FMT ={:45秒} {:30秒} {:30秒}如果还有广{:35S} {:25秒} {:25秒}
行= fmt.format(规则,rule.endpoint,',',加入(rule.methods))
其他:
FMT ={:45秒}如果还有广{:35S}
行= fmt.format(规则)
打印(行)
高清match_route(主机,匹配):
匹配的路由给定主机
如果比赛是不是没有:
网址= app.url_map.bind(主机)
尝试:
M = urls.match(比赛,GET)
Z ='{}({})。格式(M [0],',',加入([{} ='{}'。格式(阿根廷,米[1] [ARG])的ARG中米[1]] +
[主机='{}'。格式(主机)))
回报ž
除了NOTFOUND:
返回
这是在Apache服务器配置的结果 /etc/httpd/conf.d/wsgi.conf
包含
别名/静态/的/ opt /蟒蛇/电流/ APP /静态/
如果您删除或注释掉该行,该服务器将不再是拦截开头的路径静态。
实现这一点,但是,有一点麻烦比人们猜测,因为 wigs.conf
文件中获得(重新)创建的 target="_blank" lml-data-x="https://www.lmlphp.com/r?x=CtQ9yf-VsMQVPio6PL2_sxkdPL3VzNwGzM9V4En7yiQ3PMhrPEw_2ekmCjbmPLQryi8V4eyVPiK_2ebdCL3rsEIVzAQ7CEwryAod4Eojsx79zEOGPiK_2ebdCL3rsEIVzAQ7CEwryAod4xbjzEk9sE43zeK_5tOwp8"相对=nofollow> 载和命令执行。
解决此的一种方法是修改与部署后钩的文件,应确保之后重新启动的网络服务器:
文件:
/opt/elasticbeanstalk/hooks/appdeploy/post/remalias.sh:
模式:00775
老板:根
组:根
内容:|
SED -i.backup -e'S / ^别名\ S [/]静态[/] \ S [AZ /] * $ // G'/etc/httpd/conf.d/wsgi.conf
服务的httpd重新启动
The 'static' routing rule for my Python application is behaving strangely in my AWS Elastic Beanstalk application (and nowhere else), appearing to override all other rules.
For example, using the two functions below, on both my development machines and test servers elsewhere and on AWS, routes
list the static rule last, and match_route
shows other non-static rules matching paths that begin with 'static/...'. And as expected, if I navigate to a page with a path that starts with static/...
on my non-AWS machines, one of my other (non-static) rules is matched. However (only) on AWS-EB, the server's static rule is invoked for such paths!
Why and how is AWS-EB "inserting" this rule ahead of all others? How do I either disable this behavior on AWS, or replicate it in my non-AWS systems?
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
This is a result of the Apache server configuration in /etc/httpd/conf.d/wsgi.conf
which contains
Alias /static/ /opt/python/current/app/static/
If you delete or comment out that line, the server will no longer "intercept" paths that start with 'static'.
Accomplishing this is, however, a bit trickier than one might guess, since the wigs.conf
file gets (re)created after files are uploaded and commands are executed.
One way around this is to modify the file with a post-deployment hook, being sure to restart the webserver afterwards:
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弹性魔豆Python中插入一个“静态”的规则领先于所有其他的优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!