使用staticfile buildpack的prerender.io

时间:2017-04-27 19:43:09

标签: angularjs nginx cloudfoundry

我尝试使用云代工厂静态文件buildpack(https://github.com/cloudfoundry/staticfile-buildpack)设置prerender.io,但我无法正确设置文件。

我无法找到staticfile默认使用的默认nginx.conf。我使用(https://github.com/cloudfoundry/staticfile-buildpack/blob/master/cf_spec/fixtures/reverse_proxy/nginx.conf)作为参考,但它似乎不是正确的。

下面的nginx.conf部分正常工作。出于某种原因,正在加载css,但它们没有被渲染。只显示浏览器默认样式。

可以在http://cabemcasa-front-staging-dev.mybluemix.net/

访问服务器的在线预览
worker_processes 1;
daemon off;
error_log <%= ENV["APP_ROOT"] %>/nginx/logs/error.log;
events { 
    worker_connections 1024; 
}
http {
  log_format cloudfoundry '$http_x_forwarded_for - $http_referer - [$time_local] "$request" $status $body_bytes_sent';
  access_log <%= ENV["APP_ROOT"] %>/nginx/logs/access.log cloudfoundry;
  default_type application/octet-stream;
  sendfile on;
  gzip on;
  tcp_nopush on;
  keepalive_timeout 30;
  server {
    listen <%= ENV["PORT"] %>;
    server_name localhost;
    location / {
      root <%= ENV["APP_ROOT"] %>/public;
      index index.html index.htm Default.htm;
    }

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

    #         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)$") {
    #             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) {
    #             # rewrite .* /index.html break;
    #         }
    #     }
  }
}

1 个答案:

答案 0 :(得分:1)

我在另一个项目中做过类似的事情。试试这个:

worker_processes  1;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen      <%= ENV["PORT"] %>;
        root   <%= ENV["APP_ROOT"] %>/public;
        index  index.html;
        server_name  localhost;
        location / {
            try_files $uri @prerender;
        }

        location @prerender {
            proxy_set_header X-Prerender-Token sSfyna56hdgshmXfr24O;

            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)$") {
                set $prerender 0;
            }

            resolver 8.8.8.8;
            if ($prerender = 1) {
                set $prerender "service.prerender.io";
                rewrite .* /$scheme://$host$request_uri? break;
                proxy_pass http://$prerender;
            }
            if ($prerender = 0) {
                rewrite .* /index.html break;
            }

        }
    }
}