Configuration and deployment of high performance Web server Nginx (13) Application Module memcached module +proxy_cache Double-layer Cache mode

Source: Internet
Author: User

Through the research of the configuration and deployment of high-performance Web server Nginx-(11) Application module of the memcached module of the two major scenarios of the article, we know how Nginx read data from memcached, if hit, then the efficiency is quite high. So:


1. What if I don't hit it?


We can read the data to the appropriate data server and then cache it on the Nginx server before returning the data to the client. Thus, for this resource, only the first request that penetrates the memcached is required to be read to the data server, and all requests that fall within the cache expiration time are read nginx locally. But Nginx Proxy_cache is local hard disk cache, the efficiency is much lower than memcached.


2. How should I install and configure it?


(1) Httpmemcmodule module

If you use the Nginx non-core Module--httpmemcmodule module, you can download the module:

Http://github.com/agentzh/memc-nginx-module/tags


    1. [Email protected]:~$ tar-zxvf agentzh-memc-nginx-module-a0bc33a.tar.gz
    2. [Email protected]:~$ tar-zxvf nginx-1.1.12.tar.gz
    3. [Email protected]:~$ cd nginx-1.1.12
    4. [Email protected]:~$./configure--add-module=/home/michael/agentzh-memc-nginx-module-a0bc33a
    5. [Email protected]:~$ sudo make
    6. [Email protected]:~$ sudo make install


At present, it is verified that Nginx 1.0.10 version Nginx 1.1.3 and its previous version, need to be added via--add-module to load the upstream-keepalive module, please google by yourself. Other versions are not sure, and the guess is that the 1.1 automatically carries the Upstream-keeplive module starting with the Nginx 1.1.4 and subsequent versions. (Thanks to Agentzh's tips)


Then let's edit the configuration file. As follows:

  1. HTTP {
  2. ...
  3. Upstream Data_server {
  4. Server 192.168.0.133:1234;
  5. Server 192.168.0.134:1234;
  6. Server 192.168.0.135:1234;
  7. Ip_hash;
  8. }
  9. Upstream Memc_backend {
  10. Server 127.0.0.1:11211;
  11. }
  12. ...
  13. server {
  14. Listen 8080;
  15. server_name localhost;
  16. Default_type text/html;
  17. Location/{
  18. Set $MEMC _cmd get;
  19. Set $MEMC _key $uri;
  20. Memc_pass Memc_backend;
  21. Error_page 404 @fallback;
  22. }
  23. Location @fallback {
  24. Internal
  25. Proxy_pass Http://data_server;
  26. Proxy_cache Cache_one;
  27. Proxy_cache_valid 302 1h;
  28. Proxy_cache_valid 301 1d;
  29. Proxy_cache_valid any 1m;
  30. Expires 30d;
  31. }
  32. }
  33. ...
  34. }


From the above configuration file we can see that a request arrives, its URI as key to go to memcached server 127.0.0.1:11211 to find value, if there is no hit, then return 404. At this time through the error_page will receive 404 to @fallback, and then go to Data_server to fetch the file, take the file in the local disk cache, and the user's browser through the expires set the cache times.


If the majority of requests are blocked by the memory cache of the first layer of memcached, the remaining requests can be reduced by accessing the hard disk cache files of the second Nginx server.


In the above way, the client gets a request response that contains the correct file content, but the status code is 404 (can be observed through fiddler). This seems to be causing problems. What's the problem? The vast majority of browsers, even in the case of 404, will try to read the content, if there is the correct content, it can be displayed correctly. But the more common two scenarios that can cause problems are:

(a), search engine spider climbed to 404, generally do not include the URL, I think this is not what you want to see;

(b), Flash and other ways to load, if the head is 404, may not show, I think it is not what you want to see.


(reprint please indicate from Poechant's CSDN blog: http://blog.csdn.net/poechant/article/details/7179891)


Then we change it to Chengdu is 200, is not very good?

    1. Error_page 404 =200 @fallback;


Non-also, so that 404 passes to the fallback after processing the request state if it is not 200, it is inconsistent, will cause more problems. So it should be configured as follows:

    1. Error_page 404 = @fallback;


So what is the status of the fallback processing result, replacing 404 with what?


(2) memcached module inside Nginx

If you want to use the Nginx internal memcached module, the configuration file can be combined with reference to the above example and "high-performance Web server Nginx configuration and Deployment research-(11) Application module of the memcached module of the two major scenarios"

Configuration and deployment of high performance Web server Nginx (13) Application Module memcached module +proxy_cache Double-layer Cache mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.