Joke Collection Website - Mood Talk - In-depth explanation of Nginx+PHP cache

In-depth explanation of Nginx+PHP cache

The following is a detailed analysis of PHP cache in Nginx, which can be referenced by friends in need? Nginx cache Nginx has two caching mechanisms: fastcgi_cache and proxy_cache. Let's talk about the difference between these two caching mechanisms. Proxy_cache is used to cache the content of the back-end server, which can be any content, including static and dynamic fastcgi_cache is used to cache a large amount of content generated by fastcgi. The situation is that the dynamic content proxy_cache cache generated by php reduces the communication times between nginx and the backend, saving transmission time and backend bandwidth. Fastcgi_cache cache reduces the number of communication between nginx and php, and reduces the pressure on php and database. Proxy cache cache settings.

The copy code is as follows: # Note that the paths specified by proxy_temp_path and proxy_cache_path must be in the same partition proxy _ temp _ path/data/proxy _ temp _ dir; # Set the name of the Web cache area as cache_one and the size of the memory cache space as MB. Automatically clear content that has not been visited for many days. The size of hard disk cache space is gbproxy _ cache _ path/data/proxy _ cache _ dirlevels =: keys _ zone = cache _ one: inactive = dmax _ size = g; Server {listener _ nameyourdomain index index index; root/data/htdocs/; Location/{# If the back-end server returns errors such as execution timeout, it will automatically forward the request to another server in the upstream load balancing pool to realize failover proxy _ next _ upstream _ _ errortimeout invalid _ header; Proxy _ cache _ one # sets different cache times for different HTTP status codes proxy _ cache _ valid h;; The key value Nginx of Web cache consists of domain name URI parameters, and the cache content is hashed according to the key value and stored in the secondary cache directory proxy _ cache _ key $ host $ uri $ is _ args $ args; Proxy _ set _ header Host $ host is proxy _ set _ headerx forwarded by $remote_addr; Proxy _ pass// backend _ server; Maturity d; } # Used to clear the cache Assuming that a URL is a cache that can be cleared by accessing location ~ /purge(/ *) {# Setting allows only the specified IP or IP segment to clear the URL cache allow; Allow/; Deny everything; proxy _ cache _ purge cache _ one $ host $ $ is _ args $ args; } # Dynamic applications with extension ending in php jsp cgi don't cache location ~ * (php|jsp|cgi)? $ {proxy _ set _ header Host $ host is the proxy _ set _ headerx forwarded by $remote_addr; Proxy _ pass// backend _ server; } access _ log off}} fastcgi_cache cache settings

The copy code is as follows: # Define folder fastcgi _ cache _ path/TT/cachelevels =: keys _ zone = name: inactive = dmax _ size = g; # Define to cache different url requests fastcgi _ cache _ key "$ scheme $ request _ method $ host $ uri $ arg _ filename $ arg _ x $ arg _ y"; Server {listenserver_name example; location/{ root/; index index index php} location ~(| PHP)$ { root/; fastcgi _ pass:; Fastcgi_cache name; fastcgi _ cache _ valid h; Fastcgi _ cache _ min _ uses fastcgi _ cache _ use _ stall error timeout invalid _ header _ fastcgi _ index PHP fastcgi _ paramscript _ filename/scripts $ fastcgi _ script _ name; Including fastcgi conf#, during the process of setting the cache, it was found that cookies could not be obtained. After investigation, You need to define this sentence fastcgi _ pass _ header set cookie} log _ format access $ remote _ addr $ remote _ user [$ time _ local]. " $ request " $ status $ body _ bytes _ sent " $ _ referer " " $ _ user _ agent " $ _ x _ forwarded _ for; Access_log/} Generally speaking, nginx's proxy_cache and fastcgi_cache have similar cache configurations. Memcache Cache Before discussing memcache Cache, let's take a look at mysql's memory cache. Mysql's memory cache can be specified in my cnf. The memory table is different from the temporary table, and the temporary table has the largest memory. Memory needs to be set through tmp _ table _ size = m, and when the data has checked the maximum value of the temporary table, it will be automatically converted into a disk table. At this time, due to the need of IO operation, the performance will be greatly reduced, the memory table will not be full, and the error situation of full data will be prompted.

The copy code is as follows: create table test (id int unsigned not null auto _ increment primary key state char () type char () date char ()). engine = memory default charset = utf lishixinzhi/Article/program/PHP/20 13 1 1/2 1248