Just 做 IT

求知若饥 虚心若愚 안년하세요 (•‾̑⌣‾̑•)

nginx利用proxy_cache来缓存文件

2 years ago 0


user www-data;
worker_processes auto;
error_log  /var/log/nginx/error.log  crit;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
 use epoll;
 worker_connections 65535;
}
http
{
 include       mime.types;
 default_type  application/octet-stream;
 #charset  gb2312;
 server_names_hash_bucket_size 128;
 client_header_buffer_size 32k;
 large_client_header_buffers 4 32k;
 client_max_body_size 8m;
 sendfile on;
 tcp_nopush     on;
 keepalive_timeout 60;

 tcp_nodelay on;

 fastcgi_connect_timeout 300;
 fastcgi_send_timeout 300;
 fastcgi_read_timeout 300;
 fastcgi_buffer_size 64k;
 fastcgi_buffers 4 64k;
 fastcgi_busy_buffers_size 128k;
 fastcgi_temp_file_write_size 128k;
//============
 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;
 proxy_temp_path   /usr/local/nginx/proxy_temp;
/*levels设置目录层次
keys_zone设置缓存名字和共享内存大小
inactive在指定时间内没人访问则被删除在这里是1天
max_size最大缓存空间*/
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;
//============等号中间要加的,关键只要加上proxy_cache_path

 gzip 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;
 gzip_vary on;

 upstream myselfxtajmd {
 server 127.0.0.1:10002;
 server 127.0.0.1:10001 weight=5;
 }

 server
 {
 listen       10000;
 server_name  localhost;
 index index.html index.htm index.php;
 log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
 '$status $body_bytes_sent "$http_referer" '
 '"$http_user_agent" $http_x_forwarded_for';
 access_log  /var/log/test.log  access;

 location /
 {
 proxy_cache content; //根keys_zone后的内容对应
 proxy_cache_valid  200 304 301 302 10d;   //哪些状态缓存多长时间
 proxy_cache_valid  any 1d;    //其他的缓存多长时间
 proxy_cache_key $host$uri$is_args$args;   //通过key来hash,定义KEY的值

 proxy_pass http://myselfxtajmd;
 proxy_redirect                      off;
 proxy_set_header   Host             $host;
 proxy_set_header   X-Real-IP        $remote_addr;
 proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
 }

//动态的放过
 location ~ .*\.(php|jsp|cgi)?$
 {
 proxy_set_header Host  $host;
 proxy_set_header X-Forwarded-For  $remote_addr;
 proxy_pass http://myselfxtajmd;
 }
 }
}

正向代理加缓存


proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 1024k;
proxy_buffers 10 1024k;
proxy_busy_buffers_size 1024k;
proxy_temp_file_write_size 4096k;
proxy_temp_path /var/www/nginx/temp;
proxy_cache_path /var/www/nginx/cache keys_zone=one:1024m loader_threshold=300 loader_files=200 inactive=1d max_size=10g;
server {
	resolver 8.8.4.4 8.8.8.8 114.114.114.114;
	listen 80;
	server_name 106.184.3.212 solo-media.dfs.solo-launcher.com;
	access_log off;
	location / {
		proxy_pass http://origin.storage.dfs.solo-launcher.com$request_uri;
		#proxy_pass http://orgin_solo_media;
         	#proxy_set_header Host $http_host;
		expires 30d; 
		proxy_cache one;
		proxy_cache_key $uri$is_args$args;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
        	proxy_max_temp_file_size 0;
        	proxy_cache_valid 200 302 304 10d;
        	proxy_cache_valid any 0s;
	}
}

Write a Comment