Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
haproxy configuration for using with prerender.io
# Change YOUR_TOKEN to your prerender token
# Change http://example.com (server_name) to your website url
frontend my-frontend
mode http
bind :80
# prerender.io
acl user-agent-bot hdr_sub(User-Agent) -i baiduspider twitterbot facebookexternalhit rogerbot linkedinbot embedly showyoubot outbrain pinterest slackbot vkShare W3C_Validator
acl url-asset path_end js css xml less png jpg jpeg gif pdf doc txt ico rss zip mp3 rar exe wmv doc avi ppt mpg mpeg tif wav mov psd ai xls mp4 m4a swf dat dmg iso flv m4v torrent ttf woff
acl url-escaped-fragment url_sub _escaped_fragment_
use_backend prerender if user-agent-bot !url-asset
use_backend prerender if url-escaped-fragment !url-asset
backend prerender
mode http
timeout server 20s
server prerender service.prerender.io:443 check ssl verify none
http-request set-header X-Prerender-Token YOUR_TOKEN
reqrep ^([^\ ]*)\ /(.*)$ \1\ /http://example.com/\2
@tranphuoctien

This comment has been minimized.

Copy link

tranphuoctien commented Nov 27, 2015

Yeah! thank so much! you save my time!

@cviebrock

This comment has been minimized.

Copy link

cviebrock commented Sep 29, 2016

I'm planning on running prerender locally. Obviously, I'd change the server line to something like:

server prerender localhost:3000 check ssl verify none

But what is the last line supposed to be doing? I'm not sure what the "example.com" is supposed to be.

@jsonmaur

This comment has been minimized.

Copy link

jsonmaur commented Oct 2, 2016

A few corrections. Each extension needs to have a leading dot, otherwise anything with those trailing characters but not as an extension won't be prerendered (such as pagecss).

acl url-asset path_end .js .css .xml .less .png .jpg .jpeg .gif .pdf .doc .txt .ico .rss .zip .mp3 .rar .exe .wmv .doc .avi .ppt .mpg .mpeg .tif .wav .mov .psd .ai .xls .mp4 .m4a .swf .dat .dmg .iso .flv .m4v .torrent .ttf .woff

Also instead of using reqrep, it's better to use http-request set-path. Allows variables to be used, etc.

backend prerender
  server pre1 ${PRERENDER_HOST} check
  acl with_ssl ssl_fc
  http-request set-path /https://%[hdr(host)]%[path] if with_ssl
  http-request set-path /http://%[hdr(host)]%[path] unless with_ssl
@jsonmaur

This comment has been minimized.

Copy link

jsonmaur commented Oct 2, 2016

It may actually be better to whitelist extensions rather than have a huge blacklist. Since the only pages that need to be prerendered are html pages, I think it would be safe to assume only .html, .htm, and no extension at all. This should do the trick:

acl url-asset path_reg ^(.*\.(html?)$)?[^.]*$

use_backend prerender if user-agent-bot url-asset
use_backend prerender if url-escaped-fragment url-asset
@Tazer

This comment has been minimized.

Copy link

Tazer commented Nov 23, 2016

Saved me lots of time! Thanks! 👍

@gzurbach

This comment has been minimized.

Copy link

gzurbach commented Aug 4, 2017

I recommend changing:

server prerender service.prerender.io:443 check ssl verify none

to:

server prerender service.prerender.io:443 check inter 5000 ssl verify none

This increases the interval between the checks. Default is 2000 ms. A couple days ago, prerender was down for about 10 seconds. Haproxy had 3 consecutive failed checks (3 x 2000ms = 6000ms) and marked the backend as "down". And since there is only one server, once it's down, it stays in this status forever. Increasing the interval to 5000ms allows prerender to be down for 15s.

@gnavalesi

This comment has been minimized.

Copy link

gnavalesi commented Apr 11, 2019

Since Google now supports dynamic rendering, the googlebot agent should be added to the user-agent-bot acl

acl user-agent-bot hdr_sub(User-Agent) -i googlebot baiduspider twitterbot facebookexternalhit rogerbot linkedinbot embedly showyoubot outbrain pinterest slackbot vkShare W3C_Validator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.