# Tricky Caching reverse Proxy - different User agents to be watched

**URL:** <https://www.ruby-forum.com/t/tricky-caching-reverse-proxy-different-user-agents-to-be-watched/188206>\
**Category:** NGINX\
**Created:** [May 7, 2010, 4:19pm UTC](https://www.ruby-forum.com/t/tricky-caching-reverse-proxy-different-user-agents-to-be-watched/188206 "2010-05-07T16:19:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![musa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@musa](https://www.ruby-forum.com/u/musa)\
**Post date:** [May 7, 2010, 4:19pm UTC](https://www.ruby-forum.com/t/tricky-caching-reverse-proxy-different-user-agents-to-be-watched/188206/1 "2010-05-07T16:19:04Z")

</div>

Hello all

i hope somebody can help. didnt find any usefull post nor webside which  
could help.

short backend story: there is a web application which generates sites,  
different sites for different clients (ipod, nokia phones, normal  
browser etc). its an old application running on windows, with really  
poor performance.

anyway, i had the idea to use the nginx in frontend as reverse proxy  
with caching, to speed up the site… and it works perfect.(caching time  
only 10min, but enough to take load off the backend). the problem is:

- when the first visitor is a normal browser, the generated page will be  
the version for browser → looks bad on the iphone/nokia phone
- when the first visitor is iphone… it looks bad on everything else.

so … is it possible… to cache each version of the response based on  
the http-agent? and to deliver when the agents xyz matchs the cached  
version?

or i am asking for to much?

the websites are really small, so if a iphone has 10 different  
User-Agent variables (language etc in it) it doesnt matter, it can easy  
save the side 10x.

my english isnt best… if its not clear what i mean… please say so…  
will try it again 🙂

any help/tricks/hints would help

greets  
Musa

Posted at Nginx Forum:

> **[Tricky Caching reverse Proxy - different User agents to be watched](https://forum.nginx.org/read.php?2,83848,83848#msg-83848)**
>
> Hello all i hope somebody can help. didnt find any usefull post nor webside which could help. short backend story: there is a web application which generates sites, different sites for different clients (ipod, nokia phones, normal browser etc). its...

---

<div class="post-metadata">

**Author:** ![musa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@musa](https://www.ruby-forum.com/u/musa)\
**Post date:** [May 7, 2010, 11:19pm UTC](https://www.ruby-forum.com/t/tricky-caching-reverse-proxy-different-user-agents-to-be-watched/188206/2 "2010-05-07T23:19:58Z")

</div>

On Fri, May 07, 2010 at 10:18:00AM -0400, musa wrote:

> so … is it possible.. to cache each version of the response based on the http-agent? and to deliver when the agents xyz matchs the cached version?
> 
> or i am asking for to much?
> 
> the websites are really small, so if a iphone has 10 different User-Agent variables (language etc in it) it doesnt matter, it can easy save the side 10x.
> 
> my english isnt best.. if its not clear what i mean.. please say so.. will try it again 🙂
> 
> any help/tricks/hints would help

You may set  
proxy\_cache\_key “$scheme$proxy\_host$uri$is\_args$args  
$http\_user\_agent”;  
but it will cache different responses for every browser versions.  
The solution may be is to write a module that maps some configrated  
regexes  
to the reduced value of a variable:

```
 iPhone|iPod iphone
 Nokia nokia
 MSIE normal
 Gecko normal

```

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![musa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@musa](https://www.ruby-forum.com/u/musa)\
**Post date:** [May 10, 2010, 9:41am UTC](https://www.ruby-forum.com/t/tricky-caching-reverse-proxy-different-user-agents-to-be-watched/188206/3 "2010-05-10T09:41:40Z")

</div>

Hey Igor

thnx a lot for your answer.  
it works really well. thank you.

Well, in this case (realy small page, short caching time) it doesnt  
matter when it caches for each user-agent.

the idea with the module is nice, but my coding skills are not really  
useable 🙂

Greets  
Musa

Posted at Nginx Forum:

> **[Re: Tricky Caching reverse Proxy - different User agents to be watched](https://forum.nginx.org/read.php?2,83848,84687#msg-84687)**
>
> Hello all i hope somebody can help. didnt find any usefull post nor webside which could help. short backend story: there is a web application which generates sites, different sites for different clients (ipod, nokia phones, normal browser etc). its...

---

<div class="post-metadata">

**Author:** ![musa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@musa](https://www.ruby-forum.com/u/musa)\
**Post date:** [May 10, 2010, 3:11pm UTC](https://www.ruby-forum.com/t/tricky-caching-reverse-proxy-different-user-agents-to-be-watched/188206/4 "2010-05-10T15:11:17Z")

</div>

On Mon, May 10, 2010 at 2:41 AM, musa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Well, in this case (realy small page, short caching time) it doesnt matter when it caches for each user-agent.

The user-agent string contains a lot more than just the browser  
version, unfortunately. It can also includes the versions of various  
browser plug-ins, add-ons, etc., which means you are caching a  
different version of each page for IE8 users with one version of .NET  
or another.

For example, here’s a full user-agent string pulled from our logs:  
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0;  
SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729;  
Media Center PC 6.0; InfoPath.2)

To eliminate unnecessary requests to the back-end as well as  
duplicated cache entries, it might be best to use the nginx map module  
to “normalize” the User-Agent to just a few values:

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

For example (syntax not tested, just a guess based on documentation):

map $http\_user\_agent $shortua {  
default anybrowser;  
_iPhone_ iPhone;  
_Blackberry_ bb;  
_IEMobile_ winmoble;  
“_Windows Phone_” winmobile;  
}

proxy\_cache\_key “$scheme$proxy\_host$uri$is\_args$args $shortua”;

–  
RPM
