我正在尝试将我的flask api转发到端点为api.mydomain.example的mydomain.example
例如,我的方法ping将有一个端点api.mydomain.example/v1/server/ping。但是,我得到的是xx.xxx.xxx.xx:5005/v1/server/ping作为端点。
查看这里的其他问题,因此我找到了修改app.config['SERVER_NAME']或将subdomain添加到route装饰器的建议。但是什么都不起作用。(当我做这些修改时,我得到了一个404错误)。
下面是一个最小的工作示例:
from flask import Flask, Blueprint
from flask_restplus import Resource, Api
app = Flask(__name__)
api = Api(app, version='1.0',
title='My API',
description='An API')
blueprint = Blueprint('api', __name__, url_prefix='/v1')
api.init_app(blueprint)
app.register_blueprint(blueprint)
#app.config['SERVER_NAME'] = 'mydomain.example:5005'
#app.url_map.default_subdomain = "test"
server = api.namespace("server",
description='Server API')
@server.route("/ping") #, subdomain="test")
class Ping(Resource):
def get(self):
"""
Check if Server is still alive
"""
return {"reply":"PONG"}
if __name__ == '__main__':
app.config["SWAGGER_UI_DOC_EXPANSION"] = "list"
app.run(port=5005, host= '0.0.0.0', debug=True)我转到我的域名管理页面(name.com),添加了转发的url。在这种情况下,mydomain.info会转到swagger管理页面,但mydomain.example/v1/server/ping也会转到swagger管理页面。
但是,我仍然在Request URL部分得到了xx.xxx.xxx.xx:5005。
如何对子域名进行操作?
发布于 2018-05-26 04:52:07
您不必更改您的应用程序配置。您需要做的是在WSGI服务器上运行您的应用程序,该服务器将通过Apache和Nginx等web服务器代理您的应用程序(例如,在端口80上)。WSGI服务器需要一个入口点,以便在您指定的端口(代码片段中的5005)上与您的应用程序通信。Here's一个关于这方面的相当简单的教程(更多细节,你也可以参考this链接)。
此外,API Gateways是您在这里最好的朋友,特别是当您将API发布到公共互联网上时。您可以考虑使用AWS API Gateway或Apigee进行应用程序接口管理,并对Swagger提供一流的支持(还有许多其他的工具-包括开源软件)。
https://stackoverflow.com/questions/50317168
复制相似问题