You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flask-restplus API中将IP端口替换为域名子域名?

解决Flask API子域名api.mydomain.example生效问题

我来帮你梳理下问题的核心和解决办法——你现在遇到的问题本质上是URL转发(跳转)和反向代理的区别,加上Flask需要正确识别请求的Host头才能生成正确的域名链接,以下是分步解决方案:

1. 替换URL转发为反向代理

你当前用的域名URL转发(跳转)是浏览器端的重定向,这意味着Flask收到的请求始终是来自服务器IP(xx.xxx.xxx.xx:5005),所以Swagger页面和API返回的链接都会显示IP。正确的做法是用反向代理(比如Nginx),让服务器端把api.mydomain.example的请求转发到Flask服务。

Nginx反向代理配置示例

在你的服务器上安装Nginx后,添加如下配置(路径通常是/etc/nginx/sites-available/api.mydomain.example):

server {
    listen 80;
    server_name api.mydomain.example;

    location / {
        # 转发到你的Flask服务地址
        proxy_pass http://localhost:5005;
        # 关键:把真实的域名Host头传递给Flask
        proxy_set_header Host $host;
        # 可选:传递客户端真实IP等信息
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

保存后启用配置并重启Nginx:

sudo ln -s /etc/nginx/sites-available/api.mydomain.example /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

2. 修正Flask配置和代码

现在Flask能通过反向代理拿到真实的api.mydomain.example Host头,接下来调整代码:

修改后的Flask代码

from flask import Flask, Blueprint
from flask_restplus import Resource, Api

app = Flask(__name__)
# 设置真实的子域名,不需要端口(Nginx处理80/443端口)
app.config['SERVER_NAME'] = 'api.mydomain.example'
# 如果后续启用HTTPS,添加这个配置
# app.config['PREFERRED_URL_SCHEME'] = 'https'

# 初始化蓝图和API
blueprint = Blueprint('api', __name__, url_prefix='/v1')
# 把API绑定到蓝图上
api = Api(blueprint, version='1.0', title='My API', description='An API')
app.register_blueprint(blueprint)

server = api.namespace("server", description='Server API')

@server.route("/ping")
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"
    # 生产环境建议关闭debug,host保持0.0.0.0没问题
    app.run(port=5005, host='0.0.0.0')

3. 为什么之前的设置无效?

  • URL转发的问题:跳转只是让浏览器从mydomain.example跳转到IP,Flask从未收到过域名的请求,自然无法生成域名链接。
  • subdomain参数无效:Flask的subdomain依赖请求的Host头,如果请求的Host是IP,Flask无法匹配子域名路由,导致404。
  • SERVER_NAME配置错误:你之前加了端口:5005,但反向代理后Flask收到的请求端口是80/443,所以端口配置会导致路由不匹配。

4. 验证配置

  • 确保域名解析正确:api.mydomain.example的A记录指向你的服务器IP(不要用URL转发记录)。
  • 测试API:访问http://api.mydomain.example/v1/server/ping,应该返回{"reply":"PONG"}。
  • 查看Swagger页面:访问http://api.mydomain.example/v1,此时Swagger里的请求URL应该显示api.mydomain.example而不是IP。

内容的提问来源于stack exchange,提问作者zeh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:27:37