Nginx做缓存及代理均衡

编译安装缓存nginx:

yum install -y gcc gcc-c++ openssl-devel zlib-devel
tar xf pcre-8.34.tar.gz 
cd pcre-8.34
./configure && make && make install 
cd ..
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar xf ngx_cache_purge-2.3.tar.gz -C /opt/app
tar xf nginx-1.4.7.tar.gz
cd nginx-1.4.7
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/opt/app/ngx_cache_purge-2.1/



这里我添加一块磁盘做缓存

fdisk /dev/sdb
n
p
1
w
q
mkfs.ext3 /dev/sdb1
mkdir /cache
mount /dev/sdb1 /cache


nginx-cache主要配置:

[root@centos-server ~]# cat /usr/local/nginx/conf/nginx.conf
 user www www;
worker_processes 1;
error_log /usr/local/nginx/logs/error.log crit;
pid /usr/local/nginx/nginx.pid;
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/plainapplication/x-javascript text/css application/xml;
      gzip_vary on;
 
      proxy_temp_path /cache/proxy_temp_dir;
      proxy_cache_path /cache/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
 
upstream backend_server {
      server 192.168.150.131:80 weight=1 max_fails=2 fail_timeout=30s;
      server 192.168.150.129:80 weight=1 max_fails=2 fail_timeout=30s;
}
server
{
      listen 80;
      server_name localhost;
      index index.html index.htm;
      root /cache/web;
      location /
      {
        root /cache/web;  
index index.html index.htm;
proxy_next_upstream http_502 http_504 error timeout invalid_header;#不可达就转发给其它服务器处理
           proxy_cache cache_one;#在location匹配的的URL被缓存
           proxy_cache_valid 200 304 12h;#对返回状态码200和304的缓存12小时,200表示服务器处理成功,304表示网页内容未改变
           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;
      }
      location ~ /purge(/.*)#设置手动清理缓存
      {
           auth_basic "TDT  Center  CACHE  Center";
        #   auth_basic_user_file /tmp/htpasswd;#如果想保护缓存安全可以添加验证信息
           allow 127.0.0.1;
           allow 192.168.150.0/24;
           deny all;
           proxy_cache_purge cache_one $host$1$is_args$args;
      }
 
      location ~ .*\.(php|jsp|cgi)?$#不缓存动态内容
      {
           proxy_set_header Host $host;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_pass http://backend_server;
      }
  }
}


清除缓存:

wKioL1SjXTaQr0TJAAE5mSmQnYA840.jpg


你可能感兴趣的:(nginx,缓存,代理均衡)