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.
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
/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;
Where, /path/to/cache
is the cache path, It can be modified according to the actual situation. levels=1:2
Indicates 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=10g
Indicates 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;
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.
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; }
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.
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
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!