


Nginx Cache configuration plan and how to solve related memory usage problems
May 23, 2023 pm 02:01 PM5 options for nginx cache
1. One of the traditional caches (404)
This method is to direct the 404 error of nginx to the backend, and then use proxy_store to The returned page is saved.
Configuration:
location / { root /home/html/;#主目錄 expires 1d;#網(wǎng)頁(yè)的過(guò)期時(shí)間 error_page 404 =200 /fetch$request_uri;#404定向到/fetch目錄下 } location /fetch/ {#404定向到這里 internal;#指明這個(gè)目錄不能在外部直接訪問(wèn)到 expires 1d;#網(wǎng)頁(yè)的過(guò)期時(shí)間 alias /html/; proxy_store會(huì)將文件保存到這目錄下 proxy_pass//www.jb51.net/;#后端upstream地址,/fetch同時(shí)是一個(gè)代理 proxy_set_header accept-encoding '';#讓后端不要返回壓縮(gzip或deflate)的內(nèi)容,保存壓縮后的內(nèi)容會(huì)引發(fā)亂子。 proxy_store on;#指定nginx將代理返回的文件保存 proxy_temp_path /home/tmp;#臨時(shí)目錄,這個(gè)目錄要和/home/html在同一個(gè)硬盤分區(qū)內(nèi) }
When using it, please note that nginx must have permission to write files to /home/tmp and /home/html. In Linux, nginx is generally It will be configured to run as the nobody user, so these two directories must be chown nobody, and set to be exclusive to the nobody user. Of course, you can also chmod 777, but all experienced system administrators will recommend not to use 777 casually.
2. Traditional cache 2 (!-e)
The principle is basically the same as the 404 jump, but more concise:
location / { root /home/html/; proxy_store on; proxy_set_header accept-encoding ''; proxy_temp_path /home/tmp; if ( !-f $request_filename ) { proxy_pass//www.jb51.net/; } }
You can see this The configuration saves a lot of code compared to 404. It uses !-f to determine whether the requested file exists on the file system. If it does not exist, proxy_pass to the backend, and the return is also saved using proxy_store.
Both traditional caches have basically the same advantages and disadvantages:
Disadvantage 1: Dynamic links with parameters are not supported, such as read.php?id=1, because nginx only saves the file name, so this link only Save it as read.php in the file system, so that incorrect results will be returned when users access read.php?id=2. At the same time, it does not support the home page and secondary directory //www.jb51.net/download/ in the form of //www.jb51.net/, because nginx is very honest and will write such a request into the file system according to the link, and this The link is obviously a directory, so saving fails. In these cases, rewrite is required to save correctly.
Disadvantage 2: There is no mechanism for cache expiration and cleanup inside nginx. These cached files will be permanently stored on the machine. If there are a lot of things to be cached, it will fill up the entire hard disk space. For this purpose, you can use a shell script to clean it regularly, and you can write dynamic programs such as php to do real-time updates.
Disadvantage 3: Only 200 status codes can be cached, so status codes such as 301/302/404 returned by the backend will not be cached. If a pseudo-static link with a large number of visits happens to be deleted, it will continue. The penetration causes the rear end to bear considerable pressure.
Disadvantage 4: nginx will not automatically select memory or hard disk as the storage medium. Everything is determined by the configuration. Of course, there will be an operating system-level file caching mechanism in the current operating system, so there is no need to worry too much about file caching on the hard disk. IO performance problems caused by concurrent reads.
The shortcomings of nginx’s traditional caching are also its different features from caching software such as squid, so it can also be regarded as its advantages. In production applications, it is often used as a partner with Squid. Squid is often unable to block links with ?, but nginx can block their access, such as: http://jb51.net/? and http://jb51.net / will be treated as two links on Squid, so it will cause two penetrations; nginx will only save it once, no matter the link becomes http://jb51.net/?1 or http://jb51.net/? 123, cannot be cached by nginx, thus effectively protecting the backend host.
nginx will very faithfully save the link form to the file system, so that for a link, you can easily check its cache status and content on the cache machine, and you can also easily communicate with other file managers such as Used in conjunction with rsync, etc., it is completely a file system structure.
Both of these two traditional caches can save files to /dev/shm under Linux. Generally, I do this, so that the system memory can be used for caching. If the memory is used, the expiration content will be cleaned up faster. Much more. When using /dev/shm/, in addition to pointing the tmp directory to the /dev/shm partition, if there are a large number of small files and directories, you must also modify the number of inodes and the maximum capacity of this memory partition:
mount -o size=2500m -o nr_inodes=480000 -o noatime,nodiratime -o remount /dev/shm
The above command is used on a machine with 3g of memory. Because the default maximum memory of /dev/shm is half of the system memory, which is 1500m, this command will increase it to 2500m. At the same time, the number of shm system inodes is the default It may not be enough in some cases, but the interesting thing is that it can be adjusted at will. The adjustment here is 480000, which is a bit conservative, but it is basically enough.
3. Cache based on memcached
nginx has some support for memcached, but the function is not particularly strong, and the performance is still very good.
location /mem/ { if ( $uri ~ "^/mem/([0-9a-za-z_]*)$" ) { set $memcached_key "$1"; memcached_pass 192.168.1.2:11211; } expires 70; }
這個(gè)配置會(huì)將http://jb51.net/mem/abc指明到memcached的abc這個(gè)key去取數(shù)據(jù)。
nginx目前沒(méi)有寫入memcached的任何機(jī)制,所以要往memcached里寫入數(shù)據(jù)得用后臺(tái)的動(dòng)態(tài)語(yǔ)言完成,可以利用404定向到后端去寫入數(shù)據(jù)。
4、基于第三方插件ncache
ncache是新浪兄弟開(kāi)發(fā)的一個(gè)不錯(cuò)的項(xiàng)目,它利用nginx和memcached實(shí)現(xiàn)了一部分類似squid緩存的功能,我并沒(méi)有使用這個(gè)插件的經(jīng)驗(yàn),可以參考:
http://code.google.com/p/ncache/
5、nginx新開(kāi)發(fā)的proxy_cache功能
從nginx-0.7.44版開(kāi)始,nginx支持了類似squid較為正規(guī)的cache功能,目前還處于開(kāi)發(fā)階段,支持相當(dāng)有限,這個(gè)緩存是把鏈接用md5編碼hash后保存,所以它可以支持任意鏈接,同時(shí)也支持404/301/302這樣的非200狀態(tài)。
配置:
首先配置一個(gè)cache空間:
復(fù)制代碼 代碼如下:
proxy_cache_path /path/to/cache levels=1:2 keys_zone=name:10m inactive=5m max_size=2m clean_time=1m;
注意這個(gè)配置是在server標(biāo)簽外,levels指定該緩存空間有兩層hash目錄,第一層目錄是1個(gè)字母,第二層為2個(gè)字母,保存的文件名就會(huì)類似/path/to/cache/c/29/b7f54b2df7773722d382f4809d65029c;keys_zone為這個(gè)空間起個(gè)名字,10m指空間大小為10mb;inactive的5m指緩存默認(rèn)時(shí)長(zhǎng)5分鐘;max_size的2m是指單個(gè)文件超過(guò)2m的就不緩存;clean_time指定一分鐘清理一次緩存。
location / { proxy_pass//www.jb51.net/; proxy_cache name;#使用name這個(gè)keys_zone proxy_cache_valid 200 302 1h;#200和302狀態(tài)碼保存1小時(shí) proxy_cache_valid 301 1d;#301狀態(tài)碼保存一天 proxy_cache_valid any 1m;#其它的保存一分鐘 }
ps:支持cache的0.7.44到0.7.51這幾個(gè)版本的穩(wěn)定性均有問(wèn)題,訪問(wèn)有些鏈接會(huì)出現(xiàn)錯(cuò)誤,所以這幾個(gè)版本最好不要在生產(chǎn)環(huán)境中使用。nginx-0.7下目前所知較為穩(wěn)定的版本是0.7.39。穩(wěn)定版0.6.36版也是近期更新,如果在配置里沒(méi)有使用到0.7的一些新標(biāo)簽新功能,也可以使用0.6.36版。
nginx緩存的內(nèi)存占用問(wèn)題的一般解決方法
1、前些日子某服務(wù)被刷,每分鐘達(dá)到上幾百萬(wàn)請(qǐng)求;當(dāng)時(shí)采用了nginx cache來(lái)解決的;但是因?yàn)槟撤?wù)不能緩存太久,當(dāng)時(shí)設(shè)置了5s,那么帶來(lái)的問(wèn)題就是產(chǎn)生大量小文件,而且很快就刪除了。
2、通過(guò)
free -m
會(huì)發(fā)現(xiàn)used是27g;但是通過(guò)top查看進(jìn)程占的內(nèi)存并沒(méi)有那么多
那內(nèi)存去哪了?
3、通過(guò)查閱資料會(huì)發(fā)現(xiàn)(cat /proc/meminfo)
slab: 22464312 kb
sreclaimable: 16474128 kb (這些是內(nèi)核保持的但是可以釋放的inode和dentry的緩存)
sunreclaim: 5990184 kb
4、這些內(nèi)存為什么會(huì)不自動(dòng)清理呢?
某機(jī)房機(jī)器系統(tǒng)版本:linux 2.6.32-431.el6.x86_64 #1 smp fri nov 22 03:15:09 utc 2013 x86_64 x86_64 x86_64 gnu/linux(正常,沒(méi)出現(xiàn)內(nèi)存快到100%的情況)
某機(jī)房機(jī)器系統(tǒng)版本:linux 2.6.32-279.el6.x86_64 #1 smp fri jun 22 12:19:21 utc 2012 x86_64 x86_64 x86_64 gnu/linux (不釋放)
5、通過(guò)設(shè)置如下參數(shù)來(lái)設(shè)置內(nèi)存閥值
sysctl -w vm.extra_free_kbytes=6436787 sysctl -w vm.vfs_cache_pressure=10000
The above is the detailed content of Nginx Cache configuration plan and how to solve related memory usage problems. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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".

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 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.

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]

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

Practical Tips for Improving PhpStorm Performance in CentOS Systems This article provides a variety of methods to help you optimize the performance of PhpStorm in CentOS systems and thus improve development efficiency. Before implementing any optimization measures, be sure to back up important data and verify the results in the test environment. 1. System-level optimization and streamline system services: Disable unnecessary system services and daemons to reduce system resource usage. Interfaceless Mode: Switching to interfaceless mode can significantly save resources if you do not need a graphical interface. Uninstall redundant software: Remove software packages and services that are no longer in use and free up system resources. 2. PHP configuration optimization enable OPcache: install and configure OPcache extensions to display

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.
