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

王林
Release: 2023-08-02 22:58:45
Original
1065 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template