All Products
Search
Document Center

:Gzip compression is not enabled normally after a request passes through Alibaba Cloud Content Delivery Network back-to-origin

Last Updated:Mar 01, 2021

Description

Alibaba Cloud Content Delivery Network the origin site is an Nginx server, with Gzip compression enabled, the Gzip compression function works when the client directly requests the origin site. When the client requests back-to-origin, the Gzip compression function becomes invalid. The details are as follows:

When Gzip compression /decompression is supported, Nginx will return the compressed content to reduce the traffic overhead and speed up the response. However, after the Alibaba Cloud Content Delivery Network is enabled, the request will be forwarded through the Alibaba Cloud Content Delivery Network, while the client will finally receive the uncompressed content. That is, after the Alibaba Cloud Content Delivery Network back-to-origin process, the Gzip function on the origin site does not take effect. The details are as follows:

  • Alibaba Cloud Content Delivery Network is not enabled
    The Request Header contains Accept-Encoding: gzip, deflate, and the Response Header returns Content-Encoding: gzip normally. That is, the content has been compressed.
  • After Alibaba Cloud Content Delivery Network is enabled
    The request header contains Accept-Encoding: gzip, deflate, but the response header returns Content-Length and does not respond to Content-Encoding: gzip.

Causes

Gzip-Related configurations in the Origin Nginx server are incorrect. The Gzip compression feature is not enabled for back-to-origin requests Alibaba Cloud Content Delivery Network. Details are as follows:

When a client request is forwarded to an origin server through Alibaba Cloud Content Delivery Network, the Via field is added to the request header of the origin server to identify that the request is from a proxy server (Alibaba Cloud Content Delivery Network here). The ngx_http_gzip_module module of Nginx has a gzip_proxied configuration. This configuration is specifically used to control whether Gzip compression is enabled for requests to the proxy server, and one of the prerequisites for this configuration to take effect is that the request header contains Via fields. From this, we can see that gzip_proxied configuration will determine whether Gzip compression is enabled for back-to-origin requests.

Solution

If the problem you encounter is exactly the same as that in the problem description, you can refer to the following steps to update the configuration file of Nginx to fix the problem. If you are not sure about the problem symptom, see more information.

  1. Locate the configuration segment that contains Gzip in Nginx. Because Gzip can be configured in the http, server, and location configuration segments. The configuration files corresponding to different configuration segments may be different, and you are subject to the actual configuration. This topic uses the http configuration segment where Gzip is configured in the nginx.conf file as an example.
  2. Check whether the gzip_proxied configuration exists in the Gzip configuration. If exists, modify the following configurations. If not, add the following configurations. For more information about gzip_proxied configuration, see Nginx official document.
    note: if gzip_proxied configuration does not exist, the default value off is used.
    gzip_proxied  any
    description: any indicates that all requests from the proxy server will have compression enabled.
  3. After saving the preceding configuration, run the following commands in sequence to verify that the Nginx configuration is correct, and then reload the Nginx configuration file.
    nginx -t
    nginx -s reload
  4. After the Alibaba Cloud Content Delivery Network is enabled, make sure that the response header that is returned contains Alibaba Cloud Content Delivery Network, namely, the content is compressed. The client requests are forwarded to the origin Nginx server through Content-Encoding: gzip.

References

To ensure that the problem situation in the field environment is consistent with the problem description in this article, please refer to the following steps for testing:

  1. Log on to any client that supports curl commands.
  2. Run the following command to directly access the origin site through the curl Command. Add a request header containing the Accept-Encoding: gzip, deflate.
    curl -voa 'http://[$Domain]/[$Resource]' -x [$Original_Server_IP]:80 -H 'Accept-Encoding: gzip, deflate'
    Note:
    • [$Domain]: your Domain name.
    • [$Resource]: The request URL of a Resource on a website, such as an image or an api.
    • [$Original_Server_IP]: the public IP address of the Nginx server in the origin.
    The system returns a similar result and confirms that the response header contains Content-Encoding: gzip.
  3. Run the following command and add a Via field based on the command in step 2 to simulate requests coming from a proxy server:
    curl -voa 'http://[$Domain]/[$Resource]' -x [$Original_Server_IP]:80 -H 'Accept-Encoding: gzip, deflate' -H 'Via:xxx'
    note: you can use any value for Via field. This does not affect the test result. This topic uses xxx as an example.
    The system returns the following response and confirms that the Content-Length is returned in the response header and does not Content-Encoding: gzip the response.

Related topic

Applicability

  • CDN