nginx 配置负载均衡的缓存设置

直接上代码 /etc/nginx/nginx:

 

user root root;
worker_processes 2;
worker_cpu_affinity 01 01; 
error_log /var/log/nginx/error.log error;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
access_log_bypass_if ($uri = '/img404.jpg');
access_log_bypass_if ($status = 302);
sendfile on;
keepalive_timeout 65;
server_names_hash_bucket_size 128;
client_header_buffer_size 64k;
large_client_header_buffers 4 64k;
client_max_body_size 16M;
tcp_nopush on;
tcp_nodelay on;
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /tmp/nginx_temp 1 2; //表示临时文件存放的目录
proxy_buffering on;
proxy_connect_timeout 10;
proxy_read_timeout 180;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 24 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 256k;
proxy_max_temp_file_size 2048m;
proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=cache_one:20m inactive=6h max_size=2048m;//nginx_cache 表示缓存存放的目录;cache_one是缓存的名字,随便起的,后边要用到;6h表示6小时没人访问就自动更新缓存
upstream resinserver {
server 192.168.232.132:80;
server 192.168.232.133:80;
}
server {
listen 80;
server_name test.com; //test.com是不需要缓存的网站
location / {
proxy_pass http://resinserver/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80 default; //其他需要缓存的网站
location / {
proxy_cache cache_one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 302 6h; //保证在inactive时间内有被访问过的情况下,有效期是6小时,针对的是200 304 302这几个状态
expires 6h; //缓存6小时就过期

 

#proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
proxy_pass http://resinserver/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

注意:这个配置在chrome浏览器里边,会被保存,如果修改了配置,需要清理浏览器。不清理的话,可能不生效。