Problem description
The origin server for DCDN is an Nginx server with Gzip compression enabled. Gzip compression works as expected when a client directly requests the origin server, but fails for back-to-origin requests.
When gzip compression is enabled, Nginx returns compressed content to reduce traffic overhead and accelerate response speed. However, after you enable DCDN, requests are forwarded through DCDN, but the client ultimately receives uncompressed content. When DCDN sends a back-to-origin request, the gzip feature on the origin server does not take effect.
-
Without or DCDN A request header with
Accept-Encoding: gzip, deflateresults in a response header withContent-Encoding: gzip, which indicates that the content is compressed. -
With or DCDN A request header with
Accept-Encoding: gzip, deflateresults in a response header that includesContent-Lengthbut notContent-Encoding: gzip.
Cause
The Gzip configuration on the origin Nginx server is incorrect, and Gzip compression is not enabled for back-to-origin requests from or DCDN.
When a client request is forwarded to the origin server through DCDN, the Via field is added to the back-to-origin request header to indicate that the request comes from a proxy server (in this case, DCDN). The ngx_http_gzip_module module in Nginx provides a gzip_proxied directive that controls Gzip compression for proxied requests. One prerequisite for this directive is that the request header contains the Via field. Therefore, the gzip_proxied directive determines whether Gzip compression is enabled for back-to-origin requests.
Solution
If your problem matches the one described in Problem description, follow these steps to update the Nginx configuration. If you are unsure about the symptoms, see More information to verify first.
-
Locate the gzip configuration block in Nginx. These directives can be placed in the
http,server, orlocationblocks, so the relevant configuration file may vary. In this topic's example, the gzip configuration is in thehttpblock of thenginx.conffile. -
In your gzip configuration, check for the
gzip_proxieddirective. Modify it to match the following configuration, or add the directive if it does not exist. For more information about thegzip_proxieddirective, see the official Nginx documentation.NoteIf the
gzip_proxieddirective is not specified, it defaults tooff.gzip_proxied anyNoteThe
anyparameter enables compression for all requests from a proxy server. -
After saving the configuration, run the following commands to check and reload the Nginx configuration.
nginx -t nginx -s reload -
With or DCDN enabled, send a client request that is forwarded to the Nginx . Confirm that the response header now contains
Content-Encoding: gzip, indicating the content is compressed.
More information
To confirm that your issue matches the one described in the Problem description section, run the following test:
-
Use any client that supports the
curlcommand. -
Run the following
curlcommand to directly access the origin server. Include theAccept-Encoding: gzip, deflateheader in the request.curl -voa 'http://[$Domain]/[$Resource]' -x [$Original_Server_IP]:80 -H 'Accept-Encoding: gzip, deflate'Note-
[$Domain]: Your domain name.
-
[$Resource]: The request URL for a resource on a website, such as an image or an API interface.
-
[$Original_Server_IP]: The public IP address of the origin Nginx server.
The response is similar to the following. Verify that the response header contains
Content-Encoding: gzip.
-
-
Refer to the following command, which adds a
Viafield to the command from Step 2 to simulate a request from a proxy server.curl -voa 'http://[$Domain]/[$Resource]' -x [$Original_Server_IP]:80 -H 'Accept-Encoding: gzip, deflate' -H 'Via:xxx'NoteYou can use any value for the
Viaheader, since it does not affect the test result. This example usessome-proxy.The response is similar to the following. Verify that the response header contains
Content-Lengthand does not containContent-Encoding: gzip.