Condition pagespeed based on upstream response headers

46 views
Skip to first unread message

Ash Tyndall

unread,
Apr 18, 2017, 3:15:37 AM4/18/17
to ngx-pagespeed-discuss
Hi there,

For my purposes, I only want to run pagespeed filters on "public" pages 
served through my application, as these are the pages I are concerned with 
the performance of / I want to avoid any potential issues with more dynamic
pages being rewritten.

Right now I are able to accomplish this with Apache and mod_pagespeed with 
the following configuration within pagespeed.conf;

    FilterDeclare pagespeed CONTENT_SET
    FilterProvider pagespeed MOD_PAGESPEED_OUTPUT_FILTER \
      "%{resp:X-Pagespeed-Enable} == 'true'"
    FilterChain pagespeed

This config only enables pagespeed  when the response from the 
application contains the header 'X-Pagespeed-Enable: true', which it emits 
when relevant.

However, I am struggling to do this with nginx. I have a test server setup 
with nginx proxying our application through a Unix socket with a configuration 
like this (simplified);

    upstream app {
      server unix:/foo.sock fail_timeout=0;
    }

    server {
      listen 80;
      server_name localhost example.com;
      root /app/public;
      try_files $uri/index.html $uri @app;

      # Pagespeed
      pagespeed on;
      pagespeed FileCachePath /var/ngx_pagespeed_cache;

      location @app {
        proxy_pass http://app;

        # Both X-Debug-1 and X-Debug-2 successfully contain the 
        # X-Pagespeed-Enable header returned by the server
        add_header "X-Debug-1" $upstream_http_x_pagespeed_enable;
        add_header "X-Debug-2" $sent_http_x_pagespeed_enable;
      }

      # Ensure requests for pagespeed optimized resources go to the pagespeed 
      # handler, and no extraneous headers get set.
      location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
        add_header "" "";
      }
      location ~ "^/pagespeed_static/" { }
      location ~ "^/ngx_pagespeed_beacon$" { }
    }

However, I cannot figure out how to make pagespeed or its filters conditional 
on the value of the $upstream_http_x_pagespeed_enable /
$sent_http_x_pagespeed_enable (same value) variable.

I've tried using if statements similar to that outlined by the tests
various permutations of if statements / maps / variables / etc, but I can't 
seem to find a working approach.
 
I suspect that the response variables aren't set at the point that they are 
being evaluated in the config, but I'm not well versed enough in nginx to say
for sure.

Does anyone have any suggestions / thoughts on how I might accomplish this?

Greatly appreciated,
Ash

Otto van der Schaaf

unread,
Apr 18, 2017, 7:26:50 AM4/18/17
to ngx-pagespeed-discuss
If you could add "PageSpeed:[on|off]" to the response headers with add_header in the @app location{} block, I think that should work.
See https://modpagespeed.com/doc/experiment#PerRequest 

Alternatively, ngx_pagespeed also allows you to script configuration variables, but PageSpeed:on|off is the one variable that is not yet support as that needs to be treated differently from the rest, but maybe a conditional RewriteLevel PassTrough mode may help you here.

Otto

--
You received this message because you are subscribed to the Google Groups "ngx-pagespeed-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ngx-pagespeed-di...@googlegroups.com.
Visit this group at https://groups.google.com/group/ngx-pagespeed-discuss.
For more options, visit https://groups.google.com/d/optout.

Ash Tyndall

unread,
Apr 21, 2017, 5:28:58 PM4/21/17
to ngx-pagesp...@googlegroups.com
Thanks Otto, setting the "PageSpeed" header worked great!

For anyone curious, the following config maintains compatibility with my existing header (and to default pagespeed to off if the header is unset), and seems to otherwise work;

map $upstream_http_x_pagespeed_enable $pagespeed_header {
  "true"        "on";
  default       "off";
}

upstream app {
  server unix:/foo.sock fail_timeout=0;
}

server {
  listen 80;
  server_name localhost example.com;
  root /app/public;
  try_files $uri/index.html $uri @app;

  # Pagespeed
  pagespeed on;
  pagespeed FileCachePath /var/ngx_pagespeed_cache;

  location @app {
    proxy_pass http://app;
    
    # Conditionally enable pagespeed
    add_header "PageSpeed" $pagespeed_header;
  }

  # Ensure requests for pagespeed optimized resources go to the pagespeed 
  # handler, and no extraneous headers get set.
  location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
    add_header "" "";
  }
  location ~ "^/pagespeed_static/" { }
  location ~ "^/ngx_pagespeed_beacon$" { }
}

Ash

To unsubscribe from this group and stop receiving emails from it, send an email to ngx-pagespeed-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "ngx-pagespeed-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ngx-pagespeed-discuss/nWlLP01A_54/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ngx-pagespeed-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages