问题现象
我创建了一个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://123456789.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/CustomDemo/func-http/test
- 使用浏览器访问HTTP函数。
https://123456789.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/CustomDemo/func-http/test
说明 HTTP函数的URL格式为:https://$ACCOUNT_ID.$REGION.fc.aliyuncs.com/2016-08-15/proxy/$ServiceName/$functionName/$path。
解决方案
您可以选择以下任意方式解决该问题:
- 在访问的命令中增加名为
x-fc-invocation-target
的Header。访问格式如下:
本文的访问示例如下:curl -v "x-fc-invocation-target: 2016-08-15/proxy/$ServiceName/$functionName" https://$ACCOUNT_ID.$REGION.fc.aliyuncs.com/$path
curl -v -H "x-fc-invocation-target: 2016-08-15/proxy/CustomDemo/func-http" https://123456789.cn-hangzhou.fc.aliyuncs.com/test
- 为您的函数绑定自定义域名,绑定成功后再执行以下命令重新访问即可。关于绑定域名的操作步骤,请参见绑定自定义域名。
假设域名是
test.abc.com
,访问格式如下:
本文的访问示例如下:curl -v https://test.abc.com/$path
curl -v https://test.abc.com/test
注意 您需要将路径设置为/*
,本文对应的服务名为CustomDemo、函数名为func-http。 - 修改您的函数代码,并且成功部署函数后,重新使用默认的URL访问即可。函数代码修改示例如下:
本文的访问示例如下:@app.route('/2016-08-15/proxy/CustomDemo/func-http/test', methods = ['POST','GET']) def test():
curl -v https://123456789.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/CustomDemo/func-http/test