注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秋风扫落叶

 
 
 

日志

 
 
 
 

Nginx as a proxy_cache function to replace squid  

2012-06-22 10:16:56|  分类: 原创linux经验 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Nginx 0.7.48 version from the start, to support a similar function Squid cache. This cache is the URL and the relevant portfolio as Key, with the md5 hash to preserve in the hard disk, so it can support any URL links, but also supports 404/301/302 such a non-200 status code. Although the official Nginx Web cache service can only specify the URL or the status code set expiration time, the PURGE Squid does not support such instructions, manually clear a specific cache page, but through a third party Nginx module, you can clear the cache the specified URL .

Nginx’s Web caching services related mainly proxy_cache fastcgi_cache instruction set and related instruction set composition, the former agent for the reverse, on the back-end content source server cache, which is mainly used for FastCGI dynamic process cache. Both features are essentially the same.

The latest version of Nginx 0.8.32, proxy_cache and fastcgi_cache already fairly complete, with a third party ngx_cache_purge module (used to clear the specified URL in the cache), they can already completely replace the Squid. We have used in the production environment of proxy_cache Nginx caching more than two months, very stable, speed is not inferior to Squid.

In function, Nginx already have Squid Web cache owned by acceleration, clear the specified URL cache functions. In performance, Nginx on multi-core CPU utilization, better than a lot of Squid. In addition, the reverse proxy, load balancing, health checking, back-end server fail, Rewrite rewrite, ease of use, Nginx is also much stronger than the Squid. This makes a Nginx may also be a “load balancing server” and “Web cache server” to use.

1, Nginx installation:

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 configuration file contents:

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_rlimit_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: proxy_temp_path and proxy_cache_path specified path must be in the same partition
  proxy_temp_path   /data0/proxy_temp_dir;
  # Set the Web cache name cache_one, the memory cache space to 200MB, 1 days without access to the content to be automatically cleared space for the 30GB hard disk cache.
  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 back-end server returns 502,504, implementation of the overtime and other errors, automatically forwarding the request to the upstream load balancing pool to another server, to achieve failover.
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_cache cache_one;
         # Different HTTP status codes to set a different cache time
         proxy_cache_valid  200 304 12h;
         #To domain, URI, parameters into the Web cache Key value, Nginx hash value according to Key, the contents stored in the cache 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;
    }
 
    #Clear Cache,For example, a URL for the http://192.168.1.2/test.txt, by visitinghttp://192.168.1.2/purge/test.txt can clear the URL cache。
    location ~ /purge(/.*)
    {
     #Setting only allows you to specify the IP or IP segment can 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;
    } 
    #Extension to. Php,. Jsp,. Cgi at the end of the dynamic application does not cache。
    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

  评论这张
 
阅读(1812)| 评论(2)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017