Using Nginx as both a web cache server and load balancing

  

Nginx starts with version 0.7.48 and supports Squid-like caching. This cache uses the URL and related combinations as keys, and is encoded on the hard disk by md5 encoding hash, so it can support any URL link, and also supports non-200 status codes such as 404/301/302. Although the current Nginx Web Cache service can only set the expiration time for the specified URL or status code, it does not support Squid-like PURGE instructions, manually clear the specified cache page, but through a third-party Nginx module, you can clear the cache of the specified URL. .

Nginx's Web Cache service consists mainly of the proxy_cache related instruction set and the fastcgi_cache related instruction set. The former is used for the reverse proxy, the backend content source server is cached, and the latter is mainly used for the FastCGI dynamics. The program is cached. The functions of the two are basically the same.

The latest Nginx version 0.8.32, proxy_cache and fastcgi_cache are relatively complete, plus the third-party ngx_cache_purge module (used to clear the cache of the specified URL), can completely replace Squid. We have used Nginx's proxy_cache caching feature for more than two months in production, which is very stable and not inferior to Squid.

In terms of functionality, Nginx already has the Web cache acceleration feature that Squid has, and the ability to clear the specified URL cache. In terms of performance, Nginx's use of multi-core CPU is better than Squid. In addition, Nginx is much more powerful than Squid in reverse proxy, load balancing, health checks, backend server failover, Rewrite rewrite, and ease of use. This allows an Nginx to be used simultaneously as a "load balancing server" & "quo; web cache server".

1, Nginx load balancing and cache server compile and install under Linux:


ulimit -SHn 65535 wget ftp://ftp.csx.cam.ac. uk /pub /software /programming /pcre /pcre-8.00.tar.gz tar zxvf pcre-8.00.tar.gz cd pcre-8.00 /./configure make & & make install cd ../wget http: //Labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz tar zxvf ngx_cache_purge-1.0.tar.gz wget http://nginx.org/download/nginx-0.8.32.tar.gz tar zxvf nginx-0.8. 32.tar.gz cd nginx-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 && make install cd ../

2, /usr/local/webserver/nginx/conf/nginx.conf The configuration file reads as follows:


user www www; worker_processes 8; error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; pid /usr/local/webserver/nginx/nginx.pid; #Specifies the Value for maximum file descriptors that can be opened by this process. worker_rlimi t_nofile 65535; events {use epoll; worker_connections 65535;} http {include mime.types; default_type application /octet-stream; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; client_body_buffer_size 512k; 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; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text /Plain application/x-javascript
text/css application/xml; gzip_vary on; #Note: The path specified by proxy_temp_path and proxy_cache_path must be in the same partition proxy_temp_path /data0/proxy_temp_dir; #Set the web cache name to cache_one, The memory cache space is 200MB, and the cache is cleaned once a day. The size of the hard disk cache is 30GB. proxy_cache_path /data0 /proxy_cache_dir levels = 1: 2 keys_zone = cache_one: 200m inactive = 1d max_size = 30g; upstream backend_server {server 192.168.8.43:80 weight = 1 max_fails = 2 fail_timeout = 30s; server 192.168.8.44:80 weight = 1 max_fails = 2 fail_timeout = 30s; server 192.168.8.45:80 weight = 1 max_fails = 2 fail_timeout = 30s;} server {listen 80; server_name www.yourdomain.com 192.168.8.42; index index.html index.htm; root /data0 /htdocs/www; location /{ # If the backend server returns 502, 504, execution timeout and other errors, the request is automatically forwarded to another server in the upstream load balancing pool for failover. Proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; #Set different caching time for different HTTP status codes proxy_cache_valid 200 304 12h; #Use domain name, URI, parameter to form the key value of the web cache, Nginx stores according to the key value hash Cache content to the secondary cache directory proxy_cache_key $host$uri$is_args$args; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://backend_server; expires 1d; } # is used to clear the cache, assuming A URL is http://192.168.8.42/test.txt. You can clear the cache of the URL by visiting http://192.168.8.42/purge/test.txt. Location ~ /purge(/.*) { #Setting only allows the specified IP or IP segment to clear the URL cache. Allow 127.0.0.1; allow 192.168.0.0/16; deny all; proxy_cache_purge cache_one $host$1$is_args$args; } # Dynamic applications whose extensions end in .php, .jsp, .cgi are not cached. Location ~ .*\\.(php| Jsp| ? Cgi) $ {proxy_set_header Host $ host; proxy_set_header X-Forwarded-For $ remote_addr; proxy_pass http: //backend_server;} access_log off;}}

3, start Nginx:

/Usr/local/webserver/nginx/sbin/nginx



4. Clear the specified URL cache example:
Clear the specified URL cache example

Copyright © Windows knowledge All Rights Reserved