生活随笔
收集整理的這篇文章主要介紹了
nginx利用proxy_cache来缓存文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
proxy_cache是nginx自帶的內置緩存模塊,配置一下就可以用了,看下面的配置
vi /usr/local/nginx/conf/nginx.conf
查看復制打印?
user??zhangy?users;?? worker_processes?10;?? error_log??/var/vlogs/nginx_error.log??crit;?? pid????????/var/vlogs/nginx.pid;?? #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;?? ? ? ? ?? proxy_cache_path?/usr/local/nginx/proxy_cache?levels=1:2?keys_zone=content:20m?inactive=1d?max_size=100m;?? ?? ?? ?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;??? ?proxy_cache_valid??200?304?301?302?10d;????? ?proxy_cache_valid??any?1d;?????? ?proxy_cache_key?$host$uri$is_args$args;????? ?? ?proxy_pass?http:?? ?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:?? ?}?? ?}?? }??
上面只是配置的部分內容,反向代理的那部分沒有放進去了,參考linux下nginx反向代理,實現負載均衡,當我們瀏覽http://localhost:10000/222.jpg時,在代理端就把圖片緩存了,不用到代理終端去緩存了,可以節省資源。緩存的內容放在你設置的proxy_cache_path路徑下面,看下圖
nginx proxy_cache
第一層目錄只有一個字符,是由levels=1:2設置,總共二層目錄,子目錄名字由二個字符組成。突然發現,我在我的系統里面,配置過varnish,squid,apache和mod_cache,nginx和proxy_cache,他們都是可以對文件進行緩存,不知道他們哪一個對文件的緩存效果最好呢?有空比較一下,對了這4種緩存,這個博客里面都有的,有興趣的朋友,可以看一下
總結
以上是生活随笔為你收集整理的nginx利用proxy_cache来缓存文件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。