Skip to content

Instantly share code, notes, and snippets.

@rishi-raj-jain
Created July 30, 2020 15:40
Show Gist options
  • Save rishi-raj-jain/158698d442a6fbef1cdc439e1b7e5dc4 to your computer and use it in GitHub Desktop.
Save rishi-raj-jain/158698d442a6fbef1cdc439e1b7e5dc4 to your computer and use it in GitHub Desktop.
server {
listen 80;
listen [::]:80;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri @prerender;
}
location @prerender {
set $prerender 0;
if ($http_user_agent ~* "whatsapp|googlebot|bingbot|yandex|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|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;
}
if ($prerender = 1) {
rewrite .* /render/$scheme://$host$request_uri? break;
proxy_pass http://domain.com;
}
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