Skip to content

Instantly share code, notes, and snippets.

@nikolaymatrosov
Created December 30, 2014 13:01
Show Gist options
  • Save nikolaymatrosov/f8bda2f66f848b3eec7d to your computer and use it in GitHub Desktop.
Save nikolaymatrosov/f8bda2f66f848b3eec7d to your computer and use it in GitHub Desktop.
Lua redirects based on backend response
server {
listen 7777;
location / {
root /tmp/www;
index index.html;
try_files $uri @lua;
}
location /api {
rewrite /api/(.*)$ $1 break;
proxy_pass http://localhost:7778;
}
location @lua {
default_type 'text/plain';
content_by_lua '
local res = ngx.location.capture("/api/"..ngx.var.uri)
if res.status == 200 then
ngx.redirect("http://google.com")
end
if res.status == 404 then
ngx.redirect("http://yandex.ru")
end
';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment