When origin 301/302 redirection is enabled, CDN points of presence (POPs) handle 301 and 302 redirects from the origin server directly instead of returning them to clients. This reduces data exchange overhead and accelerates resource delivery.
Prerequisites
Your origin server uses 301/302 redirects to implement its business logic.
Background
A 301 or 302 is an HTTP status code that indicates that a resource has moved and is no longer accessible at the requested location. To handle this, a server typically includes a Location header in its response. When a client receives a 301/302 response with a Location header, it sends a new request to the address specified in the header to retrieve the resource.
How it works
301/302 redirection means that when a CDN point of presence (POP) receives a 301/302 status code from the origin server in response to a back-to-origin request, the CDN POP directly follows the Location address to retrieve the resource instead of returning the 301/302 status code to the user.
-
A client requests the file
http://example.com/examplefile.txt. -
The file is not cached on the CDN point of presence (POP), and a back-to-origin request is sent.
-
The origin server returns a 301/302 status code, with the Location header pointing to
http://www.example.org/examplefile.txt. -
After the CDN POP receives a response from the origin server, it sends a request to the Location address
http://www.example.org/examplefile.txtto retrieve the resource. -
After a CDN point of presence (POP) obtains the required resource, the resource is cached on the CDN POP.
-
The CDN POP returns the retrieved resources to the user.
At this point, if another user requests the http://example.com/examplefile.txt file, the request directly hits the cache on the CDN point of presence (POP), and the file is returned to the user.
Usage notes
Before you configure origin 301/302 redirection, check whether an origin host is configured for the accelerated domain name in ApsaraVideo VOD:
-
If a default origin host or a specific origin host is not configured: When the origin server responds to a CDN POP with a 301/302 status code and a Location URL, the Host header of the back-to-origin request will use the domain name from the Location URL.
-
If an origin host is configured for the accelerated domain name: When the origin server returns a 301/302 status code with a Location URL, the CDN POP uses the configured origin host as the Host header in follow-up requests to the Location URL. By default, the origin host is the accelerated domain name. If your origin server requires the Host header to match the domain name in the Location URL, access may fail. To address this, submit a ticket to request backend configuration. For more information, see Contact us.
Procedure
-
Log on to the ApsaraVideo VOD console.
In the left-side navigation pane, choose Configuration Management > CDN Configuration > Domain Names.
-
Find the domain name that you want to manage and click Configure in the Actions column.
-
In the navigation pane on the left for the domain name, click Back-to-Origin.
-
On the Configuration tab, in the Origin 301/302 Redirection section, turn on the Origin 301/302 Redirection switch and configure the parameters.
Parameter
Description
Maximum 301/302 Redirects
This is the maximum number of times that a CDN point of presence (POP) can follow a redirect during a single user request. If this limit is exceeded, the POP directly returns a 301 or 302 status code to the user. The default value is 2, and the valid range is 1 to 5.
NoteConfiguring the Maximum 301/302 Redirects affects the maximum number of back-to-origin requests. The maximum number of back-to-origin requests is the maximum number of times that a CDN node can access the origin server during a single user request.
The formula is: Maximum number of back-to-origin requests = Maximum 301/302 Redirects + 1. The default maximum is 3, and the valid range is 2 to 6.
Retain Parameters in 301/302 Redirects
-
Retain: The original request parameters are retained in the back-to-origin request to the new location.
-
Do Not Retain: The original request parameters are not retained in the back-to-origin request to the new location.
Retain Request Headers in 301/302 Redirects
-
Retain: The original request headers are retained in the back-to-origin request to the new location.
-
Do Not Retain: The original request headers are not retained in the back-to-origin request to the new location.
-
-
Click OK to complete the configuration.
Configuration example
Use case: The resources on your origin server have moved to a new location, but you want clients to access them through the original domain name transparently.
Configuration: Enable origin 301/302 redirection for the domain name example.com with the following settings:
-
Maximum Number of 301/302 Redirects: 2
-
Retain Parameters in 301/302 Redirects: Do not retain
-
Retain Request Headers in 301/302 Redirects: Do not retain
Explanation of results: A user requests the http://example.com/examplefile.txt file. If the file is not cached on the CDN node, the node requests the resource from the origin server. The origin server returns a 301/302 status code and the redirect URL http://www.example.org/examplefile.txt.
-
When a CDN point of presence (POP) receives a 301 or 302 status code, it sends a request to the redirect address. The POP attempts to follow the redirect up to two times. If the attempts fail, the POP returns the 301 or 302 status code to the user.
-
The CDN POP retrieves the resource, returns it to the user, and caches it on the CDN POP.
-
When other users request the file again, CDN directly returns the cached resource.