3

I'm building web app using SSI to reduce server usage as much as possible (since it small VPS), one of the issue I face is serving different contents/style to mobile users I use custom detect-mobile.html header I include it on the rest of pages parts . but I was only able to use it directive <!--# if expr="$MOBILEDEVICE = Yess" --> on the requested html file only e.g index.html . I can't use it on included files footer.html or header.html SSI didn't recognize it .here is the code I use in detect-mobile.html .

<!--#if expr="$HTTP_USER_AGENT=/iPhone/" -->
  <!--#set var="MOBILEDEVICE" value="Yess" -->
<!--#elif expr="$HTTP_USER_AGENT=/Android/" -->
  <!--#set var="MOBILEDEVICE" value="Yess" -->
<!--#elif expr="$HTTP_USER_AGENT=/iPod/" -->
  <!--#set var="MOBILEDEVICE" value="Yess" -->
<!--#else -->
  <!--#set var="MOBILEDEVICE" value="Noo" -->
<!--#endif -->

I tried to add my own custom header from nginx

set     $ismobile Noo;  
if ($http_user_agent ~* "(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od|ad)|iris|kindle|lge |maemo|midp|mmp|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   $ismobile  Yess ;    }
proxy_set_header HTTP_CF_ISMOBILE $ismobile; #
add_header 'HTTP_ISMOBILE' $ismobile; 

but SSI can't read it <!--#echo var = "HTTP_ISMOBILE" -->

Beside allowed SSI variables [1] like HTTP_USER_AGENT , I want to pass sort of custom variable like authorize in the future , could this be possible in SSI .since cloud flare country directive is works ! how they do that !

<!--#echo var = "HTTP_CF_IPCOUNTRY" -->

As far I reach the only possible way for me now , SSI html file could read variable via custom QUERY_STRING redirect e.g rewrite ^ http://example.com$uri?m=1 permanent;

2 Answers 2

1

It looks like you are setting the header passed to the proxy as "HTTP_CF_ISMOBILE". Since the HTTP headers are accessed in SSI using the "HTTP_" prefix, you may need to access this as the variable "HTTP_HTTP_CF_ISMOBILE".

proxy_set_header HTTP_CF_ISMOBILE $ismobile;

Alternatively, you can provide a variable in your nginx without the HTTP prefix

proxy_set_header CF_ISMOBILE $ismobile;
2
  • Hi I already add it proxy_set_header HTTP_CF_ISMOBILE $ismobile; and change it to HTTP_ISMOBILE with same result the output is none , in the browser I can see the header value Noo but I don't know how SSI able to output HTTP_CF_IPCOUNTRY
    – Salem F
    May 10, 2019 at 22:03
  • OK I got it working looks like this due to nginx proxy not accepting underscore header BTW I'm using Cloudflare are proxy for my site .
    – Salem F
    May 12, 2019 at 2:48
1

OK after long searching I found out Nginx didn't accept underscore header when proxy to other server (since I'm using cloudflare proxy server) so I change my header to looks like this

add_header               'ISMOBILE'         $ismobile  ; 

when I echo it on SSI file I got correct value which is amazing for me

<!--#echo var = "ISMOBILE" -->  

this is complete code I use to detect mobile device using HTML SSI and nginx server only (no php Javascript etc)

set     $itismobile Noo;  
if ($http_user_agent ~* "(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od|ad)|iris|kindle|lge |maemo|midp|mmp|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   $itismobile  Yess ;    }

mobile UA code reference

Edit: I didn't even need to set header value at all , SSI can read nginx special variable directly .

<!--#echo var = "itismobile" -->  

print Noo on desktop and Yess on mobile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .