11月 09

用nginx的cache 对本地静态文件做缓存

需要对静态文件做缓存,但是静态文件也是在本地,由同一个nginx来解析。
使用if不能嵌套,所以只能使用本地的IP跳转一下,我的cache使用的nginx命名cache_one配置如下。如果本地跳转有问题可以再hosts表中修改一下指向。

   #######################pic
   upstream  local_img {
            server localhost:81;
       }
    server{
        listen       81;
        server_name 127.0.0.1;
        location / {
                root /image/;
                client_max_body_size   10m;
                access_log off;
                autoindex off;
                }
    }
   server {
        listen       80;
        server_name img.test.com;
        proxy_cache cache_one;
        location / {
                proxy_redirect off;
                proxy_cache_valid 200 304 12h;
                proxy_cache_valid 301 302 1m;
                proxy_cache_key $host$uri$is_args$args;
                add_header X-Cache $upstream_cache_status;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_pass http://local_img;
                access_log off;
                }
    }