user www www; worker_processes 8; error_log /data1/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 51200; events { use epoll; worker_connections 51200; } 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; sendfile on; #tcp_nopush on; keepalive_timeout 30; tcp_nodelay on; #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区 proxy_temp_path /data0/proxy_temp_path; #设置Web缓存区名称为cache_one,内存缓存空间大小为500MB,自动清除超过1天没有被访问的缓存数据,硬盘缓存空间大小为30GB。 proxy_cache_path /data0/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; upstream my_server_pool { server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.4:80 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_name my.domain.com; location / { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://my_server_pool; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { #使用Web缓存区cache_one proxy_cache cache_one; #对不同HTTP状态码缓存设置不同的缓存时间 proxy_cache_valid 200 304 12h; proxy_cache_valid 301 302 1m; proxy_cache_valid any 1m; #设置Web缓存的Key值,Nginx根据Key值md5哈希存储缓存,这里根据“域名、URI、参数”组合成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://my_server_pool; } #用于清除缓存,假设一个URL为http:// my.domain.com/test.gif,通过访问http://my.domain.com/purge/test.gif就可以清除该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; } access_log off; } }