After you configure 302 redirection on your origin server, Alibaba Cloud CDN edge nodes process the HTTP 302 status code that is returned by your origin server instead of returning the status code to clients. This simplifies the request processing pipeline and accelerates content delivery.

Prerequisites

302 redirection is enabled on the origin server.

Background information

302 is an HTTP status code, which indicates that an existing resource is relocated and the request fails to access the resource. To address this issue, the server typically adds the Location header to the response header. After a client receives a response that carries the HTTP 302 status code and the Location header, the request is redirected to the address specified by the Location header to retrieve the resource.

How it works

If an edge node redirects a request to the origin server and receives an HTTP 302 status code from the origin server, the edge node directly processes the status code. The edge node redirects to the address specified by the Location header returned from the origin server to retrieve the resource. In this case, the HTTP 302 status code is not returned to the client. How it works
  1. The user sends a request to http://example.com/examplefile.txt.
  2. The requested file is not cached on edge nodes. The edge node redirects the request to the origin server.
  3. The origin server receives the request and returns the HTTP 302 status code. The response header carries the Location header, which specifies an address: http://www.example.org/examplefile.txt.
  4. The edge node receives the response and redirects to the address http://www.example.org/examplefile.txt.
  5. The edge node retrieves and caches the file.
  6. The edge node delivers the file to the user.
If other users send requests to http://example.com/examplefile.txt, the file is directly returned to the users because the file is already cached on edge nodes.

Usage notes

Before you configure 302 redirection for an accelerated domain name, check whether an origin host is configured for the accelerated domain name (For more informarion, see Configure an origin host):
  • If not an origin host is configured: When the origin server returns the HTTP 302 status code and the Location header that specifies a URL to the edge node, the request is redirected to the URL specified by the Location header. The value of the Host header in the request is the domain name specified by the Location header.
  • If so an origin host is configured: When the origin server returns the HTTP 302 status code and the Location header that specifies a URL to the edge node, the Host header in the back-to-origin request is the Host header configured for the accelerated domain name. By default, the Host header value is the accelerated domain name. If you require that the Host header value in the back-to-origin request must be the domain name specified by the Location header, an error occurs. If you want Alibaba Cloud CDN to support this requirement, submit a ticket.

Procedure

  1. Log on to the Alibaba Cloud CDN console.
  2. In the left-side navigation pane, click Domain Names.
  3. On the Domain Names page, find the domain name that you want to manage and click Manage in the Actions column.
  4. In the left-side management pane of the domain name, click Back-to-origin.
  5. On the Configurations tab, find the 302 Redirection section.
  6. Turn on 302 Redirection.
  7. Set the following parameters.
    Configure 302 redirection
    Parameter Description
    Maximum 302 Redirects Specify the maximum number of times that an edge node can redirect to the address specified by the Location header for each request. If the maximum value is exceeded, the HTTP 302 status code is returned to the user. Valid values: 1 to 5. Default value: 2.
    Note The maximum number of 302 redirects determines the number of back-to-origin redirects that can be redirected to the origin server by an edge node. which refers to the maximum number of times that a request can be redirected to the origin server by an edge node.

    Maximum number of back-to-origin redirects = Maximum number of 302 redirects +1. In this case, the default maximum number of back-to-origin redirects is 3, and valid values are 2 to 6.

    Retain Parameters in 302 Redirects
    • Yes: Request parameters are retained during 302 redirects. The request parameters are passed to the server specified by the Location header.
    • No: Request parameters are not retained during 302 redirects.
    Retain Request Headers in 302 Redirects
    • Yes: Request headers are retained during 302 redirects. The request parameters are passed to the server specified by the Location header.
    • No: Request headers are not retained during 302 redirects.
  8. Click OK.

Configuration examples

Sample scenario: The origin server relocated to another domain name. You want users to visit the origin server through the original domain name so that you do not need to make user-facing changes.

Configuration: Enable 302 redirection for the domain name example.com. The following figure shows the configuration.Configurations
Expected result: Users request the file http://example.com/examplefile.txt, which is not cached on Alibaba Cloud CDN edge nodes. The edge nodes redirect the requests to the origin server with the original request parameters and headers retained. The origin server receives the requests and returns the HTTP 302 status code and the URL of the file http://www.example.org/examplefile.txt to the edge nodes.
  1. The edge nodes receive the HTTP 302 status code and redirect to the URL provided by the origin server. Maximum 302 Redirects is set to 2, which indicates that the requests can be redirected two times. If the requests fail, the HTTP 302 status code is returned to the users.
  2. The edge nodes retrieve the requested file, return it to the users, and cache it.
  3. Other users can retrieve the file http://example.com/examplefile.txt directly from the edge nodes.