What do I do if a 404 error occurs when I use a browser or the cURL tool to access a function?

更新时间:
复制 MD 格式

When you invoke an HTTP function through the default proxy URL, Function Compute passes the full URL path — including the /2016-08-15/proxy/<serviceName>/<functionName> prefix — into your container as the HTTP request path. If your container's routing code registers only a short path like /test, the full path won't match, and your function returns a 404.

For example, this route:

@app.route('/test', methods=['POST', 'GET'])
def test():

won't match the request path /2016-08-15/proxy/CustomDemo/func-http/test that Function Compute sends to the container. The fix is either to change the URL you use to invoke the function, or to update the route in your code.

Solutions

Pick the approach that best fits your situation:

ApproachBest for
Use the subdomain URLQuickest fix; no code change required
Add the x-fc-invocation-target headercURL-based testing without a custom domain
Bind a custom domain nameProduction traffic; clean URLs
Update the route in your codeKeep using the default proxy URL; accept the full path in code

Use the subdomain URL

Function Compute assigns a dedicated subdomain to each HTTP trigger. Requests sent to this subdomain are routed directly to your function — the prefix /2016-08-15/proxy/<serviceName>/<functionName> is not included in the path your container receives.

Subdomain URL format:

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

Example:

https://funcname-svcname-khljsjksld.cn-shanghai.fcapp.run/action?hello=world

To find your subdomain, see Step 3: Test the function.

Add the x-fc-invocation-target header

Add the x-fc-invocation-target header to your cURL command. This tells Function Compute which service and function to invoke, and your container receives only /$path — not the full proxy prefix.

Command syntax:

curl -v -H "x-fc-invocation-target: 2016-08-15/proxy/$ServiceName/$functionName" \
  https://<account_id>.<region_id>.fc.aliyuncs.com/$path

Example:

curl -v -H "x-fc-invocation-target: 2016-08-15/proxy/CustomDemo/func-http" \
  https://164901546557****.cn-hangzhou.fc.aliyuncs.com/test

Bind a custom domain name

Bind a custom domain name to your function, then invoke the function through that domain. The path your container receives is whatever you configure in the route settings — no proxy prefix is included.

Command syntax:

curl -v https://example.com/$path

Example (domain: example.com, path: /test):

curl -v https://example.com/test
Important

Replace /$path with the path configured in the route settings when you bind the domain. For details, see Route matching rules.

For setup instructions, see Configure a custom domain name.

Update the route in your code

Register the full proxy path in your container's routing code, then redeploy the function.

Update the route from:

@app.route('/test', methods=['POST', 'GET'])
def test():

To:

@app.route('/2016-08-15/proxy/CustomDemo/func-http/test', methods=['POST', 'GET'])
def test():

After redeploying, invoke the function using the original default URL:

curl -v https://164901546557****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/CustomDemo/func-http/test
Note

The default proxy URL format is https://<account_id>.<region_id>.fc.aliyuncs.com/<version>/proxy/<serviceName>/<functionName>/<path>.