The 301/302 redirection feature supports both 301 and 302 response status codes from the origin server and handles them in the same way. When this feature is enabled, CDN points of presence (POPs) directly handle the 301/302 responses from the origin server, which reduces data interaction and accelerates resource retrieval for users.
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 301/302 redirection, make sure that CDN is configured with a default origin host or a specific origin host:
-
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 a default origin host is configured: When the origin server responds to a CDN point of presence (POP) with a 301/302 status code and a Location URL, the back-to-origin request uses the Host header that is configured in CDN. If your origin server requires that the domain name in the Location URL is used as the back-to-origin Host, use the specific origin host feature.
The Location header in the response from the origin server to a CDN point of presence (POP) supports the following three formats:
-
Location: http://www.example.net/index.html: The CDN point of presence (POP) will use the complete URL from the Location header. -
Location: //www.example.net/index.htmL: The CDN POP will combine the protocol of the back-to-origin request with the information in theLocationheader to form a URL. -
Location: /index.html: The CDN POP forms a URL by concatenating the protocol and domain name of the original back-to-origin request with the value in the Location header.
Procedure
-
Log on to the CDN console.
-
In the left navigation pane, click Domain Names.
-
On the Domain Management page, find the domain name that you want to manage and click Actions in the Manage column.
-
Click Origin Fetch. On the Configurations tab, find the 301/302 Redirection section and turn on the switch.
-
In the dialog box that appears, configure the parameters and click OK.
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.
-
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 301/302 redirection for the domain name example.com. In the 301/302 Redirection dialog box, set Maximum 301/302 Redirects to 2. The maximum number of back-to-origin requests is 3. The valid range for redirects is 1 to 5, and the valid range for back-to-origin requests is 2 to 6. Select Do Not Retain for Retain Parameters in 301/302 Redirects, select Do Not Retain for Retain Request Headers in 301/302 Redirects, and then click OK.
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.