Home Backend Development PHP Tutorial nginx cache configuration

nginx cache configuration

Jul 30, 2016 pm 01:31 PM
cache http nbsp nginx proxy

nginx cache configuration

Nginx supports Squid-like caching function starting from version 0.7.48. This cache treats the URL and related combinations as Key, uses md5 encoding and hashing and stores them on the hard disk, so it can support any URL link and also supports non-200 status codes such as 404/301/302. Although the current official Nginx web cache service can only set the expiration time for the specified URL or status code, and does not support the Squid-like PURGE instruction to manually clear the specified cache page, however, through a third-party Nginx module, the cache of the specified URL can be cleared. . IT Network, http://www.it.net.cn

Nginx’s Web caching service is mainly composed of proxy_cache related instruction set and fastcgi_cache related instruction set. The former is used for reverse proxy, which affects the back-end content source server. Cache, the latter is mainly used to cache FastCGI dynamic programs. The functionality of both is basically the same.

In the latest Nginx 0.8.32 version, proxy_cache and fastcgi_cache are relatively complete. Together with the third-party ngx_cache_purge module (used to clear the cache of the specified URL), it can completely replace Squid. We have been using Nginx's proxy_cache caching function in the production environment for more than two months. It is very stable and the speed is not inferior to Squid.

In terms of functionality, Nginx already has the Web cache acceleration function and the function of clearing the specified URL cache that Squid has. In terms of performance, Nginx's utilization of multi-core CPUs is much better than Squid. In addition, Nginx is much more powerful than Squid in terms of reverse proxy, load balancing, health check, back-end server failover, rewrite, and ease of use. This allows one Nginx to be used as a "load balancing server" and a "Web cache server" at the same time.

1. Compile and install Nginx load balancing and caching server under Linux:
Linux learning, http://linux.it.net.c

ulimit -SHn 65535
wget ftp://ftp.csx .cam.ac.uk/pub/software/programming/pcre/pcre-8.00.tar.gz
tar zxvf pcre-8.00.tar.gz


cd pcre-8.00/
./configure
make && make install
cd ../

wget http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz
tar zxvf ngx_cache_purge-1.0.tar.gz

wget http://nginx.org/download/nginx-0.8.32. tar.gz
tar zxvf nginx-0.8.32.tar.gz
cd nginx-0.8.32/
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.0 - -prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../ IT Network, http://www.it.net.cn

2, /usr/local/nginx/conf/nginx.conf The configuration file content is as follows:
IT Network, http://www.it.net.cn

user www www;

worker_processes 8;

error_log / usr/local/nginx/logs/nginx_error.log crit; IT Network, http://www.it.net.cn

pid /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535; /octet-stream ;

Linux learning, http:// linux.it.net.cn



#charset utf-8;

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;

large_client_header_buffers 4 32k;
client_max_body_size 300m;

sendfile on; tcp_nopush on;

keepalive_timeout 60;

IT Network, http://www.it.net.cn


tcp_nodelay on;

client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60 ;
proxy_send_timeout 5;

proxy_buffer_size 16k;

proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k;

IT Network, http://www.it.net.cn

gzip on;
gzip_min_length 1k;
g zip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

#Note: The paths specified by proxy_temp_path and proxy_cache_path must be in the same partition
proxy_temp_path /data0/proxy_temp_dir;
#Set the name of the Web cache area to cache_one, the memory cache space size to 200MB, content that has not been accessed in 1 day will be automatically cleared, and the hard disk cache The space size is 30GB.
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_z inactive=1d max_size=30g;

upstream backend_server {
server 192.168.8.43:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.8.44:80 weight= 1 max_fails=2 fail_timeout=30s;
server 192.168.8.45:80 weight=1 max_fails=2 fail_timeout=30s;
}

server
{
listen 80;
server_name www.it.net.cn 192.168.8.42;
index index.html index.htm;
root /data0/htdocs/www;

location /
{
Another server for failover.
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
          proxy_cache cache_one; 
      #Set different cache times for different HTTP status codes
                      proxy_cache_valid     200 304 12h; 
    ​​ #Combine the domain name, URI, and parameters to form the Key value of the Web cache. Nginx uses the Key Value hash, store cache content in the second-level cache directory
      proxy_cache_key $host$uri$is_args$args;
    proxy_set_header Host   $host;


        proxy_set_header X-Forwarded-For   $remote_addr;
    proxy_pass http://back end_server;
expires   1d;
  }
 ​​​​​​​​ # Used to clear the cache. Suppose a URL is http://192.168.8.42/test.txt. You can clear the URL by accessing http://192.168.8.42/purge/test.txt cache.
Location ~ /purge(/.*)
{
#Set only the specified IP or IP segment to clear the URL cache.
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all; proxy_cache_purge cache_one $host$1$is_args $args;
}
  # Dynamic applications with extensions ending in .php, .jsp, .cgi are not cached.
location ~ .*.(php|jsp|cgi)?$

{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}
access_log off;
}
}
Linux learning, http://linux.it.net.cn

The above introduces the nginx cache configuration, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

How to configure nginx in Windows How to configure nginx in Windows Apr 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 check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check whether nginx is started How to check whether nginx is started Apr 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 create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

See all articles