We have an NginX balancing proxy in front of our web application. Unfortunately there is not enough development resources to support outdated browsers, yet :( We want to redirect members of our web project to /outdated page by their browser user-agent data like this (https://www.in2circle.com/outdated). For some reason we don't want to load back-end with User-Agent content analysis logic. I've heard it's easy to do with NginX http://nginx.org/en/docs/http/ngx_http_map_module.html If somebody did so, can you help me with examples and explanations, please!
Asked
Active
Viewed 1.6k times
2 Answers
20
Next solution works pretty good and is exactly what I wanted:
map $http_user_agent $outdated {
default 0;
"~MSIE [1-9]\." 1;
"~Mozilla.*Firefox/[1-9]\." 1;
"~Mozilla.*Firefox/[0-2][0-9]\." 1;
"~Mozilla.*Firefox/3[0-1]\." 1;
"~Opera.*Version/[0-9]\." 1;
"~Opera.*Version/[0-1][0-9]\." 1;
"~Opera.*Version/2[0-1]\." 1;
"~AppleWebKit.*Version/[0-6]\..*Safari" 1;
"~Chrome/[0-9]\." 1;
"~Chrome/[0-2][0-9]\." 1;
"~Chrome/3[0-3]\." 1;
}
if ($outdated = 1){
rewrite ^ /outdated redirect;
}
Thanks everyone for Help/Answers.

Alexander Arutinyants
- 411
- 1
- 3
- 10
-
After struggling with nginx's module ngx_http_browser_module with the ancient_browser and modern_browser variables, I found this solution actually worked the best. The only problem I'm having now is that the if($outdated...) rewrite has to be put into a server { } block, whereas I'd like to make it global for all virtual servers running under nginx. – delatbabel Apr 16 '15 at 10:38
-
The map declaration itself can be defined in http {} block, it is if directive which must be used inside server or location context as well as location itself. So unfortunately I don't see the way to use rewrite for location outside the server. Despite that you can and actually shoud put the map part in the http section of your config (According to [the documentation](http://nginx.org/en/docs/http/ngx_http_map_module.html#map) at least, I did not test it). – Alexander Arutinyants Apr 16 '15 at 13:07
-
However I hope you can try to put the if statement to a separate file and include it in each servers config by using include directive. – Alexander Arutinyants Apr 16 '15 at 13:11
-
Yes it would certainly be possible to put the if statement in a separate file, although changing 3 lines of if statement into 1 line of include statement and adding a 3 line include file does not save much. – delatbabel Apr 20 '15 at 08:01
-
You are definitely right, @delatbabel. Alas, I have no idea yet how to do it better. – Alexander Arutinyants Apr 20 '15 at 08:10
-
Revisiting this in '19, the [`modern_browser/ancient_browser`](https://trac.nginx.org/nginx/browser/nginx/src/http/modules/ngx_http_browser_module.c) seems useless by comparison as there are very few/limited masks. This still appears to be the best option to have some granular control when you know the browser user agents you want to mark outdated. – kross May 08 '19 at 18:22
-
Here is an initial gist of what I came up with based on this map. A more updated conf based on `browserslist` output. I hope it helps someone else https://gist.github.com/rosskevin/5cfd78c73a10ca1989696350d76ea3c1 – kross May 08 '19 at 20:13
5
A simply test is sufficient:
location / {
if ($http_user_agent ~ "MSIE 6.0" ) {
set $browser-version outdated;
}
if ($http_user_agent ~ "MSIE 7.0" ) {
set $browser-version outdated;
}
if ($http_user_agent ~* "(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino") {
set $browser-version mobile;
}
if ( $uri ~ ^/(images|favicon\.ico) ) {
set $browser-version independent;
}
if ($browser-version = outdated) {
rewrite ^ /outdated/index.html break;
}
if ($browser-version = mobile) {
rewrite ^ /mobile/index.html break;
}
}

HBruijn
- 77,029
- 24
- 135
- 201
-
It's beautiful and really simple solution. But the problem is we must count brouser outbatet if it is: IE: older than 2 version (>= 11-2 for today) Safari: older than 2 versions Chrome: older than 5 version Firefox: older than 3 version Opera: older than 2 version Even if we write down all last versions of brousers, it is to hard to write so many IFs. – Alexander Arutinyants Jul 25 '14 at 14:41