Skip to content

Instantly share code, notes, and snippets.

@regilero regilero/nginx.conf forked from thoop/nginx.conf
Last active Jun 7, 2019

Embed
What would you like to do?
Altered prerender.io nginx.conf for nginx
# Change YOUR_TOKEN to your prerender token and uncomment that line if you want to cache urls and view crawl stats
# Change example.com (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;
server_name example.com;
root /path/to/your/root;
index index.html;
# 5xx errors managed by your app
proxy_intercept_errors on;
error_page 500 502 503 504 =200 /index.html;
# uncomment if you want 404 managed by the app
# error_page 404 =200 /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
resolver 8.8.8.8;
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 "127.0.0.1:3000";
# Or online service
set $proxyprerender "service.prerender.io";
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;
}
}
}
@regilero

This comment has been minimized.

Copy link
Owner Author

commented May 18, 2017

Modifications to remove dangerous if usages, replacing with 2 chained map. Adding error pages handling, some more bots, and a 404 for statics.

@regilero

This comment has been minimized.

Copy link
Owner Author

commented Aug 30, 2017

Matching bots with case insensitive ~* instead of ~.

@tspicer

This comment has been minimized.

Copy link

commented Nov 8, 2017

Nice work, the proper way to do an nginx conf.

@xorik

This comment has been minimized.

Copy link

commented Dec 11, 2017

@regilero thanks. Works great. I've also added google+ bot:

"~*developers.google.com/\+/web/snippet/" 1;
@CNSKnight

This comment has been minimized.

Copy link

commented Dec 12, 2017

Curious, why not try_files ... index.html fallback, rather the = 0 check?

@zvaraondrej

This comment has been minimized.

Copy link

commented Feb 16, 2019

Hey, great work! Please note there's a need to add user agents for googlebot, bingbot and yandex, if you want to support them. https://prerender.io/documentation/google-support

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.