Use nginx as the Server Load balancer and Web Cache Server
Nginx's Web Cache service mainly consists of proxy_cache instruction sets and fastcgi_cache instruction sets. In the latest Nginx version 0.8.32, proxy_cache and fastcgi_cache are well-developed and can replace Squid and be used as the Server Load balancer server and Web Cache Server.
Nginx versions 0.7.48 and later support cache functions similar to Squid. This cache uses the URL and related combinations as the Key and stores them on the hard disk after md5 hash. Therefore, it supports any URL link, it also supports non-404/301 status codes such as 302/200. Although the official Nginx Web Cache service can only set an expiration time for a specified URL or status code, unlike the Squid PURGE command, you can manually clear the specified cache page. However, A third-party Nginx module can clear the cache of a specified URL.
The Nginx Web Cache service consists of the proxy_cache instruction sets and fastcgi_cache instruction sets. The former is used to cache the back-end content source server for reverse proxy, the latter is mainly used to cache FastCGI dynamic programs. The two functions are basically the same.
In the latest Nginx 0.8.32, proxy_cache and fastcgi_cache have been well-developed. In addition, the third-party ngx_cache_purge module (used to clear the cache of the specified URL) can completely replace Squid. We have used the proxy_cache cache function of Nginx in the production environment for more than two months, which is very stable and not inferior to Squid.
In terms of functions, Nginx already has the Web cache acceleration function of Squid and the function of clearing the specified URL cache. In terms of performance, Nginx's use of multi-core CPU is much better than Squid. In addition, Nginx is much more powerful than Squid in reverse proxy, Server Load balancer, health check, backend server failover, Rewrite, and ease of use. This allows an Nginx instance to be used as both the "Server Load balancer" and "Web Cache Server.
1. Compile and install Nginx Server Load balancer and cache server in Linux:
- Ulimit-SHn65535
- Wgetftp: // ftp. csx. cam. ac. uk/pub/software/programming/pcre/pcre-8.00.tar.gz
- Tarzxvfpcre-8.00.tar.gz
- Cdpcre-8.00/
- ./Configure
- Make & makeinstall
- Cd ../
- Wgethttp: // labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz
- Tarzxvfngx_cache_purge-1.0.tar.gz
- Wgethttp: // nginx.org/download/nginx-0.8.32.tar.gz
- Tarzxvfnginx-0.8.32.tar.gz
- Cdnginx-0.8.32/
- ./Configure-user = www-group = www-add-module = ../ngx_cache_purge-1.0-prefix =/usr/local/webserver/nginx-with-http_stub_status_module-with-http_ssl_module
- Make & makeinstall
- Cd ../
2. the/usr/local/webserver/nginx/conf/nginx. conf configuration file is as follows:
- Userwwwwww;
- Worker_processes8;
- Error_log/usr/local/webserver/nginx/logs/nginx_error.logcrit;
- Pid/usr/local/webserver/nginx. pid;
- # Specifiesthevalueformaximumfiledescriptorsthatcanbeopenedbythisprocess.
- Worker_rlimit_nofile65535;
- Events
- {
- Useepoll;
- Worker_connections65535;
- }
- Http
- {
- Includemime. types;
- Default_typeapplication/octet-stream;
- Charsetutf-8;
- Server_names_hash_bucket_size128;
- Client_header_buffer_size32k;
- Large_client_header_buffers432k;
- Client_max_body_size300m;
- Sendfileon;
- Tcp_nopushon;
- Keepalive_timeout60;
- Tcp_nodelayon;
- Client_body_buffer_size512k;
- Proxy_connect_timeout5;
- Proxy_read_timeout60;
- Proxy_send_timeout5;
- Proxy_buffer_size16k;
- Proxy_buffers464k;
- Proxy_busy_buffers_size128k;
- Proxy_temp_file_write_size128k;
- Gzipon;
- Gzip_min_length1k;
- Gzip_buffers416k;
- Gzip_http_version1.1;
- Gzip_comp_level2;
- Gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
- Gzip_varyon;
- # Note: The paths specified by proxy_temp_path and proxy_cache_path must be in the same partition.
- Proxy_temp_path/data0/proxy_temp_dir;
- # Set the name of the Web cache area to cache_one. The size of the memory cache space is 200 MB. cache is cleared once a day, and the size of the hard disk cache space is 30 GB.
- Proxy_cache_path/data0/proxy_cache_dirlevels = 1: 2keys_zone = cache_one: 200 minactive = 1dmax_size = 30g;
- Upstreambackend_server {
- Server192.168.8.43: 80 weight = 1max_fails = 2fail_timeout = 30 s;
- Server192.168.8.44: 80 weight = 1max_fails = 2fail_timeout = 30 s;
- Server192.168.8.45: 80 weight = 1max_fails = 2fail_timeout = 30 s;
- }
- Server
- {
- Listen80;
- Server_namewww.yourdomain.com192.168.8.42;
- Indexindex.htmlindex.htm;
- Root/data0/htdocs/www;
- Location/
- {
- # If the backend server returns errors such as 502, 504, and execution timeout, the request is automatically forwarded to another server in the upstream Server Load balancer pool for failover.
- Proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
- Proxy_cachecache_one;
- # Set different cache times for different HTTP Status Codes
- Proxy_cache_valid20030412h;
- # Combine domain names, Uris, and parameters into the Key values of the Web cache. Nginx hashes the cached content to the second-level cache directory based on the Key values.
- Proxy_cache_key $ host $ uri $ is_args $ args;
- Proxy_set_headerHost $ host;
- The proxy_set_headerX-Forwarded-For $ remote_addr;
- Proxy_passhttp: // backend_server;
- Expires1d;
- }
- # It is used to clear the cache. If a URL is http: // 192.168.8.42/test.txt, you can clear the cache of the URL by accessing http: // 192.168.8.42/purge/test.txt.
- Location ~ /Purge (/.*)
- {
- # Only the specified IP address or IP segment is allowed to clear the URL cache.
- Allow127.0.0.1;
- Allow192.168.0.0/16;
- Denyall;
- Proxy_cache_purgecache_one $ host $1 $ is_args $ args;
- }
- # Dynamic applications whose extensions end with. php,. jsp, And. cgi are not cached.
- Location ~. * \. (Php | jsp | cgi )? $
- {
- Proxy_set_headerHost $ host;
- The proxy_set_headerX-Forwarded-For $ remote_addr;
- Proxy_passhttp: // backend_server;
- }
- Access_logoff;
- }
- }
3. Start Nginx:
- /Usr/local/webserver/nginx/sbin/nginx
4. Example of clearing the specified URL cache:
Address: http://blog.s135.com/nginx_cache/