您的位置:首页 > 运维架构 > Nginx

Nginx:设置Nginx作为缓存

2016-03-07 18:46 417 查看
一、设置Nginx作为缓存

1.1 完整的配置文件nginx.conf

#user  nobody;
worker_processes  1;

events {
worker_connections  1024;
}

http {
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log  logs/access.log  main;

#source site ip and port
upstream contactpool{
server 106.38.193.183:80;
}

#proxy cache info
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 /home/temp_dir;
proxy_cache_path /home/cache
levels=1:2
keys_zone=cache_one:50m
inactive=20m
max_size=30g;

server {
listen       80;
server_name  localhost;

location / {
#proxy cache info
index  index.html index.htm index.php;
proxy_cache cache_one;
proxy_cache_valid 200 302 1h;

proxy_cache_key $host$uri$is_args$args;
proxy_pass   http://contactpool; proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie"; # 不处理后端服务器返回的指定响应头

expires 30d;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

}


 1.2 测试

        curl www.guowenyan.cn/test.html

        用户第一次请求一个url,nginx会回源,将结果返回给用户的同时,把结果缓存;

        第二次再请求该url时,nginx直接将结果返回给用户,无需回源。 

        是否回源,我是通过在nginx上抓包看的。

参考资料:

        Nginx缓存功能的设置:http://blog.csdn.net/yybjroam05/article/details/21954457
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: