Skip to content

Instantly share code, notes, and snippets.

@muresan
Created November 11, 2015 11:47
Show Gist options
  • Save muresan/f4f1f47a65b4e8aafe5a to your computer and use it in GitHub Desktop.
Save muresan/f4f1f47a65b4e8aafe5a to your computer and use it in GitHub Desktop.
Redirect based on value of origin_host from memcache. Needs nginx-lua and nginx-memc modules.
server {
listen 80;
server_name example.com;
root /var/www/example.com;
set $origin_host 'origin.example.com';
access_log /var/log/nginx/example.com.access.log main;
location / {
access_by_lua '
local res = ngx.location.capture("/memcached", { args = { cmd = "get", key = "origin_host" } } )
if res.status == 200 then
ngx.var.origin_host = res.body
end
';
header_filter_by_lua '
ngx.status = 301
ngx.header["Location"] = "http://" .. ngx.var.origin_host .. ngx.var.uri;
';
}
location /memcached {
internal;
set $memc_cmd $arg_cmd;
set $memc_key $arg_key;
memc_pass 127.0.0.1:11211;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment