1
votes

I'm using Nginx to serve a Drupal site and with Boost.

So far it's been working fine, but recently we added a mobile version and i configured boost/drupal to cache the mobile version in another directory, however i'm triying to configure Nginx to check for the adecuate directory if the device is mobile, but i'm getting 404 for some pages, and others never ever hit the cached files.
I've a location @cache with something like this:

set $mobile_rewrite desktop;
if ($http_user_agent ~ (iPhone|Android) ) {
    set $mobile_rewrite mobile;
}
try_files /cache/normal/$mobile_rewrite/$host${uri}_$args.html /cache/$host${uri}_$args.html @drupal;

When hitting the frontpage with a mobile user agent, i get served the mobile version, but every time it recreates it (so it goes to the @drupal location), and if i try to navigate further in the site (with mobile user agent) i get a 404 error

Any ideas?

I solved it using custom error and different locations.

1
@Deepak didn't know about this!! i accepted the answersdoterobcn

1 Answers

0
votes

I finally got around this, to do so, i changed the approach, instead of trying to change the try_files directive using a variable, i did this:

Setup a "map" to check the user agent and determine if mobile:

#Inside my http block
map $http_user_agent $is_desktop {
  default 0;
  ~*linux.*android|windows\s+(?:ce|phone) 0; # exceptions to the rule
  ~*spider|crawl|slurp|bot 1; # bots
  ~*windows|linux|os\s+x\s*[\d\._]+|solaris|bsd 1; # OSes
}
map $is_desktop $mobile_device {
  1 0;
  0 1;
}

Then in my server block, on the default / location:

error_page 418 = @mobile; #defined a new error page pointing to a @location  
    if ($mobile_device) { #checking for above variable/map if true, goes to @location defined in error
           return 418;
    }

This new location is exactly the same @cache i had before, but pointing to another directory.
Hope it helps someone else!