Created
November 11, 2015 11:47
-
-
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.
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 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