使用Nginx的proxy_cache緩衝功能取代Squid

來源:互聯網
上載者:User

[文章作者:張宴 本文版本:v1.2 最後修改:2009.01.12 轉載請註明原文連結:http://blog.s135.com/nginx_cache/]

  Nginx從0.7.48版本開始,支援了類似Squid的緩衝功能。這個緩衝是把URL及相關組合當作Key,用md5編碼雜湊後儲存在硬碟上,所以它可以支援任意URL連結,同時也支援404/301/302這樣的非200狀態代碼。雖然目前官方的Nginx Web快取服務只能為指定URL或狀態代碼設定到期時間,不支援類似Squid的PURGE指令,手動清除指定快取頁面面,但是,通過一個第三方的Nginx模組,可以清除指定URL的緩衝。

  Nginx的Web快取服務主要由proxy_cache相關指令集和fastcgi_cache相關指令集構成,前者用於反向 Proxy時,對後端內容來源伺服器進行緩衝,後者主要用於對FastCGI的動態程式進行緩衝。兩者的功能基本上一樣。

  最新的Nginx 0.8.32版本,proxy_cache和fastcgi_cache已經比較完善,加上第三方的ngx_cache_purge模組(用於清除指定URL的緩衝),已經可以完全取代Squid。我們已經在生產環境使用了 Nginx 的 proxy_cache 緩衝功能超過兩個月,十分穩定,速度不遜於 Squid。

  在功能上,Nginx已經具備Squid所擁有的Web緩衝加速功能、清除指定URL緩衝的功能。而在效能上,Nginx對多核CPU的利用,勝過Squid不少。另外,在反向 Proxy、負載平衡、健全狀態檢查、後端伺服器容錯移轉、Rewrite重寫、易用性上,Nginx也比Squid強大得多。這使得一台Nginx可以同時作為“負載平衡伺服器”與“Web快取服務器”來使用。
  


  1、Nginx 負載平衡與快取服務器在 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 設定檔內容如下: 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;

  #註:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區
  proxy_temp_path   /data0/proxy_temp_dir;
  #設定Web緩衝區名稱為cache_one,記憶體緩衝空間大小為200MB,1天沒有被訪問的內容自動清除,硬碟緩衝空間大小為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 /
    {
         #如果後端的伺服器返回502、504、執行逾時等錯誤,自動將請求轉寄到upstream負載平衡池中的另一台伺服器,實現容錯移轉。
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_cache cache_one;
         #對不同的HTTP狀態代碼設定不同的緩衝時間
         proxy_cache_valid  200 304 12h;
         #以網域名稱、URI、參數組合成Web緩衝的Key值,Nginx根據Key值雜湊,儲存緩衝內容到二級緩衝目錄內
         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;
    }
    
    #用於清除緩衝,假設一個URL為http://192.168.8.42/test.txt,通過訪問http://192.168.8.42/purge/test.txt就可以清除該URL的緩衝。
    location ~ /purge(/.*)
    {
     #設定只允許指定的IP或IP段才可以清除URL緩衝。
     allow            127.0.0.1;
     allow            192.168.0.0/16;
     deny            all;
     proxy_cache_purge    cache_one   $host$1$is_args$args;
    }    

    #副檔名以.php、.jsp、.cgi結尾的Live App程式不緩衝。
    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、啟動 Nginx: /usr/local/webserver/nginx/sbin/nginx



  4、清除指定的URL緩衝樣本:

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.