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

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

How to implement HTTP streaming using C++? How to implement HTTP streaming using C++? May 31, 2024 am 11:06 AM

How to implement HTTP streaming in C++? Create an SSL stream socket using Boost.Asio and the asiohttps client library. Connect to the server and send an HTTP request. Receive HTTP response headers and print them. Receives the HTTP response body and prints it.

How to deploy and maintain a website using PHP How to deploy and maintain a website using PHP May 03, 2024 am 08:54 AM

To successfully deploy and maintain a PHP website, you need to perform the following steps: Select a web server (such as Apache or Nginx) Install PHP Create a database and connect PHP Upload code to the server Set up domain name and DNS Monitoring website maintenance steps include updating PHP and web servers, and backing up the website , monitor error logs and update content.

How to use Fail2Ban to protect your server from brute force attacks How to use Fail2Ban to protect your server from brute force attacks Apr 27, 2024 am 08:34 AM

An important task for Linux administrators is to protect the server from illegal attacks or access. By default, Linux systems come with well-configured firewalls, such as iptables, Uncomplicated Firewall (UFW), ConfigServerSecurityFirewall (CSF), etc., which can prevent a variety of attacks. Any machine connected to the Internet is a potential target for malicious attacks. There is a tool called Fail2Ban that can be used to mitigate illegal access on the server. What is Fail2Ban? Fail2Ban[1] is an intrusion prevention software that protects servers from brute force attacks. It is written in Python programming language

Come with me to learn Linux and install Nginx Come with me to learn Linux and install Nginx Apr 28, 2024 pm 03:10 PM

Today, I will lead you to install Nginx in a Linux environment. The Linux system used here is CentOS7.2. Prepare the installation tools 1. Download Nginx from the Nginx official website. The version used here is: 1.13.6.2. Upload the downloaded Nginx to Linux. Here, the /opt/nginx directory is used as an example. Run "tar-zxvfnginx-1.13.6.tar.gz" to decompress. 3. Switch to the /opt/nginx/nginx-1.13.6 directory and run ./configure for initial configuration. If the following prompt appears, it means that PCRE is not installed on the machine, and Nginx needs to

How to implement PHP security best practices How to implement PHP security best practices May 05, 2024 am 10:51 AM

How to Implement PHP Security Best Practices PHP is one of the most popular backend web programming languages ​​used for creating dynamic and interactive websites. However, PHP code can be vulnerable to various security vulnerabilities. Implementing security best practices is critical to protecting your web applications from these threats. Input validation Input validation is a critical first step in validating user input and preventing malicious input such as SQL injection. PHP provides a variety of input validation functions, such as filter_var() and preg_match(). Example: $username=filter_var($_POST['username'],FILTER_SANIT

WordPress site file access is restricted: Why is my .txt file not accessible through domain name? WordPress site file access is restricted: Why is my .txt file not accessible through domain name? Apr 01, 2025 pm 03:00 PM

Wordpress site file access is restricted: troubleshooting the reason why .txt file cannot be accessed recently. Some users encountered a problem when configuring the mini program business domain name: �...

How to implement HTTP file upload security using Golang? How to implement HTTP file upload security using Golang? Jun 01, 2024 pm 02:45 PM

Implementing HTTP file upload security in Golang requires following these steps: Verify file type. Limit file size. Detect viruses and malware. Store files securely.

Record once and use strace to diagnose the problem of PHP occupying too much system resources. Record once and use strace to diagnose the problem of PHP occupying too much system resources. May 03, 2024 pm 04:31 PM

Local environment: redhat6.7 system. nginx1.12.1, php7.1.0, the code uses the yii2 framework problem: the local web site needs to use the elasticsearch service. When PHP uses elasticsearch built on a local server, the local load is normal. When I use AWS's elasticsearch service, the load on the local server is often too high. Check the nginx and php logs and find no exceptions. The number of concurrent connections in the system is also not high. At this time, I thought of a strace diagnostic tool that our boss told me. Debugging process: Find a php sub-process idstrace-

See all articles