search
HomeOperation and MaintenanceNginxHow to use Nginx for compression and decompression of HTTP requests

How to use Nginx to compress and decompress HTTP requests

Nginx is a high-performance web server and reverse proxy server that is powerful and flexible. When processing HTTP requests, you can use the gzip and gunzip modules provided by Nginx to compress and decompress the requests to reduce the amount of data transmission and improve the request response speed. This article will introduce the specific steps of how to use Nginx to compress and decompress HTTP requests, and provide corresponding code examples.

  1. Configuring the gzip module

First you need to enable the gzip module in the Nginx configuration file. Open your Nginx configuration file (usually located at /etc/nginx/nginx.conf or /usr/local/nginx/conf/nginx.conf), find the http block, and add the following Configuration:

http {
  gzip on;
  gzip_types text/plain text/css application/javascript;
  gzip_min_length 1024;
  gzip_comp_level 1;
}

The meaning of the above configuration is as follows:

  • gzip on; means enabling the gzip module.
  • gzip_types Indicates the file types that need to be compressed. Here we have selected text files, CSS files and JavaScript files. You can configure it according to your needs.
  • gzip_min_length Indicates that only files whose length exceeds this value will be compressed.
  • gzip_comp_level Indicates the compression level. The higher the level, the better the compression effect, but the more CPU resources it takes up.
  1. Configuring the gunzip module

If you need to decompress the received request, you also need to enable the gunzip module. Find the http block in the same configuration file and add the following configuration:

http {
  gunzip on;
}
  1. Apply the configuration and restart Nginx

After saving the Nginx configuration file, execute the following command to apply the configuration and Restart the Nginx service:

sudo service nginx reload

Please make sure you have sufficient permissions.

  1. Verify compression and decompression

Now you have configured Nginx’s compression and decompression modules. You can verify that these features are working by sending an HTTP request to Nginx.

First, use the curl tool to send a GET request to the configured Nginx service:

curl -H "Accept-Encoding: gzip, deflate" -I http://localhost

The -H "Accept-Encoding: gzip, deflate" in the command means tell The server can accept gzip or deflate compression methods. -I indicates that only the response header information is obtained, without obtaining the complete response body.

If everything is normal, you will see information similar to the following in the response header:

Content-Encoding: gzip

This means that the server has compressed the response body into gzip format for transmission.

Next, you can use the following command to send a decompressed request:

curl --compressed -I http://localhost

--compressed in the command means telling the server that the response received has been compressed , need to be decompressed.

If everything goes well, you will see information similar to the following in the response header:

Content-Encoding: 

This means that the server has decompressed the response body and transmitted it in its original format.

Through the above steps and verification, you have successfully configured Nginx's gzip and gunzip modules to implement compression and decompression of HTTP requests.

Summary

This article introduces how to use Nginx to compress and decompress HTTP requests, and provides related configuration and command examples. Using Nginx's gzip and gunzip modules can help you reduce the amount of data transmission and improve the response speed of requests. Through proper configuration and use, the performance and user experience of web applications can be improved. If you encounter problems during use, you can refer to Nginx's official documentation or ask for help. I hope the content of this article is helpful to you!

Reference link:

  • Nginx official documentation: http://nginx.org/en/docs/
  • Gzip module configuration guide: https://nginx .org/en/docs/http/ngx_http_gzip_module.html
  • Gunzip module configuration guide: https://nginx.org/en/docs/http/ngx_http_gunzip_module.html

The above is the detailed content of How to use Nginx for compression and decompression of HTTP requests. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

How to start nginxHow to start nginxApr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to check whether nginx is startedHow to check whether nginx is startedApr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to close nginxHow to close nginxApr 14, 2025 pm 01:00 PM

To shut down the Nginx service, follow these steps: Determine the installation type: Red Hat/CentOS (systemctl status nginx) or Debian/Ubuntu (service nginx status) Stop the service: Red Hat/CentOS (systemctl stop nginx) or Debian/Ubuntu (service nginx stop) Disable automatic startup (optional): Red Hat/CentOS (systemctl disabled nginx) or Debian/Ubuntu (syst

How to configure nginx in WindowsHow to configure nginx in WindowsApr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to solve nginx403 errorHow to solve nginx403 errorApr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to start nginx in LinuxHow to start nginx in LinuxApr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to check whether nginx is started?How to check whether nginx is started?Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)