Dynamic Redirects
Ezra Zygmuntowicz
ezmobius at gmail.com
Wed Apr 7 23:30:59 MSD 2010
On Apr 7, 2010, at 12:09 PM, Harmer, Sean wrote:
> NGINX Team,
>
> Is there a way to capture the User-Agent from an HTTP Header, then query a database for that User-Agent, then based on the result of the database query, transparently route the request to an appropriate server?
>
> Thanks,
> Sean
Sean-
Here is one way to do what you want, you will need to use redis as the database to lookup your backend with. You will need to compile nginx with two additional modules:
http://wiki.nginx.org/NginxHttpRedis
http://www.grid.net.ru/nginx/eval.en.html
Once you have both of those module compiled into your nginx you can use a config file like this:
worker_processes 1;
user root root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
keepalive_timeout 65;
server {
listen 80;
server_name _;
access_log /var/log/nginx/redis.access.log main;
error_log /var/log/nginx/redis.error.log notice;
location / {
eval_escalate on;
eval $answer {
set $redis_key "$http_user_agent";
set $redis_db "0";
redis_pass 127.0.0.1:6379;
}
proxy_pass $answer;
}
}
}
Then in redis you will need to store key value pairs mapping the $http_user_agent to the server:port combo that should serve that user agent type:
redis.set "some-user-agent", "http://192.168.0.1:80"
Then when a request comes into the nginx server and has a $http_user_agent value of "some-user-agent", redis will return http://192.168.0.1:80 and then nginx will proxy_pass to the particular server.
Hope this helps point you in the right direction.
Cheers-
Ezra Zygmuntowicz
ez at engineyard.com
More information about the nginx
mailing list