Nginxproxy_cache is a complete caching solution similar to squid. It is a function added after version 0.7.44 and has been developed for a long time. It must be stable. You can only use proxy_store to cache pages before proxy_cache is available, because nginx does not have any refresh mechanism designed for proxy_store, so you have to add
Nginx proxy_cache is similar to squIdThe complete cache solution, which is added after version 0.7.44, has been developed for a long time and must be stable. You can only use proxy_store to cache pages before proxy_cache is available. nginx does not have any refresh mechanism designed for proxy_store. Therefore, you have to add the externally designed refresh function, such as shell.PhpAnd so on. It is easier to use proxy_cache.
1. proxy_cache
There are many examples of proxy_cache on the Internet that can be copied. I also copied a simple configuration:
Proxy_cache_path/dev/shm/nginx_cache leveLs= Keys_zone = cache_one: 200 m inactive = 1d max_size = 200 m;
Server {
LoCatIon /{
Proxy_pass http: // 192.168.0.123;
Proxy_cache cache_one;
Proxy_cache_valid 200 302 301 1d;
Proxy_cache_key $ host $ uri $ is_args $ args;
Proxy_hide_header Vary;
Proxy _Set_ Header Host $ host;
Proxy_set_header Accept-ENcOding '';
Proxy_set_header X-ForwardEd-For $ remote_aDdR;
Proxy_ignore_headers Cache-ConTrOl Expires;
}
}
I want to create a server similar to a transparent proxy. I don't need to configure too many domain names on this cache machine. The configuration above will throw all domain names to 192.168.0.123 in the background, saving you trouble.
Note: This nginx instance requires proxy_set_header Accept-Encoding '';Zip) So that the machine will not be cachedCompression.
Proxy_ignore_headers Cache-Control Expires; after this configuration item is added, proxy_cache supports background settingsExPires.
Ii. cache_purge
Cache_purge is a third-party module. You can use the cache_purge module to manually delete a link in the cache for real-time update. on this page, you can download it:
Http://labs.frickle.com/nginx_cache_purge/
The configuration code is provided on the above page. Generally, the test can be copied directly and the installation is relatively simple. -- add-moDuLe =.
Location ~ /Purge (/.*){
Allow 192.168.0.0/24;
Deny all;
Proxy_cache_purge cache_one $ host $1 $ is_args $ args;
}
Use this configuration and enter:
Curl http: // www.SuDone.com/purge/index.html
It is actually quite convenient to refresh the cache, but there will be a problem. I want to refresh a page, such as http://www.sudone.com/index.html, so it is not convenient to insert a purgein the middle of the page. There are many ways to solve this problem. For example, put purge behind the link and change it to "commit.
The configuration method is to use if to determine that $ request_method is PURGE, and then go to/purge/to process it:
Location /{
... # Configuration above
If ($ request_method = PURGE ){
ReWrite^ (. *) $/Purge $1 last;
}
}
Use squidclient to test configuration modification. Test method:
Squidclient-h192.168.0.123-p80-mPURGE-v http://www.sudone.com/index.html
If the test fails, a 405 Method not allowed is thrown. Think ...... It cannot be solved.
However, there is a solution to 405 of the problems, which is very simple.
Location ~ /Purge (/.*){
... # Configuration above
Error_page 405 = 200/purge $1;
}
When a 405 error occurs, the system forwards it to itself. The problem is solved and the test is successful.
A complete configuration is provided:
Proxy_cache_path/dev/shm/nginx_cache levels = keys_zone = cache_one: 200 m inactive = 1d max_size = 200 m;
Server {
Location /{
Proxy_pass http: // 192.168.0.123;
Proxy_cache cache_one;
Proxy_cache_valid 200 302 301 1d;
Proxy_cache_key $ host $ uri $ is_args $ args;
Proxy_hide_header Vary;
Proxy_set_header Host $ host;
Proxy_set_header Accept-Encoding '';
Proxy_set_header X-Forwarded-For $ remote_addr;
If ($ request_method = PURGE ){
Rewrite ^ (. *) $/purge $1 last;
}
}
Location ~ /Purge (/.*){
Allow 192.168.0.0/24;
Deny all;
Error_page 405 = 200/purge $1;
Proxy_cache_purge cache_one $ host $1 $ is_args $ args;
}
}