如何将Python可执行文件部署至Azure App Service并实现域名访问?
解决Windows Azure App Service部署Flask打包.exe的域名访问问题
核心问题
Azure App Service Windows环境默认对外暴露80/443端口,你的.exe当前仅监听8000端口,且缺少IIS与.exe之间的路由配置,导致域名请求无法转发到8000端口。
具体配置步骤
1. 确认exe的监听配置
确保你的Flask应用(打包前的代码)启动时监听0.0.0.0和8000端口,保证能接收外部请求:
if __name__ == '__main__': app.run(host='0.0.0.0', port=8000)
若已打包成exe且代码未做此配置,需重新打包。
2. 创建web.config文件(关键)
在/site/wwwroot目录下创建web.config文件,用于配置IIS转发请求到你的exe:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <handlers> <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/> </handlers> <httpPlatform processPath=".\你的应用文件名.exe" arguments="" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" startupTimeLimit="30"> <environmentVariables> <environmentVariable name="PORT" value="8000" /> </environmentVariables> </httpPlatform> </system.webServer> </configuration>
- 将
你的应用文件名.exe替换为实际的exe文件名 - 开启
stdoutLogEnabled可在/site/wwwroot/logs目录查看运行日志,方便排查问题
3. 配置启动命令(备选)
若web.config未生效,进入Azure Portal的App Service管理页:
- 进入「配置」>「通用设置」
- 在「启动命令」中填入:
.\你的应用文件名.exe - 保存后重启App Service
4. 排查验证
- 确认exe未硬编码监听
127.0.0.1,必须是0.0.0.0才能接收外部请求 - 查看App Service的「日志流」或
/site/wwwroot/logs/stdout文件,排查启动失败或请求转发异常的原因
内容的提问来源于stack exchange,提问作者Ajith
相关产品推荐
相关产品推荐

