`
ol_beta
  • 浏览: 293486 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

varnish+nginx实现内网附件缓存

阅读更多

本文是对文件内网缓存方案的实现。

 

 

  • varnish作为缓存服务,部署在内网192.168.0.220,varnish只能本机访问(nginx),内网用户不能直接访问varnish,需要通过nginx代理来访问。
  • nginx作为varnish的代理,如果将来有更大规模的缓存,可以做负载均衡。
  • HttpSecureLinkModule 对请求(超时,防盗)验证,每个跳转到内网的url(带token和超时时间)都要经过nginx的验证,而且这个url会在很短时间失效,这样防止了内网用户盗用链接。

 

下面是一个请求的流程图:


配置地址:https://gist.github.com/3797290

 

varnish配置:

 

backend default {
          .host = "hostname1.com";
         
}

acl access {
        "192.168.0.220";
        "localhost";
}

sub vcl_recv {
        unset req.http.Cookie;
        set req.grace = 3m;
        #set req.url = regsub(req.url,"^(.+)(&token=.+)$","\1:");
        if(req.url ~ "/varnish-ping"){
                error 200 "OK";
        }
        if(!client.ip ~ access){
                error 405 "Access Denied !";
        }
        
        if(req.url ~ ".*/attachment/.*"){
                set req.http.host = "file.hostname";
                set req.url = regsub(req.url,"/attachment/(.+)(&token=.+)$","/cache/attachment/\1");
                #return (lookup);
        }
        return (lookup);
}

sub vcl_fetch {
        if (beresp.status == 500) {
                error 404 "File not Found!";
        }
        if (beresp.http.Content-Length == "0" || beresp.http.Content-Length == "" ) {
                return (hit_for_pass);
        }

        set beresp.ttl = 90d;
        set beresp.grace = 5m;
        return (deliver);
}         
 

nginx配置:

user  root;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  192.168.0.220;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

                #varnish_host for get cache
                set $varnish_host       http://192.168.0.220:9350;

                # ping for check varnish health
                location ~ varnish-ping$ {
                        allow   all;
                        proxy_pass      $varnish_host;
                }

                # for contract
                location / {
                        allow   all;
                        root    html;
                        proxy_pass $varnish_host;
                        secure_link $arg_token,$arg_expire;
                        secure_link_md5 my_keys_string$uri$arg_expire;

                        if ($secure_link = "") {
                                return 403;
                        }

                        if ($secure_link = "0") {
                                return 404;
                        }

                        #if ( $args ~ ^(.+)(&token=.+)$ ) {
                        #       set $args $1;
                        #       set $varnish_host   http://192.168.0.220:9350;
                        #       rewrite ^(.+)$ $varnish_host$uri?$1 break;
                        #       #rewrite ^(.*)$ "http://www.meituan.com"$args break;
                        #}
                }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }


}
 

 

 

 

 

 

 

  • 大小: 38 KB
分享到:
评论

相关推荐

    Nginx教程从入门到精通到失业

    - **配置步骤**:详细介绍如何配置 Nginx 实现内网域名转发。 - **遇到的问题**:列出在配置过程中可能遇到的问题及解决方案。 #### 十七、Nginx + Keepalived + Proxy Cache 配置高可用 Nginx 群集和高速缓存 - *...

    Nginx教程从入门到精通

    在安全性方面,本教程提供了隐藏Nginx版本号的方法,以及如何使用CDN调度器HAProxy、Nginx和Varnish来提升网站的响应速度和服务可用性。 Nginx还可以用作反向代理,为内网域名转发提供服务。本教程会展示如何搭建...

    大型高并发web应用系统架构分析与设计

    常见的缓存技术包括Varnish、Nginx等。 **2.4 Web应用层** - **2.4.1 静态处理**:针对静态资源(如HTML页面、图片等),可以直接由Web服务器提供服务。 - **2.4.2 动态处理**:动态内容则需要由应用服务器处理,...

    LVS搭建手册及说明文档

    此外,还可以考虑使用缓存服务(如Varnish)、CDN网络等方式进一步优化性能。 总之,LVS是构建高可用、高性能网络服务的重要工具,通过合理规划和配置,可以实现灵活、可靠的分布式系统。在Suse和Redhat 5.4环境中...

    单点登录源码

    各个子系统前台thymeleaf模板,前端资源模块,使用nginx代理,实现动静分离。 > zheng-upms 本系统是基于RBAC授权和基于用户授权的细粒度权限控制通用平台,并提供单点登录、会话管理和日志管理。接入的系统可自由...

Global site tag (gtag.js) - Google Analytics