nginx prerender不起作用

时间:2018-04-09 22:03:20

标签: angularjs nginx prerender

我尝试使用以下配置,但它无效。 Prerender.io页面会显示“我们还没有看到您的Prerender令牌的请求”。 显然我用我的YOUR_TOKEN替换了 有人可以帮帮我吗?

server {
  listen       80;
  server_name  localhost;

  root /usr/share/nginx/html;
  index index.html index.htm;

  location / {
     try_files $uri $uri/ /index.html =404;
 }

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

对不起身份

1 个答案:

答案 0 :(得分:1)

看起来你的try_files指令中没有@prerender,所以不会执行@prerender。

此外,如果您从文件系统提供index.html,有时try_files $ uri将始终与索引页面匹配,因此即使您在其中添加@prerender,它也可能无法执行。你可能会尝试类似的东西:

server {
  listen       80;
  server_name  localhost;

  root /usr/share/nginx/html;
  index index.html index.htm;

  location / {
    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|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 "service.prerender.io";
        rewrite .* /$scheme://$host$request_uri? break;
        proxy_pass http://$prerender;
    }

     try_files $uri $uri/ /index.html =404;
 }

}