Created
August 15, 2011 03:50
-
-
Save stunti/1145681 to your computer and use it in GitHub Desktop.
nginx.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment