使用本地测试服务器进行预渲染

时间:2016-06-01 03:11:58

标签: nginx localhost prerender

我想在我的本地开发服务器上使用Prerender进行测试。 Flask(网站后端)的应用服务器运行在localhost:5000上,现在我已经在localhost:3000上设置了Prerender服务器。 在Prerender的GitHub页面上,它说为了使用Prerender作为本地服务,我需要

export PRERENDER_SERVICE_URL=<your local url>

以及我的nginx配置文件

    server {
    listen 80;
    server_name example.com;

    root   /path/to/your/root;
    index  index.html;

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

我有两个问题:

1.什么

export PRERENDER_SERVICE_URL=<your local url>

做什么?我应该如何设置&#34;您的本地网址&#34;至?它应该是网站的app服务器的url(localhost:5000)还是prerender服务器的url(localhost:3000)。

2.我将我的nginx.conf更改为包括:

server {
    listen 80;
    server_name localhost:5000;

    root   /path/to/my/root;
    index  index.html;

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

它似乎不起作用。当我访问localhost(localhost:80)时,它说404找不到。似乎它甚至没有正确地将流量路由到localhost:5000。

关于我做错了什么的任何想法?非常感谢!

1 个答案:

答案 0 :(得分:0)

由于您使用的是nginx配置,因此无需使用PRERENDER_SERVICE_URL。在你的情况下,它不会做任何事情。

service.prerender.io改为localhost:3000就像在nginx配置中一样,这是指向本地Prerender服务器的正确方法。

虽然,您只是没有正确设置nginx。您可能必须将重写更改为index.html到proxy_pass到localhost:5000。这应该localhost:80可以使用,然后您可以访问localhost:80/?_escaped_fragment_=

上的预呈现页面