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

请求协助将Python Azure Functions迁移至Azure VM

迁移Azure Functions到Azure VM的HTTPS端点方案

核心结论

你必须使用Web框架(如FastAPI/Flask)搭配WSGI/ASGI服务器来托管你的Python脚本——Azure Functions之所以能让你用独立脚本提供HTTPS端点,是因为背后有Azure托管的Functions Runtime帮你处理了HTTP路由、端口监听、HTTPS终止等底层工作;而Azure VM是空白的服务器环境,没有这套托管机制,得自己搭建HTTP服务栈。

为什么不能直接跑独立脚本?

如果直接运行普通Python脚本,它没有内置的HTTP服务器能力,无法监听网络端口、解析HTTP请求、响应客户端的HTTPS连接。除非你自己手动实现TCP socket监听、HTTP协议解析、SSL证书加载等逻辑——这完全是重复造轮子,不仅耗时,还会引入大量稳定性、安全性问题,维护成本极高,完全不推荐。

最佳实践方案

推荐用轻量Web框架+生产级服务器+反向代理的组合,既能快速迁移现有Functions逻辑,又能保证生产环境的稳定性和安全性:

1. 选择合适的Web框架

  • FastAPI:优先推荐,性能优于Flask,原生支持异步,自动生成API文档,和Azure Functions的HTTP触发逻辑适配性很高,迁移成本低。
  • Flask:更轻量化,学习曲线低,适合逻辑简单的端点。

2. 封装现有Functions逻辑

把每个Azure Function的HTTP触发逻辑,改成框架的路由处理函数。比如:
原来的Azure Function代码:

import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')
    if name:
        return func.HttpResponse(f"Hello, {name}.")
    else:
        return func.HttpResponse("Please pass a name.", status_code=400)

改成FastAPI版本:

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

app = FastAPI()

class NameRequest(BaseModel):
    name: str

# 对应原Function的HTTP触发路径
@app.get("/api/hello")
@app.post("/api/hello")
def hello(name: str = None, req_body: NameRequest = None):
    # 复用原有的参数解析逻辑
    target_name = name or (req_body.name if req_body else None)
    if not target_name:
        raise HTTPException(status_code=400, detail="Please pass a name.")
    return {"message": f"Hello, {target_name}."}

3. 配置生产级服务器

不要用框架自带的开发服务器(比如Flask的app.run()、FastAPI的uvicorn直接启动),生产环境要用稳定的WSGI/ASGI服务器:

  • FastAPI搭配:gunicorn + uvicorn.workers.UvicornWorker(进程管理+异步支持)
  • Flask搭配:gunicorn或uwsgi

启动命令示例(FastAPI):

gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app --bind 0.0.0.0:8000
  • -w 4:启动4个工作进程(根据VM CPU核心数调整)
  • --bind 0.0.0.0:8000:监听所有网卡的8000端口

4. 配置HTTPS与反向代理

推荐用Nginx做反向代理来处理HTTPS终止,比让应用服务器直接加载证书更灵活(还能做负载均衡、静态文件托管、请求限流等):

  1. 安装Nginx:sudo apt install nginx(Ubuntu/Debian)或sudo yum install nginx(CentOS/RHEL)
  2. 申请SSL证书:用Let's Encrypt的certbot工具免费申请自动续期的证书,命令:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com
  1. 配置Nginx反向代理(编辑/etc/nginx/sites-available/default):
server {
    listen 443 ssl;
    server_name your-domain.com;

    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;

    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

# 把HTTP请求重定向到HTTPS
server {
    listen 80;
    server_name your-domain.com;
    return 301 https://$host$request_uri;
}
  1. 重启Nginx:sudo systemctl restart nginx

5. 配置进程管理

用systemd把应用服务器做成系统服务,确保VM重启后自动启动:

  1. 创建服务文件/etc/systemd/system/fastapi.service:
[Unit]
Description=FastAPI Endpoints Service
After=network.target

[Service]
User=azureuser  # 替换成你的VM用户名
WorkingDirectory=/home/azureuser/my-endpoints  # 替换成你的代码目录
ExecStart=/home/azureuser/my-endpoints/venv/bin/gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app --bind 0.0.0.0:8000
Restart=always  # 进程崩溃时自动重启

[Install]
WantedBy=multi-user.target
  1. 启用并启动服务:
sudo systemctl daemon-reload
sudo systemctl enable fastapi.service
sudo systemctl start fastapi.service

6. 环境与依赖管理

  • 创建虚拟环境:python -m venv venv
  • 激活虚拟环境:source venv/bin/activate(Linux)或venv\Scripts\activate(Windows)
  • 安装依赖:把原Azure Functions的requirements.txt复制过来,执行pip install -r requirements.txt

额外建议

  • 如果你有5个独立的端点,可以把它们都放在同一个FastAPI/Flask应用里,用不同的路由区分;如果逻辑过于独立,也可以拆分多个应用,用Nginx做路由转发。
  • 监控:用Azure Monitor监控VM的CPU、内存状态,或者用prometheus+grafana监控应用服务器的请求量、响应时间。
  • 版本管理:用Git托管代码,方便部署回滚和多环境同步。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 23:13:19