使用nginx做缓存服务器

nginx做cache其实也比较简单。配置项非常少
[text]

#user nobody;
worker_processes 1;

error_log logs/error.log;
pid logs/nginx.pid;

events {
worker_connections 65535;
}

http {
include mime.types;
default_type application/octet-stream;
underscores_in_headers on;
log_format main ‘$http_orig_client_ip $remote_addr $host – $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"’;

access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
proxy_cache_path /home/admin/nginxcache/one levels=1:2 keys_zone=blogone:10m inactive=5m max_size=100m;
# proxy_cache_path /dev/shm/nginxcache/one levels=1:2 keys_zone=blogone:10m inactive=5m max_size=100m;
#5分钟不使用的会被删除,最大100M

server {
listen 80;
server_name localhost;
charset gbk;
access_log logs/cache.access.log main;
keepalive_requests 1000;
client_max_body_size 50m;
############gzip ##################
gzip_disable "msie6";
gzip_comp_level 7;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 1k;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text /javascript;
######## proxy ####################
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_intercept_errors on;
proxy_set_header Host $http_host;
proxy_set_header ORIG_CLIENT_IP $remote_addr;
proxy_read_timeout 5;
proxy_send_timeout 3;
proxy_connect_timeout 2;
##########cache#################
proxy_cache_methods GET HEAD;
proxy_cache_min_uses 2;#被访问次数大于2个才会缓存
proxy_cache_valid 302 5m;
proxy_cache_valid 200 20m;
proxy_cache_key "$scheme$host$request_uri";
proxy_cache blogone;#使用缓存空间
location / {

proxy_pass http://blog;
}
location ~* \.(gif|jpg|png|swf|flv)$ {

expires 10m; #图片缓存10分钟
proxy_pass http://blog;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upstream blog {
server blog.gnuers.org:80;
keepalive 1;

}
}
[/text]

如果是想把内容放内存,可以把缓存的路径设置到shm里面。

此条目发表在nginx分类目录。将固定链接加入收藏夹。

发表评论