Skip to content

@Stanback /nginx.conf
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Example Nginx configuration for serving pre-rendered HTML from Javascript pages/apps using the Prerender Service (https://github.com/collectiveip/prerender). Instead of using try_files (which can cause unnecessary overhead on busy servers), you could check $uri for specific file extensions and set $prerender appropriately.
server {
listen 80;
listen [::]:80;
server_name yourserver.com;
root /path/to/your/htdocs;
error_page 404 /404.html
index index.html;
location ~ /\. {
deny all;
}
location / {
try_files $uri @prerender;
}
location @prerender {
#proxy_set_header X-Prerender-Token YOUR_TOKEN;
set $prerender 0;
if ($http_user_agent ~* "googlebot|yahoo|bingbot|baiduspider|yandex|yeti|yodaobot|gigabot|ia_archiver|facebookexternalhit|twitterbot|developers\.google\.com") {
set $prerender 1;
}
if ($args ~ "_escaped_fragment_|prerender=1") {
set $prerender 1;
}
if ($http_user_agent ~ "Prerender") {
set $prerender 0;
}
if ($prerender = 1) {
rewrite .* /$scheme://$host$request_uri? break;
#proxy_pass http://localhost:3000;
proxy_pass http://service.prerender.io;
}
if ($prerender = 0) {
rewrite .* /index.html break;
}
}
}
@MWers

I encountered the following error when using this Gist:

Heroku | No such app

There is no app configured at that hostname.
Perhaps the app owner has renamed it, or you mistyped the URL.

To fix it, I commented out line #20:

        # proxy_set_header Host $host;
@thoop

If anyone sees problems with their URL looking like this: http://myurl.com/post/123#!_escaped_fragment_= (notice the #! AND the_escaped_fragment_)

Change this line:

rewrite .* /$scheme://$host$request_uri break;

to:

rewrite .* /$scheme://$host$request_uri? break;

According to (nginx.org)[http://wiki.nginx.org/HttpRewriteModule]:

If you specify a ? at the end of a rewrite then Nginx will drop the original $args (arguments). When using $request_uri or $uri&$args you should specify the ? at the end of the rewrite to avoid Nginx doubling the query string.

@saintberry

Any tips on how I can modify this to work with the Facebook Open Graph crawler (https://developers.facebook.com/tools/debug/)?

With the above, and with the canonical url and og:url metadata tags set as domain.com/#!/path if a user likes that page Facebook will save the share URL as domain.com/?_escaped_fragment=/path. So when a user clicks on the share link on Facebook they get directed through the proxy.

@Stanback
Owner

@saintberry Good question, you can try getting rid of the following lines:

if ($args ~ "_escaped_fragment_|prerender=1") {
    set $prerender 1;
}

Since it's already checking for the user agent, I don't think there will be a problem with omitting that additional check. An alternate option that you could experiment with would be to set $prerender to 0 if the $http_referer matches Facebook.

@Stanback
Owner

FYI:
Googlebot is starting to render Javascript so you may want to remove it from the user agents list (after testing in webmaster tools)

See:
http://googlewebmastercentral.blogspot.com/2014/05/rendering-pages-with-fetch-as-google.html

@dfmcphee

Could this be used with another proxy pass to a node.js application using an upstream? I can't seem to get it to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.