我目前正在开发托管在亚马逊 AWS 上的 Flask 应用程序。我正在处理的应用程序有几个组件,我正在处理其中的两个。我希望为这些组件设置子域,尽管遇到了一些麻烦。
我想 :
- www.example.com > GoDaddy
- 仪表板.example.com > AWS
- api.example.com > AWS
子域是一个静态站点,www
一切正常。我能够设置 Route53 和 GoDaddy,以便 dashboard.example.com 和 api.example.com 访问运行我的 Flask 应用程序的 EC2 服务器。我已经确认了这一点,因为当我使用浏览器访问域时,我可以看到请求(即 404ing)。
我遇到的问题是 Flask 期望app.config.SERVER_NAME
将 设置为服务器名称,该名称也必须与 URI 匹配。我的 EC2 实例的服务器名称是ec2-###-###-###-###.compute-1.amazonaws.com.
如果我将我的服务器名称设置为此,Flask 应用程序运行良好,我得到一个 404 页面,当我 GET 到 dashboard.example.com 或 api.example 时,通过观察 Flask 应用程序的输出来查看请求。 com。
不显示 404 页面app.config.SERVER_NAME
应该设置为 example.com,但如果我这样做,Flask 应用程序将不会运行,我得到socket.error: [Errno 99] Cannot assign requested address.
这是我的代码:
from flask import \
Flask, \
request
# my imports
from dashboard import blueprint as dashboard
app = Flask(__name__)
@app.route('/')
def catch():
return request.url
def main():
print('Starting main ...')
app.register_blueprint(dashboard)
if __name__ == '__main__':
main()
app.config['SERVER_NAME'] = 'example.com'
app.run(
host=app.config['server_name'],
port=80,
debug=True,
)
这是我的仪表板模块:
from flask import \
render_template, \
Blueprint,\
Response
blueprint = Blueprint(
'dashboard', __name__,
static_folder='static',
template_folder='templates',
subdomain='dashboard',)
@blueprint.route('/', methods=['GET'])
def _():
return render_template('index.html')
@blueprint.route('/js/')
def _js(path):
print('js : %s' % path)
with blueprint.open_resource('static/js/' + path, mode='r') as f:
content = f.read()
return Response(content, mimetype='text/javascript')
@blueprint.route('/css/')
def _css(path):
print('css : %s' % path)
with blueprint.open_resource('static/css/' + path, mode='r') as f:
content = f.read()
return Response(content, mimetype='text/css')
提前感谢大家提供的任何帮助。