Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node.js prerenderer for Angular etc.
server_name example.com;
location / {
# Angular/React/whatever frontend
root /home/example/frontend/dist;
try_files $uri @prerender;
}
# config for Node.js prerenderer
location @prerender {
root /home/example/frontend/dist;
set $prerender 0;
if ($http_user_agent ~* "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;
}
#resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
resolver 8.8.8.8;
if ($prerender = 1) {
#setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
set $prerender "localhost:3000";
rewrite .* /$scheme://$host$request_uri? break;
proxy_pass http://$prerender;
}
if ($prerender = 0) {
rewrite .* /index.html break;
}
}
# Django backend
location ~ ^/(api|) {
include uwsgi_params;
uwsgi_pass #localhost:9000;
uwsgi_intercept_errors on;
}
{
"name": "docker-prerender",
"version": "1.0.0",
"description": "Dockerfile to build the prerender container image",
"main": "server.js",
"dependencies": {
"phantomjs-prebuilt": "^2.1.12",
"prerender": "4.3.1",
"sockjs-client": "^1.1.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "https://github.com/cerisier/docker-prerender.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/cerisier/docker-prerender/issues"
},
"homepage": "https://github.com/cerisier/docker-prerender"
}
var prerender = require('prerender');
var server = prerender({
workers: process.env.PHANTOM_CLUSTER_NUM_WORKERS,
iterations: process.env.PHANTOM_WORKER_ITERATIONS || 10,
phantomBasePort: process.env.PHANTOM_CLUSTER_BASE_PORT || 12300,
messageTimeout: process.env.PHANTOM_CLUSTER_MESSAGE_TIMEOUT
});
server.use(prerender.removeScriptTags());
if (process.env.BASIC_AUTH_USERNAME) {
server.use(prerender.basicAuth());
}
server.use(prerender.inMemoryHtmlCache());
//server.use(prerender.blacklist());
//server.use(prerender.httpHeaders());
// server.use(prerender.s3HtmlCache());
// server.use(require('my-plugin'));
server.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment