一、配置Nginx缓存。
参考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_path 语法 proxy_cache_path path [levels=levels] keys_zone=name:size [inactive=time] [max_size=size] [loader_files=number] [loader_sleep=time] [loader_threshold=time]; 配置格式: proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m; #定义/data/nginx/cache为缓存目录;levels=1:2 有2级子目录,最多为3级子目录,用冒号隔开定义3个数字,每个数字表示其目录名称;keys_zone=one:10m 用多大的空间保存键值; 1.nginx缓存设置: vim /data/nginx/conf/nginx.conf user nginx nginx; worker_processes auto; pid /data/nginx/logs/nginx.pid; worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; multi_accept on; } http { include mime.types; default_type application/octet-stream; charset utf-8; log_format main ‘$remote_addr - $remote_user [$time_local] "$request" $http_host‘ ‘$status $upstream_status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$upstream_addr" "$http_x_forwarded_for" ‘; #access_log logs/access.log main; sendfile on; tcp_nopush on; keepalive_timeout 60; server_tokens off; limit_rate_after 3m; limit_rate 512k; tcp_nodelay on; client_header_buffer_size 256k; large_client_header_buffers 4 256k; #Define nginx proxy module proxy_http_version 1.1; 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; proxy_headers_hash_max_size 51200; proxy_headers_hash_bucket_size 6400; #需要在http字段,设置nginx cache proxy_temp_path /data/nginx/proxy_temp_path; proxy_cache_path /data/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:50m inactive=20m max_size=30g; # Define Gzip compression module gzip on; gzip_vary on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; # Define realserver pool upstream tomcat_server { ip_hash; server 192.168.1.10:5678 max_fails=0 weight=2; server 192.168.1.20:7890 max_fails=0 weight=1; } server { listen 80; server_name beyond.com www.beyond.com;
nginx缓存是否成功,可以在缓存目录中查看,如图所示:
二、静态数据访问本地,静态数据交给本地nginx处理。动态数据交给后端代理的tomcat服务器处理。
location ~ .*\.(png|gif|jpg|jpeg|bmp|swf)$ { root /data/node1/webapps/ROOT; #反向代理图片服务器,这里静态数据交给本地处理了。 expires 1d; #缓存一天 proxy_cache one; #引用定义的缓存模块名称 proxy_cache_valid 200 1h; #请求返回值为200的则缓存1小时 proxy_cache_valid 302 10m; #请求返回值为301 302的则缓存10分钟 proxy_cache_valid any 10s; #其他任何返回值缓存10秒 add_header X-Via $server_addr; #定义这个header名为X-Via 通过变量$server_addr明确说明从哪个服务器来响应的 server_addr add_header X-Cache-Status $upstream_cache_status;#明确说明是否命中 $upstream_cache_status为upstream模块 } 动态数据交给后端代理的tomcat,实现动静分离。当然做了CDN那就另说了,可以单独把静态数据解析到另外一个域名上去。 location ~ \.(do|jsp|php)$ { proxy_pass http://tomcat_server; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
本文出自 “岁月在流逝,光辉依然在” 博客,请务必保留此出处http://taokey.blog.51cto.com/4633273/1673314
原文地址:http://taokey.blog.51cto.com/4633273/1673314