The origin 301/302 redirection feature allows CDN points of presence (POPs) to process 301 and 302 responses from the origin server. When this feature is enabled, POPs handle the redirection directly instead of returning the 301/302 responses to clients. This simplifies the data exchange process and accelerates resource delivery.
Prerequisites
301/302 redirection is enabled on your origin server.
Background information
301 and 302 are HTTP status codes that indicate a resource has been moved. In this situation, a server typically includes a Location header in the response. When a client receives a 301 or 302 response with a Location header, the client sends a new request to the address specified in the Location header to retrieve the resource.
How it works
With 301/302 redirection, if a CDN node receives a 301 or 302 status code from the origin server during an origin fetch, the node follows the redirection to retrieve the resource from the new location. The 301 or 302 status code is not returned to the client.
A client requests the file
http://example.com/examplefile.txt.The file is not cached on the CDN node. The node fetches the file from the origin server.
The origin server returns a 301/302 status code. The Location address is
http://www.example.org/examplefile.txt.The CDN node receives the response and sends a request to the Location address
http://www.example.org/examplefile.txtto retrieve the resource.The CDN node retrieves the resource and caches it on the CDN node.
The CDN node returns the resource to the client.
If another client then requests the file http://example.com/examplefile.txt, the file is served directly from the cache on the CDN node.
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 no default origin host or specific origin host is configured: When the origin server returns a 301 or 302 response to a CDN node, the HOST header of the subsequent origin fetch request uses the domain name from the Location header.
If an origin host is configured for the accelerated domain name: When the origin server returns a 301/302 status code and a Location URL to the CDN POP, the Host header in the origin-fetch request to the Location URL is the origin host configured for the accelerated domain name in ApsaraVideo VOD. The default origin host is the accelerated domain name. If your origin server requires that the Host header for the Location URL must be the domain name from the Location URL, access may fail. If you have this requirement, submit a ticket to request this 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
The maximum number of times that a CDN node can follow a redirect to a Location address during a single user request. If the limit is exceeded, the node returns the 301/302 status code directly to the user. Valid values: 1 to 5. Default value: 2.
NoteThe maximum number of 301/302 redirects affects the maximum number of origin fetches. The maximum number of origin fetches is the maximum number of times a CDN node can access the origin server during a single user request.
Maximum number of origin fetches = Maximum number of 301/302 redirects + 1. The default maximum number of origin fetches is 3. Valid values: 2 to 6.
Retain Parameters in 301/302 Redirects
Retain: Keeps the original request parameters when following a 301/302 redirect. The parameters are passed to the server at the Location address.
Do not retain: Removes the original request parameters when following a 301/302 redirect.
Retain Request Headers in 301/302 Redirects
Retain: Keeps the original request headers when following a 301/302 redirect. The headers are passed to the server at the Location address.
Do not retain: Removes the original request headers when following a 301/302 redirect.
Click OK to complete the configuration.
Configuration example
Scenario: The resources on your origin server are moved to another address. You want users to access the resources using the original domain name without being aware of the change.
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
Result: A client requests the file http://example.com/examplefile.txt. 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 address http://www.example.org/examplefile.txt.
After the CDN node receives the 301/302 status code, it sends a request to the redirect address. The request can be redirected up to two times. If the request fails, the 301/302 status code is returned to the client.
The CDN node retrieves the resource, returns it to the client, and caches it on the CDN node.
When other clients request the file, CDN directly returns the cached resource.