public
Last active

nginx.conf

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
server {
listen 9090;
server_name cache.beta2.asiaclassified.com;
 
root /var/www/current/public;
 
set $rooty /var/www/currentpublic;
large_client_header_buffers 4 16k;
 
location /content {
internal;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $rooty/index.php;
include fastcgi_params;
}
 
location /content_esi {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $rooty/index.php;
include fastcgi_params;
}
 
location = /redis-get {
internal;
# set_unescape_uri is provided by ngx_set_misc
# set_unescape_uri $query $arg_query;
set $key $arg_key;
redis2_query get $key;
redis2_pass 127.0.0.1:6379;
}
 
location = /redis-set {
internal;
lua_need_request_body on;
 
client_max_body_size 100k;
client_body_in_single_buffer on;
 
# set_unescape_uri is provided by ngx_set_misc
# set_unescape_uri $query $arg_query;
set $key $arg_key;
redis2_query set $key $request_body;
redis2_pass 127.0.0.1:6379;
}
 
location = /redis-expire {
internal;
set $key $arg_key;
redis2_query expire $key 60;
redis2_pass 127.0.0.1:6379;
}
 
location / {
default_type text/html;
set $key $request_uri;
set_sha1 $key;
 
proxy_pass_request_headers off;
keepalive_timeout 0;
 
content_by_lua '
function literalize(str)
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", function(c) return "%" .. c end)
end
 
local key = ngx.var.key;
local json = require("yajl");
local res;
 
-- find content in redis for this key
local parser = require("redis.parser")
local ctn;
local content;
local process_esi = false;
 
if res == nil then
content = ngx.location.capture("/content"..ngx.var.uri, {args = ngx.var.args});
if content.status == 302 or content.status == 301 then
if content.header["Location"]~= ngx.var.uri then
ngx.redirect(content.header["Location"], content.status);
end
end
if content.header["Esi-Enabled"] ~= nil then
process_esi = true;
end
if content.header["Esi-Cache-Enabled"] ~= nil then
ngx.location.capture("/redis-set?key="..key, { method = ngx.HTTP_POST, body = content.body });
ngx.location.capture("/redis-expire?key="..key);
end;
ctn = content.body
for k,hed in pairs(content.header) do
ngx.header[k] = hed;
end
else
ctn = res;
process_esi = true;
end
 
if process_esi then
local mtc = string.gmatch(ctn, "(<esi:include src=\'([^\']*)\'></esi:include>)")
 
for esi in mtc
do
local urls = string.gmatch(esi, "src=\'([^\']*)\'")
for url in urls
do
local content2 = ngx.location.capture("/content_esi"..url);
esi = string.gsub(esi, "%?", "%%?");
tmp = string.gsub(ctn, esi, literalize(content2.body));
if tmp ~= nil then
ctn = tmp;
else
end
end
end
end
 
-- if not find just forward it to the content
 
ngx.print(ctn);
';
 
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.