Created
October 24, 2012 21:13
-
-
Save benhaan/3948923 to your computer and use it in GitHub Desktop.
inteligent routing in nginx
This file contains hidden or 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
#user nobody; | |
worker_processes 1; | |
#pid logs/nginx.pid; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
include mime.types; | |
default_type application/octet-stream; | |
sendfile on; | |
keepalive_timeout 65; | |
upstream redis { | |
server localhost:6379; | |
keepalive 1024; | |
} | |
upstream app1 { | |
server localhost:5001; | |
} | |
upstream app2 { | |
server localhost:5002; | |
} | |
server { | |
listen 80; | |
server_name localhost; | |
#charset koi8-r; | |
#access_log logs/host.access.log main; | |
location /redis_check_for_v3 { | |
internal; | |
#is this domain in the Redis set v3:domain? | |
redis2_query sismember v3:domain $host; | |
redis2_connect_timeout 200ms; | |
redis2_send_timeout 200ms; | |
redis2_read_timeout 200ms; | |
redis2_pass redis; | |
error_page 500 501 502 503 504 505 @redis_error; | |
} | |
location @redis_error { | |
internal; | |
content_by_lua 'ngx.print("ignore_this_error");'; | |
} | |
location / { | |
set $root "/var/www/app1/public"; | |
set $backend "http://app1"; | |
# Any proxy configuration we need, and other Nginx config if required. | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host $http_host; | |
proxy_set_header X-Real-IP $remote_addr; | |
rewrite_by_lua ' | |
local host = ngx.var["host"]; | |
local result = ngx.location.capture("/redis_check_for_v3", { args = { host = host}}); | |
if result.body == ":1\\r\\n" then | |
ngx.log(ngx.NOTICE, "Detected v3:domain for host: ", host, ", will now route to app2"); | |
ngx.var.backend = "http://app2"; | |
ngx.var.root = "/var/www/app2/public"; | |
end | |
'; | |
root $root; | |
if (!-f $request_filename) { | |
proxy_pass $backend; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment