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 containsAccept-Encoding: gzip, deflate, and the Response Header returnsContent-Encoding: gzipnormally. That is, the content has been compressed. - After Alibaba Cloud Content Delivery Network is enabled
The request header containsAccept-Encoding: gzip, deflate, but the response header returnsContent-Lengthand does not respond toContent-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.
- 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.conffile as an example. - Check whether the
gzip_proxiedconfiguration exists in the Gzip configuration. If exists, modify the following configurations. If not, add the following configurations. For more information aboutgzip_proxiedconfiguration, see Nginx official document.note: if
gzip_proxiedconfiguration does not exist, the default valueoffis used.gzip_proxied any
description:
anyindicates that all requests from the proxy server will have compression enabled. - 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 - 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:
- Log on to any client that supports
curlcommands. - 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:
The system returns a similar result and confirms that the response header contains
- [$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.
Content-Encoding: gzip.
- Run the following command and add a
Viafield 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
The system returns the following response and confirms that theViafield. This does not affect the test result. This topic usesxxxas an example.Content-Lengthis returned in the response header and does notContent-Encoding: gzipthe response.
Related topic
Applicability
- CDN