安全配置
要求
安全中间件功能需要基于FastAPI的跟踪服务器(uvicorn),这是MLflow 3.5.0+的默认服务器。在使用--gunicorn-opts
或--waitress-opts
时,这些功能将不可用。
配置选项
安全设置可以通过CLI选项或环境变量进行配置
设置 | CLI选项 | 环境变量 | 默认值 |
---|---|---|---|
允许的主机 | --allowed-hosts | MLFLOW_SERVER_ALLOWED_HOSTS | localhost, 私有IP |
CORS来源 | --cors-allowed-origins | MLFLOW_SERVER_CORS_ALLOWED_ORIGINS | localhost:* |
X-Frame-Options | --x-frame-options | MLFLOW_SERVER_X_FRAME_OPTIONS | SAMEORIGIN |
禁用安全 | --disable-security-middleware | MLFLOW_SERVER_DISABLE_SECURITY_MIDDLEWARE | false |
--allowed-hosts
控制服务器接受哪些主机头。这通过验证传入的请求来防止DNS重绑定攻击。
# Specific hosts
mlflow server --allowed-hosts "mlflow.company.com,192.168.1.100"
# Wildcard patterns
mlflow server --allowed-hosts "*.company.com,192.168.*"
# Allow all (not recommended)
mlflow server --allowed-hosts "*"
--cors-allowed-origins
指定哪些Web应用程序可以从浏览器发出API请求。
# Specific origins
mlflow server --cors-allowed-origins "https://app.company.com,https://notebook.company.com"
# Wildcard for subdomains
mlflow server --cors-allowed-origins "https://*.company.com"
# Allow all origins (development only)
mlflow server --cors-allowed-origins "*"
--x-frame-options
设置X-Frame-Options头来控制iframe嵌入行为。
SAMEORIGIN
- 仅允许同源嵌入(默认)DENY
- 不允许嵌入NONE
- 任何网站都可以嵌入
# Allow cross-origin iframe embedding
mlflow server --x-frame-options NONE
--disable-security-middleware
完全禁用安全中间件。仅当安全由反向代理或网关处理时才使用此选项。
mlflow server --disable-security-middleware
常见配置
典型部署场景示例
本地开发
默认配置开箱即用。
mlflow server
远程访问
允许来自特定主机的连接。
mlflow server --host 0.0.0.0 --allowed-hosts "mlflow.internal:5000,localhost:*"
Web应用的CORS
启用基于浏览器的应用程序访问API。
mlflow server --cors-allowed-origins "https://notebook.internal"
允许iframe嵌入
启用将UI嵌入到其他应用程序中。
mlflow server --x-frame-options NONE