admin管理员组

文章数量:1295712

I'm trying to use prerender.io to get an snapshot of angularjs pages. Currently I have an NodeJS instance for the web app and nginx reverse proxy redirects requests from port 80 to 4000.

According to prerender nginx manual () I can forward search-engine bot requests to the prerender url but because I already have a proxy for NodeJS application, I don't know how can I prerender try_files directive.

My question is, how can I use both NodeJS application proxy and prerender directive?

I'm trying to use prerender.io to get an snapshot of angularjs pages. Currently I have an NodeJS instance for the web app and nginx reverse proxy redirects requests from port 80 to 4000.

According to prerender nginx manual (https://gist.github./thoop/8165802) I can forward search-engine bot requests to the prerender url but because I already have a proxy for NodeJS application, I don't know how can I prerender try_files directive.

My question is, how can I use both NodeJS application proxy and prerender directive?

Share Improve this question edited May 28, 2015 at 0:54 drew moore 32.7k17 gold badges79 silver badges113 bronze badges asked Mar 1, 2015 at 11:27 Afshin MehrabaniAfshin Mehrabani 35k33 gold badges144 silver badges209 bronze badges 2
  • Having the same problem but using paster instead of node - adding bounty – drew moore Commented May 27, 2015 at 18:48
  • Can you supply a nginx.conf documenting where things should happen and they are not happening? 2. On gisthub I see examples with alot of "if's", an if can ONLY be used to return a state (if xxx return yyy) when you use an if to set multiple variables it WILL go wrong. Any 'if' should be done with a 'map' and then a single if or rewrite in a location block. – itpp13 Commented May 29, 2015 at 12:10
Add a ment  | 

1 Answer 1

Reset to default 7 +100

I believe that the prerender example has the answer. If prerender is set to 1, it uses rewrite and then proxy_pass.

So you would change this:

if ($prerender = 0) {
            rewrite .* /index.html break;
        }

to this:

if ($prerender = 0) {
        rewrite .* /index.html break;
        proxy_pass http://[INTERNAL IP]:[PORT];
    }

I would make further modifications since you are using Node and don't need some of the stuff set up for static files.

Here is my final answer:

server {
   listen 80;
   server_name example.;

   location / {
       try_files $uri @prerender;
   }

location @prerender {
    #proxy_set_header X-Prerender-Token YOUR_TOKEN;

    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)") {
        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 "service.prerender.io";
        rewrite .* /$scheme://$host$request_uri? break;
        proxy_pass http://$prerender;
    }
    if ($prerender = 0) {
        proxy_pass http://[INTERNAL IP]:[PORT];
    }
}
} 

I hope that helps. One thing that I will add is that I wouldn't use a prerender engine. Spiders can and do index links and pages that use javascript and PDFs even.

Just my two cents.

本文标签: javascriptUsing prerender with proxy in nginxStack Overflow