Use nginx Reverse proxy and gzip_static module

Source: Internet
Author: User
Tags gz file nginx reverse proxy

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.