In this example, the backend server IP address is 192.168.1.10 and the front-end proxy server IP address is 192.168.1.5.
(If you do not have multiple servers, you can use an intranet IP address or nginx to listen to different ports to achieve the same effect)
1. First, we need to confirm that the gzip_static module of nginx has been compiled:
Nginx-V
If the command output has a -- with-http_gzip_static_module, nginx supports the gzip_static module. If not, add the -- with-http_gzip_static_module parameter to recompile nginx.
2. Configure nginx
The code is as follows: |
Copy code |
Location ~ . Html $ { Gzip_static on; }
|
3. Configure the nginx of the proxy server
The code is as follows: |
Copy code |
Server { Listen 80; Server_name YOUR_DOMAIN.com; Location /{ Proxy_set_header Host $ host; Proxy_set_header Accept-Encoding 'gzip '; Proxy_pass http: // 192.168.1.10; } }
|
4. After the configuration is complete, restart the proxy server at the front end and nginx at the backend for the configuration to take effect:
The code is as follows: |
Copy code |
Nginx-s reload
|
5. Test effectiveness:
The backend server uploads a test.html file in the website directory and then runs the following command:
The code is as follows: |
Copy code |
Gzip test.html
|
The test.html.gz file will be generated after completion, and the test.html file will be deleted at the same time.
Then open the browser to access the front-end nginx:
The code is as follows: |
Copy code |
Http: // 192.168.1.5/test.html |
If the page is displayed normally, the configuration is successful. We also need to verify whether the page can be displayed normally if the browser does not support gzip. Next we will use the telnet tool for testing:
The code is as follows: |
Copy code |
Telnet 192.168.1.5 80 Get.test.html HTTP/1.0 |
Note: The last two carriage returns of the GET row are consecutive.
If the plain text HTML code is output normally, the configuration is correct.
6. After the configuration is successful, you can adjust your application. When generating html static files, call the gzip command to generate a gz compressed file, which greatly reduces the space occupied by static files on the hard disk, it also reduces the CPU resources consumed by gzip compression each time.