Cannot get the real client IP
Symptom
After an edge function processes a request, the IP address retrieved from the ali-real-client-ip header is always an internal IP address, not the real client IP.
Cause
When you use fetch mode, the edge function acts as an intermediate proxy that sends requests to the origin. The origin receives requests from the edge runtime, causing the ali-real-client-ip header to return the edge node's internal IP address instead of the real client IP.
Solution
Enable Bypass Mode to prevent the edge function from processing requests. This ensures the original client IP is preserved. For detailed instructions, see the bypass mode section in Domain Configuration.
The following table describes the key differences between fetch mode and bypass mode.
Mode | Real IP | Use cases |
fetch mode | Returns an internal IP address | Scenarios where the edge function needs to modify response content, such as compression or rewriting. |
bypass mode | Returns the real client IP | Lightweight scenarios such as authentication and logging. |
To implement this logic in your edge function code, change the entry point from async fetch(request) to async bypass(request). The ResponseBypass object, a built-in utility in the ESA edge runtime, controls whether a request bypasses function processing:
// Entry function for bypass mode
async bypass(request) {
// Execute pre-processing logic, such as authentication.
const isAuthPassed = true; // Replace with your actual authentication logic.
if (isAuthPassed) {
// Authentication succeeded. Return true to bypass function processing.
return new ResponseBypass(true);
} else {
// Authentication failed. Return false and specify a failure status code.
return new ResponseBypass(false, { status: 403 });
}
}
When bypass mode is enabled, the edge function does not receive the request body. If you need to read the request body, for example from a POST request, use fetch mode instead.
Bypass mode is for lightweight tasks like authentication and logging, not for modifying the response with compression or rewriting, and it does not consume CPU time for response processing.
Verification
After enabling bypass mode, verify that the ali-real-client-ip header returns the real client IP using one of the following methods:
Use the curl command to check the response headers:
curl -I https://your-domain.comCheck the
ali-real-client-ipvalue in the response headers. It must be the client's public IP, not an internal IP.In the browser developer tools, open the Network panel and check the
ali-real-client-ipvalue in the response headers.
Can the variable key be modified?
No, it cannot be modified. The key is not editable once created. If you need to make changes, please delete it and add a new one.
Why do I read undefined?
If the current version is not bound to the variable, or the key is configured only in the other environment, the code returns undefined and does not throw an exception. Check whether the key is configured in the target environment, and whether you redeployed the version after you configured the key.
Are variables bound to versions?
Yes. Function variables are bound together with a version. If you change variables in the console without releasing a new version, the running version does not detect the change.
What if an encrypted variable fails to be decrypted?
If the value of an encrypted key cannot be decrypted, the edge function returns a 599 error. Check whether the variable value was encrypted and written correctly.
How do I switch to bypass mode by modifying my code?
Take Type A URL signing as an example:
Change the entry function from
async fetch()toasync bypass().The function must return a value constructed with
new ResponseBypass(), which takes two parameters:The first parameter is a boolean (true or false) indicating whether authentication succeeded.
If authentication fails (false), you can pass a second
statusparameter to specify the status code to return to the client.
Example code for original Type A signing | Example code for Type A signing with bypass mode |
How do I change a domain name binding?
If your current domains and routes no longer meet your needs, edit or delete them.
Log on to the ESA console, and in the navigation pane on the left, choose Edge Computing and AI > Functions & Pages. Click the target Functions & Pages instance.
Click the Domains tab.
On the Domain Names tab you can delete a bound domain name and then add it again. For a bound route, you can edit it, or delete it and add it again.
How do I bind Functions and Pages directly to a root domain?
To bind Functions and Pages directly to a root domain such as example.com, use one of the following methods:
Custom Domains: Select Add Domain Name and enter
@.example.comas the domain name.Routes: Select Add Route. For the site, use Select Website to select
example.com. For Route Mode, select Simple Mode and leave Route Prefix blank.
Invalid status code
Error message
HttpBody.constructor: invalid status codeCause
The status codes 596, 597, 598, and 599 are reserved for internal use by the edge runtime. Do not return these status codes in your function.
No available proxy address
Error message
unhandled error(13485,13477)@: Error: io error@: no available fetch proxy address now\nstacktrace:\nN/A\nCause 1
The URL passed to cache.put starts with https. The cache API does not support https. Change https to http.
Cause 2
A fetch request targets a domain not managed by ESA, but the cdnProxy:true option is set. To fix this, either add the domain to your ESA service or remove the cdnProxy:true configuration.
Broken connection
Error message
Error: unhandled error(13486,13505)@: Broken connection, unexpected EOFCause
The origin closed the connection while the edge function was receiving data from it.