WEB application

Nginx configures Proxy_cache or Fastcgi_cache cache acceleration under multi site

Jager · October 6 · 2015 · · 7541 times read

Zhang Ge has shared many tutorials about cache acceleration on his blog, and he is constantly groping for the best website static cache acceleration scheme. Here is a brief summary:

1. Friends who use virtual hosts recommend using caching plug-ins or php code version caching scheme=> Click here to arrive directly

2. Friends who use independent hosts recommend Nginx's Fastcgi pure static cache scheme=> Click here to arrive directly

 Nginx configures Proxy_cache or Fastcgi_cache cache acceleration under multi site

After I shared the Fastcgi cache of Nginx, many friends have succeeded. However, some friends reported that they did not know how to configure Nginx Fastcgi in a multi site environment. While all online sharing schemes are single site, I thought it would be better for multi site to directly insert the fastcgi cache configuration into the location module of php, but I found that an error will be reported, indicating that the cache space has been used.

After several rounds of testing by Zhang Ge, the Fastcgi cache configuration under multi site is finally tested. Let's share it briefly below.

1、 Deploy http module

① , single site

The previous article on a single site has been shared. Add the following configuration to the http module:

 fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:150m inactive=31d max_size=10G; fastcgi_temp_path /tmp/wpcache/temp; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

② , multiple sites

When you want to enable fastcgi caching for multiple sites, the above configuration will fail and an error will be reported. After testing, it can be modified as follows:

 #Site 1 cache configuration fastcgi_cache_path /tmp/zhangge_cache levels=1:2 keys_zone=zhang.ge:384m inactive=1d max_size=5G; #Site 2 cache configuration #If you want to enable more site caches, please continue to increase them. Note that the cache path and keys_zone of each site should be customized #Ps: The parameters in the code are only examples. In actual use, please modify according to the server configuration fastcgi_cache_path /tmp/zgboke_cache levels=1:2 keys_zone=zgboke.com:384m inactive=1d max_size=5G; #Other configurations can remain unchanged fastcgi_temp_path /tmp/temp_cache; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500;

2、 Deploy server module

After the http module is configured, the server module is very simple!

Just insert different fastcgi cache configurations under the php modules of different sites. In fact, this is just the difference between key_zones.

For example, I configured Zhang Ge Blog and China Blog Alliance at the same time as follows:

Zhang Ge's blog:

 server( #Other configurations are omitted location ~ [^/]\.php(/|$) { try_files $uri =404; fastcgi_pass  unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; #Fastcgi cache configuration fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; add_header X-Cache "$upstream_cache_status From $host"; fastcgi_cache zhang.ge; fastcgi_cache_valid 200 301 302 1d; } #Other configurations are omitted }

China Blog Alliance:

 server( #The above configuration is omitted location ~ [^/]\.php(/|$) { try_files $uri =404; fastcgi_pass  unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; #Fastcgi cache configuration fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; add_header X-Cache "$upstream_cache_status From $host"; fastcgi_cache zgboke.com; fastcgi_cache_valid 200 301 302 1d; } #The following configurations are omitted }

In fact, it is just a one-to-one correspondence with the cache defined in the http module, which can be distinguished! Otherwise, an error will be reported.

3、 Proxy_cache cache

We shared the fastcgi cache configuration of multi sites, and also shared the Proxy_cache cache configuration of multi sites. In case some friends don't follow suit...

In fact, I didn't actually test it, but by analogy, it should be configured as follows:

① , http module

 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; #There are also several proxy_cache_paths configured for several sites. Note that the cache path and keys_zone need to be customized #Ps: The parameters in the code are only examples. In actual use, please modify according to the server configuration proxy_cache_path /tmp/web1_cache levels=1:2 keys_zone=web1_cache:256m inactive=31d max_size=2g; proxy_cache_path /tmp/web2_cache levels=1:2 keys_zone=web2_cache:256m inactive=31d max_size=2g; proxy_temp_path /tmp/temp_cache;

② , server module

As for the server module, it should not be as detailed as the above description! The only difference between different sites is the proxy_cache configuration! For example:

Site 1 Configuration

 #Other configurations are omitted Proxy_cache web1_cache; # Fill in the keys_zone corresponding to the site proxy_cache_valid 200 302 304 301 1d; proxy_cache_valid any 1m;

Site 2 configuration:

 #Other configurations are omitted Proxy_cache web2_cache; # Fill in the keys_zone corresponding to the site proxy_cache_valid 200 302 304 301 1d; proxy_cache_valid any 1m;

Ps: Maybe someone will ask again, where is the configuration? Alas, what are you doing here? Write honestly!

Oh, understand the above configuration, and don't worry about deploying any number of sites' fastcgi or proxy caches!

19 responses
  1. bookcase 2015-10-6 · 14:34

    I don't understand, but I'd better grab a sofa first

  2. Lighthouse cloud 2015-10-6 · 17:47

    Boss, which plug-in is used for the image watermark of your website? The first picture of your blog has no watermark.

    •  avatar
      Jager 2015-10-6 · 21:54

      Daxia's: dx watermark

  3. god 2015-10-7 · 21:55

    This is good. It can be used soon, but there is no way to clean the cache for multiple sites

    • god 2015-10-8 · 1:14

      Can I delete it manually only

    •  avatar
      Jager 2015-10-10 · 15:05

      The cache is stored in different paths, which is no different from a single cleaning.

  4. An'an Blog 2015-10-8 · 10:28

    The website is still outstanding

  5. Nursing Street 2015-10-11 · 22:49

    Welcome to give me some advice, Great Xia

  6. Boys and girls silver version 2015-10-12 · 17:30

    The path in wp-config.php should also be changed, right?

    •  avatar
      Jager 2015-10-15 · 11:32

      Yes, I will update it later.

  7. Good, collect first!

  8. Jinxuan Online Earnings 2015-10-14 · 16:12

    Thanks for sharing

  9. deson 2016-3-11 · 10:32

    Please refer to # Site 1 Cache Configuration
    fastcgi_cache_path /tmp/zhangge_cache levels=1:2 keys_zone=zhang.ge:384m inactive=1d max_size=5G;
    #Site 2 cache configuration
    #If you want to enable more site caches, please continue to increase them. Note that the cache path and keys_zone of each site should be customized
    #Ps: The parameters in the code are only examples. In actual use, please modify according to the server configuration
    fastcgi_cache_path /tmp/zgboke_cache levels=1:2 keys_zone=zgboke.com:384m inactive=1d max_size=5G;
    Is this paragraph added to the http in nginx. conf? I can't start it by adding nginx... The key_zone of a single site is correctly specified... It is confirmed that it is not the service segment configuration of my vhost, because I cannot start nginx by deleting the conf configuration of a single site. Thank you for your advice.

    •  avatar
      Jager 2016-3-11 · 12:35

      See the startup error message

  10. technical nerd 2017-7-6 · 0:03

    Jager, I added the prompt Starting nginx: nginx: [emerg] "fastcgi_cache_key" direct is duplicate in/usr/local/nginx/conf/vhost/www.test. com. conf to other sites according to the tutorial. The command is repeated. This: fastcgi_cache_key "$scheme $request_method $host $request_uri";

    • technical nerd 2017-7-6 · 0:09

      Can I put the repeated instructions directly into/usr/local/nginx/conf/nginx.conf

  11. The moon climbs the stairs 2017-7-26 · 11:01

    Ha ha, it's done. The http will be added to nginx. conf, and the sever will be added to the conf of the site!

  12. Ke Shankang 2017-8-18 · 1:03

    Blogger, what should I do if the header does not have X-Cache after configuration and restart? Refresh for a long time

  13. sans 2019-6-26 · 22:36

    After a week of tossing and turning, the configuration of single site and multi site is finally completed. This article really helps a lot, making the website speed fly.