Skip to content

Instantly share code, notes, and snippets.

@alickmail
Created July 31, 2019 09:15
Show Gist options
  • Save alickmail/41f59cbc26c45e61110ca9716907bc5a to your computer and use it in GitHub Desktop.
Save alickmail/41f59cbc26c45e61110ca9716907bc5a to your computer and use it in GitHub Desktop.
# Change YOUR_TOKEN to your prerender token
# Change example.com (server_name) to your website url
# Change /path/to/your/root to the correct value
server {
listen 80;
server_name YOUR_DOMAIN.COM;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
try_files $uri @prerender;
}
location @prerender {
# When you register in prerenderio, you will get a token
proxy_set_header X-Prerender-Token YOUR_OWN_TOKEN;
# Check the header
set $prerender 0;
if ($http_user_agent ~* "googlebot|bingbot|yandex|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest\\/0\\.|pinterestbot|slackbot|vkShare|W3C_Validator") {
set $prerender 1;
}
if ($args ~ "_escaped_fragment_") {
set $prerender 1;
}
if ($http_user_agent ~ "Prerender") {
set $prerender 0;
}
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)") {
set $prerender 0;
}
#resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
resolver 8.8.8.8;
# If header come from Facebook/ Scrapper, redirect to prerender.io
if ($prerender = 1) {
#setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
set $prerender "service.prerender.io";
rewrite .* /$scheme://$host$request_uri? break;
proxy_pass <http://$prerender>;
}
# If the header come from normal user, redirect to cdn
# you may also direct to local html file too
if ($prerender = 0) {
set $prerender "YOUR_CDN_DOMAIN.com";
proxy_pass <http://$prerender>;
#return 301 https://blog.club.com$request_uri;
#rewrite ^/(.*)$ <http://testingapp.tosgame.com$1> permanent;
#return 301 http://testingapp.tosgame.com$request_uri;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment