【Azure 应用服务】Azure Function HTTP Trigger 遇见奇妙的500 Internal Server Error: Failed to forward request to http://169.254.130.x

问题描述

使用 Azure Funciton App,在本地运行完全成功的Python代码,发布到Azure Function就出现了500  Internal Server Error. 而且错误消息也是莫名的 Failed to forward request to http://169.254.130.x。

2022-07-11T11:46:01.646550271Z Failed to forward request to http://169.254.130.7.

Encountered a System.Net.Http.HttpRequestException exception after 562.442ms with message: Received an invalid status line: 'HTTP/1.1 1 '..

Check application logs to verify the application is properly handling HTTP traffic.

问题解决

在Azure Function的Test/Run页面,根据在代码中添加的日志标签,可以准确的定位到错误发生在29号代码。

 

 第29行代码为:

        return func.HttpResponse(f"Logic Hello, {dresult}. This HTTP triggered function executed successfully.",status_code=1)

与默认生成的Python Function模板代码的区别只有 status_code 设置为了1. 默认的没有指定。

        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")

所以非常怀疑就是这里的问题. 查看源码,默认值就是200, 但是这里也没有说不允许设置为1啊!

 

但是为了验证是这里的问题。修改代码,设置 status_code 为200后,再次部署到Azure Function中。 经过测试,奇妙的500错误消息。

如此可以成功判断: 是由于status_code为1导致了Failed to forward request to http://169.254.130.x的异常。

 

至此,问题解决。关于为什么status_code不能设置为1,并且去请求http://169.254.130.x 得地址,因为无法在云环境中Debug而无从得出结论。

 

 

【如以后有更多的资料后,在进行分析。。。】

 

posted @ 2022-07-11 20:18  路边两盏灯  阅读(158)  评论(1编辑  收藏  举报