|
| 1 | +include "devicedetect.vcl"; |
| 2 | +import std; |
| 3 | + |
| 4 | +# set default backend if no server cluster specified |
| 5 | + |
| 6 | +backend MainPHP { |
| 7 | + .host = ""; |
| 8 | + .port = "8080"; |
| 9 | + .first_byte_timeout = 60s; |
| 10 | + |
| 11 | +} |
| 12 | + |
| 13 | +backend HHVM { |
| 14 | + .host = ""; |
| 15 | + .port = "80"; |
| 16 | + .first_byte_timeout = 60s; |
| 17 | + .probe = { |
| 18 | + .url = "/Mainpage"; |
| 19 | + .timeout = 5s; |
| 20 | + .interval = 10s; |
| 21 | + .window = 3; |
| 22 | + .threshold = 2; |
| 23 | + } |
| 24 | + |
| 25 | +} |
| 26 | + |
| 27 | +director MoegirlphpGroup client { |
| 28 | + { |
| 29 | + .backend = MainPHP; |
| 30 | + .weight = 1; |
| 31 | + } |
| 32 | + { |
| 33 | + .backend = HHVM; |
| 34 | + .weight = 999; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +# access control list for "purge": open to only localhost and other local nodes |
| 40 | +acl purge { |
| 41 | + "localhost"; |
| 42 | + ""; |
| 43 | + ""; |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +# vcl_recv is called whenever a request is received |
| 49 | +sub vcl_recv { |
| 50 | + |
| 51 | + |
| 52 | + # Serve objects up to 2 minutes past their expiry if the backend |
| 53 | + # is slow to respond. |
| 54 | + set req.grace = 120s; |
| 55 | + |
| 56 | + if (req.restarts == 0) { |
| 57 | + if (req.http.x-forwarded-for) { |
| 58 | + set req.http.X-Forwarded-For = req.http.X-Forwarded-For; |
| 59 | + } else { |
| 60 | + set req.http.X-Forwarded-For = client.ip; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + #手机跳转判断,先判定再重新bits.moegirl.org,顺序不能反 |
| 65 | + if (req.http.host == "zh.moegirl.org"){ |
| 66 | + if(req.http.Cookie !~ "direct_mobile") { |
| 67 | + call devicedetect; |
| 68 | + if (req.http.X-UA-Device ~ "^mobile" || req.http.X-UA-device ~ "^tablet") { |
| 69 | + error 750 "Moved Temporarily"; |
| 70 | + } |
| 71 | + }else if(req.http.Cookie ~ "direct_mobile=1" && req.url !~ "no_direct$") { |
| 72 | + error 751 "Moved Temporarily"; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + #判断转发给哪台机子 |
| 77 | + |
| 78 | + |
| 79 | + if (! (req.http.host ~ "zh\.moegirl\.org" || req.http.host ~ "bits\.moegirl\.org") ){ #非zh.moegirl.org bits.moegirl.org的请求丢回旧机处理 |
| 80 | + set req.backend = MainPHP; |
| 81 | + }else{ |
| 82 | + set req.backend = MoegirlphpGroup; #如果请求的是一般页面浏览 |
| 83 | + |
| 84 | + # 健康时HHVM,挂了回源MainPHP |
| 85 | + # if( req.backend.healthy ) { |
| 86 | + # set req.backend = HHVM; |
| 87 | + # } else { |
| 88 | + # set req.backend = MainPHP; |
| 89 | + # } |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + #把bits.moegirl.org/XX/load.php 重定向到 XX.moegirl.org/load.php |
| 94 | + if (req.http.host ~ "bits\.moegirl\.org" && req.url~ "^/\w+/load\.php") { |
| 95 | + set req.http.host = regsub(req.url, "^/(\w+)/.*", "\1") + ".moegirl.org"; |
| 96 | + set req.url = regsub(req.url, "^/(\w+)/", "/"); |
| 97 | + } |
| 98 | + |
| 99 | + # This uses the ACL action called "purge". Basically if a request to |
| 100 | + # PURGE the cache comes from anywhere other than localhost, ignore it. |
| 101 | + if (req.request == "PURGE") { |
| 102 | + if (!client.ip ~ purge) { |
| 103 | + error 405 "Not allowed."; |
| 104 | + } |
| 105 | + return(lookup); |
| 106 | + } |
| 107 | + |
| 108 | + # Pass any requests that Varnish does not understand straight to the backend. |
| 109 | + if (req.request != "GET" && req.request != "HEAD" && |
| 110 | + req.request != "PUT" && req.request != "POST" && |
| 111 | + req.request != "TRACE" && req.request != "OPTIONS" && |
| 112 | + req.request != "DELETE") |
| 113 | + {return(pipe);} /* Non-RFC2616 or CONNECT which is weird. */ |
| 114 | + |
| 115 | + # Pass anything other than GET and HEAD directly. |
| 116 | + if (req.request != "GET" && req.request != "HEAD") |
| 117 | + {return(pass);} /* We only deal with GET and HEAD by default */ |
| 118 | + |
| 119 | + |
| 120 | + #pass any request with "special:" |
| 121 | + if (req.url ~ "Special:" || req.url ~ "特別:") { |
| 122 | + return(pass); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + if (req.http.Cookie ~ "UserID") { |
| 127 | + if (!req.url ~ "^/load\.php") { |
| 128 | + return (pass); |
| 129 | + } |
| 130 | + } else { |
| 131 | + unset req.http.Cookie; |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + # Pass any requests with the "If-None-Match" header directly. |
| 136 | + if (req.http.If-None-Match) |
| 137 | + {return(pass);} |
| 138 | + |
| 139 | + |
| 140 | + # Force lookup if the request is a no-cache request from the client. |
| 141 | + if (req.http.Cache-Control ~ "no-cache") |
| 142 | + {ban_url(req.url);} |
| 143 | + |
| 144 | + # normalize Accept-Encoding to reduce vary |
| 145 | + if (req.http.Accept-Encoding) { |
| 146 | + if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") { |
| 147 | + # No point in compressing these |
| 148 | + remove req.http.Accept-Encoding; |
| 149 | + ##too large for our poor RAM |
| 150 | + #return(pass); |
| 151 | + } elsif (req.http.Accept-Encoding ~ "gzip") { |
| 152 | + set req.http.Accept-Encoding = "gzip"; |
| 153 | + } elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") { |
| 154 | + set req.http.Accept-Encoding = "deflate"; |
| 155 | + } else { |
| 156 | + # unkown algorithm |
| 157 | + remove req.http.Accept-Encoding; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return(lookup); |
| 162 | +} |
| 163 | + |
| 164 | +sub vcl_pipe { |
| 165 | + # Note that only the first request to the backend will have |
| 166 | + # X-Forwarded-For set. If you use X-Forwarded-For and want to |
| 167 | + # have it set for all requests, make sure to have: |
| 168 | + # set req.http.connection = "close"; |
| 169 | + |
| 170 | + # This is otherwise not necessary if you do not do any request rewriting. |
| 171 | + set req.http.connection = "close"; |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +sub vcl_hash { |
| 176 | + hash_data(req.url); |
| 177 | + if (req.http.host) { |
| 178 | + hash_data(req.http.host); |
| 179 | + } else { |
| 180 | + hash_data(server.ip); |
| 181 | + } |
| 182 | + return (hash); |
| 183 | +} |
| 184 | + |
| 185 | +# Called if the cache has a copy of the page. |
| 186 | +sub vcl_hit { |
| 187 | + if (req.request == "PURGE") |
| 188 | + {ban_url(req.url); |
| 189 | + error 200 "Purged";} |
| 190 | + |
| 191 | + if (!obj.ttl > 0s) |
| 192 | + {return(pass);} |
| 193 | +} |
| 194 | + |
| 195 | +# Called if the cache does not have a copy of the page. |
| 196 | +sub vcl_miss { |
| 197 | + if (req.request == "PURGE") |
| 198 | + {error 200 "Not in cache";} |
| 199 | +} |
| 200 | + |
| 201 | +# Called after a document has been successfully retrieved from the backend. |
| 202 | +sub vcl_fetch { |
| 203 | + set beresp.http.X-Backend = beresp.backend.name; |
| 204 | + |
| 205 | + |
| 206 | +# 让Vary 只包含 Accept-Encoding 和 Cookie, 防止产生过多的cache object |
| 207 | + if (beresp.http.Vary) { |
| 208 | + if (beresp.http.Vary ~ "Accept-Encoding") { |
| 209 | + set beresp.http.tempVary = "Accept-Encoding"; |
| 210 | + } |
| 211 | + if (beresp.http.Vary ~ "Cookie") { |
| 212 | + if (beresp.http.tempVary) { |
| 213 | + set beresp.http.tempVary = beresp.http.tempVary + ",Cookie"; |
| 214 | + } else { |
| 215 | + set beresp.http.tempVary = "Cookie"; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + if (beresp.http.tempVary) { |
| 220 | + set beresp.http.Vary = beresp.http.tempVary; |
| 221 | + remove beresp.http.tempVary; |
| 222 | + } else { |
| 223 | + remove beresp.http.Vary; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + |
| 228 | + # set minimum timeouts to auto-discard stored objects |
| 229 | +# set beresp.prefetch = -30s; |
| 230 | + set beresp.grace = 120s; |
| 231 | + |
| 232 | + if (beresp.ttl < 48h) { |
| 233 | + set beresp.ttl = 48h;} |
| 234 | + |
| 235 | + if (!beresp.ttl > 0s) |
| 236 | + {return(hit_for_pass);} |
| 237 | + |
| 238 | + if ( ! beresp.http.Set-Cookie ) { |
| 239 | + set beresp.ttl = 1h; |
| 240 | + return (deliver);} |
| 241 | + |
| 242 | +# if (beresp.http.Cache-Control ~ "(private|no-cache|no-store)") |
| 243 | +# {return(hit_for_pass);} |
| 244 | + |
| 245 | + if (req.http.Authorization && !beresp.http.Cache-Control ~ "public") |
| 246 | + {return(hit_for_pass);} |
| 247 | + |
| 248 | + } |
| 249 | + |
| 250 | + |
| 251 | +sub vcl_deliver { |
| 252 | + |
| 253 | + if (obj.hits > 0) { |
| 254 | + set resp.http.X-Cache = "HIT"; |
| 255 | + } else { |
| 256 | + set resp.http.X-Cache = "MISS"; |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +sub vcl_error { |
| 261 | + if(obj.status == 750) { |
| 262 | + set obj.http.Content-Type = "text/html; charset=utf-8"; |
| 263 | + synthetic {" |
| 264 | + <html> |
| 265 | + <head> |
| 266 | + <script language="javascript"> |
| 267 | + <!-- |
| 268 | + function confirmation() { |
| 269 | + var d = new Date(); |
| 270 | + d.setTime(d.getTime()+(60*60*1000)); |
| 271 | + var expires = "expires="+d.toGMTString(); |
| 272 | + var msg_text = unescape("%u70B9%u51FB%22%u786E%u5B9A%22%u8DF3%u8F6C%u5230%u624B%u673A%u7248%u9875%u9762%uFF0C%u70B9%u51FB%22%u53D6%u6D88%22%u7EE7%u7EED%u8BBF%u95EE"); |
| 273 | + //var answer = confirm("Press \"OK\" to be redirected to the mobile page or \"Cancel\" to continue."); |
| 274 | + var answer = confirm(msg_text); |
| 275 | + if (answer) { |
| 276 | + document.cookie = "direct_mobile=1; " + expires; |
| 277 | + window.location = "http://m.moegirl.org"} + req.url + {""; |
| 278 | + } |
| 279 | + else { |
| 280 | + document.cookie = "direct_mobile=0; " + expires; |
| 281 | + window.location = "http://zh.moegirl.org"} + req.url + {""; |
| 282 | + } |
| 283 | + } //--> |
| 284 | + </script> |
| 285 | + </head> |
| 286 | + <body onload="confirmation();"> |
| 287 | + <p></p> |
| 288 | + </body> |
| 289 | + </html> |
| 290 | + "}; |
| 291 | + set obj.status = 200; |
| 292 | + return(deliver); |
| 293 | + } else if(obj.status == 751) { |
| 294 | + set obj.http.Location = "http://m.moegirl.org" + req.url; |
| 295 | + set obj.status = 302; |
| 296 | + return(deliver); |
| 297 | + } |
| 298 | + |
| 299 | + # For 500 error 500错误用设置 |
| 300 | +# if (obj.status >= 500 && obj.status <= 505) { |
| 301 | +# synthetic(std.fileread("/etc/varnish/50X.html")); |
| 302 | +# return(deliver); |
| 303 | +# } |
| 304 | + return (deliver); |
| 305 | +} |
0 commit comments