Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
new nginx.conf with map
# Change YOUR_TOKEN to your prerender token and uncomment that line if you want to cache urls and view crawl stats
# Change (server_name) to your website url
# Change /path/to/your/root to the correct value
# Generate $prerender_ua bool value based on user agent
# indexation bots will get this as 1,
# prerender user agent will always get 0 (avoid loops)
map $http_user_agent $prerender_ua {
default 0;
"~Prerender" 0;
"~baiduspider" 1;
"~twitterbot" 1;
"~baiduspider" 1;
"~facebookexternalhit" 1;
"~rogerbot" 1;
"~linkedinbot" 1;
"~embedly" 1;
"~quora link preview" 1;
"~showyoubot" 1;
"~outbrain" 1;
"~pinterest" 1;
"~slackbot" 1;
"~vkShare" 1;
"~Slack-ImgProxy" 1;
"~Slackbot-LinkExpanding" 1;
"~Site Analyzer" 1;
"~SiteAnalyzerBot" 1;
"~Viber" 1;
"~Whatsapp" 1;
"~Telegram" 1;
"~W3C_Validator" 1;
# Generate $prerender bool value based on _escaped_fragement_ argument presence
# detection OR get the $prerender_ua bool value
map $args $prerender {
default $prerender_ua;
"~(^|&)_escaped_fragment_=" 1;
server {
listen 80;
root /path/to/your/root;
index index.html;
location / {
try_files $uri @prerender;
location @prerender {
#proxy_set_header X-Prerender-Token YOUR_TOKEN;
# static files extensions detection. These should be managed by the
# $uri part of the try_file "try_files $uri @prerender;". If we get
# there it means either the static file is missing OR we need to
# handle it in the app. In that second case see error_page instructions
if ($uri ~* "\.(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|svg|eot)") {
return 404;
#resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
if ($prerender = 1) {
#setting proxyprerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
# local prerender
#set $proxyprerender "";
# Or online service
set $proxyprerender "";
proxy_intercept_errors on;
# $http_x_forwarded_proto could be used instead of $scheme
# if you are behind an ssl terminator with X-Forwarded-Proto set
rewrite .* /$scheme://$host$request_uri? break;
proxy_pass http://$proxyprerender;
if ($prerender = 0) {
rewrite .* /index.html break;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment