Home Operation and Maintenance Nginx How to use Nginx for reverse proxy caching of HTTP requests

How to use Nginx for reverse proxy caching of HTTP requests

Aug 02, 2023 pm 10:58 PM
nginx reverse proxy cache

How to use Nginx for reverse proxy caching of HTTP requests

Nginx is a high-performance open source web server software. In addition to serving as a web server to handle static resource requests, Nginx can also perform HTTP requests. reverse proxy. Reverse proxy can help us forward client requests to multiple back-end servers for processing, and can improve response speed through caching. This article will introduce how to configure reverse proxy cache in Nginx.

  1. Install Nginx
    First, we need to install the Nginx server. In a Linux environment, it can be installed through the package manager. Taking Debian/Ubuntu as an example, execute the following command:

    sudo apt update
    sudo apt install nginx
    Copy after login
  2. Configure reverse proxy cache
    After the installation is completed, we need to edit the Nginx configuration file to configure the reverse proxy cache. The default configuration file is located at /etc/nginx/nginx.conf. Open the file with a text editor and find the http{} block.

Add the following configuration in the http{} block:

proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;
Copy after login

Where, /path/to/cache is the cache path, It can be modified according to the actual situation. levels=1:2Indicates the level of the cache directory. keys_zone=my_cache:10m means allocating 10M of memory for the cache to store cache metadata. max_size=10gIndicates that the maximum size of the cache is 10G. inactive=60m means that the cache file will be considered expired if it is not accessed for 60 minutes.

Then, add the following configuration within the server{} block:

proxy_cache my_cache;
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 404 10m;
proxy_cache_use_stale error timeout updating http_500 http_503;
Copy after login

Among them, proxy_cache my_cache means enabling the name my_cache cache. proxy_cache_valid is used to configure the cache validity period of different status codes. For example, the request cache validity period of 200 and 302 status codes is 1 hour. proxy_cache_use_stale Used to specify whether to use an expired cache as a response when an error or timeout occurs on the backend server.

  1. Configuring the reverse proxy
    After completing the cache configuration, we need to configure the reverse proxy. Within the server{} block, add the following configuration:

    location / {
     proxy_pass http://backend_server;
     proxy_set_header Host $host;
     proxy_cache_bypass $http_cache_control;
     proxy_cache_key $uri$is_args$args;
    }
    Copy after login

    where proxy_pass specifies the address of the backend server. proxy_set_header is used to set the header information in the proxy request. Here, the host header of the request is set to the current host. proxy_cache_bypass is used to specify which requests do not use the cache. This is determined based on the cache-control in the request header. proxy_cache_key Used to generate the cache key, here is a combination of the requested URI and parameters.

  2. Restart Nginx and test
    After completing the above configuration, save and exit the configuration file. Then, restart the Nginx service through the following command:

    sudo systemctl restart nginx
    Copy after login

    Next, send a request through the browser or command line tool to observe the working of the Nginx cache.

Summary
Through the above steps, we successfully configured Nginx’s reverse proxy cache function. By using caching, the response speed of requests can be significantly improved and the load on the back-end server can be reduced. At the same time, Nginx also provides flexible configuration options that can be adjusted and optimized according to actual needs.

I hope this article will help you understand and use Nginx's reverse proxy cache function. If you have any questions or doubts, please leave a message in the comment area and I will try my best to answer them.

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

Statement of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Nginx Performance Tuning: Optimizing for Speed and Low Latency Nginx Performance Tuning: Optimizing for Speed and Low Latency Apr 05, 2025 am 12:08 AM

Nginx performance tuning can be achieved by adjusting the number of worker processes, connection pool size, enabling Gzip compression and HTTP/2 protocols, and using cache and load balancing. 1. Adjust the number of worker processes and connection pool size: worker_processesauto; events{worker_connections1024;}. 2. Enable Gzip compression and HTTP/2 protocol: http{gzipon;server{listen443sslhttp2;}}. 3. Use cache optimization: http{proxy_cache_path/path/to/cachelevels=1:2k

How do I configure Nginx for server-side includes (SSI)? How do I configure Nginx for server-side includes (SSI)? Mar 17, 2025 pm 05:06 PM

The article discusses configuring Nginx for server-side includes (SSI), performance implications, using SSI for dynamic content, and troubleshooting common SSI issues in Nginx.Word count: 159

How do I implement HTTP authentication (basic auth, digest auth) in Nginx? How do I implement HTTP authentication (basic auth, digest auth) in Nginx? Mar 17, 2025 pm 05:03 PM

The article discusses implementing HTTP authentication in Nginx using basic and digest methods, detailing setup steps and security implications. It also covers using authentication realms for user management and suggests combining authentication meth

How do I monitor Nginx performance and resource usage? How do I monitor Nginx performance and resource usage? Mar 17, 2025 pm 05:08 PM

The article discusses monitoring and optimizing Nginx performance, focusing on using tools like Nginx's status page, system-level monitoring, and third-party solutions like Prometheus and Grafana. It emphasizes best practices for performance optimiza

How do I configure Nginx for URL rewriting and redirection? How do I configure Nginx for URL rewriting and redirection? Mar 17, 2025 pm 05:02 PM

The article discusses configuring Nginx for URL rewriting and redirection, detailing steps and best practices. It addresses common mistakes and testing methods to ensure effective URL management.

What are the best tools for monitoring Nginx? What are the best tools for monitoring Nginx? Mar 17, 2025 pm 05:09 PM

The article discusses top Nginx monitoring tools like Datadog, New Relic, and NGINX Amplify, focusing on their features for real-time monitoring, alerting, and detailed metrics to enhance server performance.

How do I configure Gzip compression in Nginx? How do I configure Gzip compression in Nginx? Mar 17, 2025 pm 04:57 PM

The article details how to configure Gzip compression in Nginx, its performance benefits, and verification methods. Main issue: optimizing web server performance through compression.[159 characters]

How do I configure Nginx for WebSocket proxying? How do I configure Nginx for WebSocket proxying? Mar 17, 2025 pm 05:01 PM

Article discusses configuring Nginx for WebSocket proxying, detailing necessary settings and troubleshooting steps for successful WebSocket connections.(159 characters)

See all articles