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 access a custom runtime HTTP function using the proxy URL, Function Compute forwards the full path — including the version, service name, and function name — to your application. If your application registers only a short route such as /test, the paths don't match, and the platform returns 404.

For example, when you access:

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

Function Compute forwards the full path /2016-08-15/proxy/CustomDemo/func-http/test to your application. If your application handles only /test, the request fails.

The proxy URL format is:

SegmentExampleDescription
<account_id>164901546557****Your Alibaba Cloud account ID
<region_id>cn-hangzhouThe region where the function is deployed
<version>2016-08-15FC API version
<serviceName>CustomDemoThe service that contains the function
<functionName>func-httpThe function name
<path>testThe path your application needs to handle

Solution

Choose a method based on your scenario.

Use the HTTP trigger subdomain URL (for testing)

The HTTP trigger allocates a dedicated subdomain for your function. Requests to this URL are routed directly to the function without the /2016-08-15/proxy/<serviceName>/<functionName> prefix, so your /test route works as expected.

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 the subdomain URL for your function, see the "Step 3: Test the function" section in Configure and use an HTTP trigger.

Add the x-fc-invocation-target request header (for cURL)

Add the x-fc-invocation-target header to separate the routing prefix from the path your application handles.

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 (for production)

Binding a custom domain name lets you define routing rules that map domain paths directly to function routes. No changes to your application code are required.

After binding a custom domain name, invoke the function with:

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

Example (with example.com bound to the function):

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

Replace $path with the path defined in the routing rules for your custom domain name. For details, see the "Routing rules" section in Configure a custom domain name.

For setup instructions, see Configure a custom domain name.

Update your function code to use the full path

If you want to keep using the default proxy URL without a header or domain change, update your route to match the full proxy path.

Change this:

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

To this:

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

After deploying the updated function, the original proxy URL works:

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

This approach hard-codes the service name and function name into your application routing. Use it only for testing or when the other options are not available.