2

我目前正在开发托管在亚马逊 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')

提前感谢大家提供的任何帮助。

4

1 回答 1

1

我确实发表了评论,但我设法在测试中准确地重现了该错误。

您不能绑定到地址 example.com 并且 IP 地址未在本地计算机上列出。我可以绑定到 mydomain.com,因为它解析到我服务器上的地址。

此外,我认为示例中存在拼写错误,我认为您的意思是host=app.config['SERVER_NAME']没有缺少应用程序或小写的 SERVER_NAME?

于 2013-08-15T09:51:57.717 回答