首页 函数计算(旧版) 服务支持 常见问题 代码开发FAQ Custom Container FAQ 当我使用浏览器或cURL方式访问函数时出现404怎么办?

当我使用浏览器或cURL方式访问函数时出现404怎么办?

更新时间: 2023-10-24 14:58:22

问题现象

我创建了一个Custom Container Runtime的HTTP函数,其中服务名为CustomDemo、函数名为func-http,并且设置了匿名的HTTP触发器,实现Custom Container Runtime的HTTP Server的路由代码示例如下:

@app.route('/test', methods = ['POST','GET'])
def test():
当我使用cURL工具或浏览器等方式访问HTTP函数的URL时,遇到404报错。
  • 使用cURL工具访问HTTP函数。
    curl  -v  https://164901546557****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/CustomDemo/func-http/test
  • 使用浏览器访问HTTP函数。
    https://164901546557****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/CustomDemo/func-http/test
    说明 HTTP函数的URL格式为:https://<account_id>.<region_id>.fc.aliyuncs.com/<version>/proxy/<serviceName>/<functionName>/<path>

解决方案

您可以选择以下任意方式解决该问题:
  • 使用函数计算为新建HTTP触发器分配的子域名重新访问。具体信息,请参见步骤三:测试函数

    子域名格式如下:

    https://<subdomain>.<region_id>.fcapp.run/[action?queries] 

    本文的访问示例如下:

    https://funcname-svcname-khljsjksld.cn-shanghai.fcapp.run/action?hello=world
  • 在访问的命令中增加名为x-fc-invocation-target的Header。访问格式如下:
    curl -v -H "x-fc-invocation-target: 2016-08-15/proxy/$ServiceName/$functionName" https://<account_id>.<region_id>.fc.aliyuncs.com/$path
    本文的访问示例如下:
    curl -v -H "x-fc-invocation-target: 2016-08-15/proxy/CustomDemo/func-http" https://164901546557****.cn-hangzhou.fc.aliyuncs.com/test
  • 为您的函数绑定自定义域名,绑定成功后再执行以下命令重新访问即可。关于绑定域名的操作步骤,请参见配置自定义域名
    假设域名是example.com,访问格式如下:
    curl -v  https://example.com/$path
    本文的访问示例如下:
    curl -v  https://example.com/test
    重要 您需要将路径/$path设置为绑定的自定义域名中设置的与函数名称服务名称对应的路径。更多信息,请参见路由匹配规则
  • 修改您的函数代码,并且成功部署函数后,重新使用默认的URL访问即可。函数代码修改示例如下:
    @app.route('/2016-08-15/proxy/CustomDemo/func-http/test', methods = ['POST','GET'])
    def test():
    本文的访问示例如下:
    curl  -v  https://164901546557****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/CustomDemo/func-http/test
阿里云首页 函数计算(旧版) 相关技术圈