nginx – Passing Nginx Custom Header or Variables to SSI

nginxserver-side-includes

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;

Best Answer

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;
Related Topic