I've tons of images, css and javascript hosted at backend server IP 192.168.1.5 and 192.168.1.6. How do I make sure nginx reverse proxy load balancer always send all http_user_agent requests coming from "Mozilla" to server 192.168.1.5 and MSIE to server 192.168.1.6 only?
Nginx web server does support if conditional which can redirect and/or select configuration depending on variables. In this case you need to use the variable called $http_user_agent which can be used to retrieve information about the users browser, version and operating system. The syntax is as follows:
if ( condition ){ do_something } if ( $http_user_agent = "wget" ){ do_something } if ( $http_user_agent ~ MSIE ){ return 403; }
The if directive is used to checks the truth of a condition. If the condition evaluates to true, then the code indicated in the curly braces is carried out and the request is processed in accordance with the configuration within the following block. Configuration inside directive if is inherited from the previous level. You can use the name of variable or any string starting with "0" in condition. Also,
- The comparison of variable with the line with using the = and != operators;
- Pattern matching with regular expressions using the symbols ~* and ~:
- ~ is case-sensitive match
- ~* specifies a case-insensitive match (firefox matches FireFox)
- !~ and !~* mean the opposite, "doesn't match"
- checking for the existence of a file using the -f and !-f operators;
- checking existence of a directory using the -d and !-d operators;
- checking existence of a file, directory or symbolic link using the -e and !-e operators;
- checking whether a file is executable using the -x and !-x operators.
- Parts of the regular expressions can be in parentheses, whose value can then later be accessed in the $1 to $9 variables.
Examples
Edit /usr/local/nginx/conf/nginx.conf, enter:
# vi /usr/local/nginx/conf/nginx.conf
Setup upstream servers:
upstream myproxybackend { server 192.168.1.1; server 192.168.1.2; server 192.168.1.3; server 192.168.1.4; } upstream msiebackend { server 192.168.1.6; } upstream mozillabackend { server 192.168.1.5; }
Update config as follows:
server { access_log logs/access.log; error_log logs/error.log; index index.html; listen 202.54.1.5:80 default; root /usr/local/nginx/html; server_name example.com www.example.com 0.example.com; ## PROXY - Web location / { proxy_pass http://myproxybackend; if ($http_user_agent ~ MSIE ) { proxy_pass http://msiebackend; } if ($http_user_agent ~ Mozilla ) { proxy_pass http://mozillabackend; } proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; ... .. }
Finally restart / reload new changes:
# /usr/local/nginx/sbin/nginx -s reload
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop












{ 1 comment… read it below or add one }
Maybe it shout be the follow configuration?
## PROXY – Web
location / {
if ($http_user_agent ~ MSIE ) {
proxy_pass http://msiebackend;
}
if ($http_user_agent ~ Mozilla ) {
proxy_pass http://mozillabackend;
}
proxy_pass http://myproxybackend;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
…
..
}