1. create image_servers.conf in ..../nginx/conf
server{#you can access images by http://localhost:8000/images listen 8000; server_name localhost; location /images/{ root /share/; autoindex on; autoindex_exact_size off; autoindex_localtime on; } } server{#you can access images by http://localhost:9000/images listen 9000; server_name localhost; location /images/{ root /share/; autoindex on; autoindex_exact_size off; autoindex_localtime on; } }
2. create upstream_image_servers.conf in ..../nginx/conf
upstream image_backend{#weight server 127.0.0.1:8000 weight=10 max_fails=1 fail_timeout=5; server 127.0.0.1:9000 weight=10; #server 127.0.0.1:10000 backup; } upstream image_backend1{#ip_hash ip_hash; server 127.0.0.1:8000; server 127.0.0.1:9000; } #upstream image_backend2{#select server according to server responding # server 127.0.0.1:8000; # server 127.0.0.1:9000; # fair;#third party #} #upstream image_backend3{#url hash # server 127.0.0.1:8000; # server 127.0.0.1:9000; # hash $request_uri; # hash_method crc32; #}
3. create http_proxy_cache.conf in ..../nginx/conf
proxy_connect_timeout 10; proxy_read_timeout 180; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_temp_path /tmp/temp_dir; proxy_cache_path /tmp/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=3d max_size=10g;
4. include above created conf files in http block of ..../nginx/conf/nginx.conf
http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on;
include image_servers.conf;
include http_proxy_cache.conf;
include upstream_image_servers.conf;
}
5. server the static files, and use proxy_cache.
server { listen 80; server_name 10.197.60.137; location ~ .*\.(gif|jpg|png|css|js)(.*) { proxy_cache cache_one; proxy_cache_valid 200 302 24h; proxy_cache_valid 301 304 30d; proxy_cache_valid any 5m; expires 90d; add_header X-Cache HIT-Linux; proxy_cache_key $host$uri$is_args$args; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://image_backend; proxy_redirect off; }