Skip to content

Instantly share code, notes, and snippets.

@ninnemana
Created November 24, 2015 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninnemana/7d1136ee4d80e9791067 to your computer and use it in GitHub Desktop.
Save ninnemana/7d1136ee4d80e9791067 to your computer and use it in GitHub Desktop.
server {
listen 80;
server_name _;
set $site_root /data/www/$host;
set $log_root $site_root/logs;
location / {
root $site_root;
set $backend "";
rewrite_by_lua '
-- load global route cache into current request scope
-- by default vars are not shared between requests
local routes = _G.routes
if routes == nil then
routes = {}
ngx.log(ngx.ALERT, "Route cache is empty.")
end
local route = routes[ngx.var.http_host]
if route == nil then
local redis = require "redis"
local client = redis.connect("localhost", 6379)
route = client:get(ngx.var.host)
end
-- fallback to redis for lookups
if route ~= nil then
ngx.var.upstream = route
routes[ngx.var.http_host] = route
_G.routes = routes
else
ngx.exit(ngx.HTTP_NOT_FOUND)
end
';
proxy_buffering off;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://$backend;
set $log_root "/var/log/nginx";
}
error_log $log_root/error.log info;
access_log $log_root/access.log main;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment