ini 使用Prerender服务从Javascript页面/应用程序提供预渲染HTML的示例Nginx配置(https://github.com/collect

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ini 使用Prerender服务从Javascript页面/应用程序提供预渲染HTML的示例Nginx配置(https://github.com/collect相关的知识,希望对你有一定的参考价值。

# Note (November 2016):
# This config is rather outdated and left here for historical reasons, please refer to prerender.io for the latest setup information
# Serving static html to Googlebot is now considered bad practice as you should be using the escaped fragment crawling protocol

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;
        }
    }
}

ini 为nginx改变了prerender.io nginx.conf

# Change YOUR_TOKEN to your prerender token and uncomment that line if you want to cache urls and view crawl stats
# Change example.com (server_name) to your website url
# Change /path/to/your/root to the correct value

# Generate $prerender_ua bool value based on user agent
# indexation bots will get this as 1, 
# prerender user agent will always get 0 (avoid loops)
map $http_user_agent $prerender_ua {

    default 0;

    "~Prerender" 0;

    "~*baiduspider" 1;
    "~*twitterbot" 1;
    "~*baiduspider" 1;
    "~*facebookexternalhit" 1;
    "~*rogerbot" 1;
    "~*linkedinbot" 1;
    "~*embedly" 1;
    "~*quora link preview" 1;
    "~*showyoubot" 1;
    "~*outbrain" 1;
    "~*pinterest" 1;
    "~*slackbot" 1;
    "~*vkShare" 1;
    "~*Slack-ImgProxy" 1;
    "~*Slackbot-LinkExpanding" 1;
    "~*Site Analyzer" 1;
    "~*SiteAnalyzerBot" 1;
    "~*Viber" 1;
    "~*Whatsapp" 1;
    "~*Telegram" 1;
    "~*W3C_Validator" 1;
}

# Generate $prerender bool value based on _escaped_fragement_ argument presence
# detection OR get the $prerender_ua bool value
map $args $prerender {
    default $prerender_ua;
    "~(^|&)_escaped_fragment_=" 1;
}

server {
    listen 80;
    server_name example.com;
 
    root   /path/to/your/root;
    index  index.html;
    
    # 5xx errors managed by your app
    proxy_intercept_errors on;  
    error_page 500 502 503 504 =200 /index.html;
    # uncomment if you want 404 managed by the app
    # error_page 404 =200 /index.html;

    
    location / {
        try_files $uri @prerender;
    }
 
    location @prerender {
        #proxy_set_header X-Prerender-Token YOUR_TOKEN;
        
        # static files extensions detection. These should be managed by the
        # $uri part of the try_file "try_files $uri @prerender;". If we get
        # there it means either the static file is missing OR we need to
        # handle it in the app. In that second case see error_page instructions       
        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)") {
            return 404;
        }
        
        #resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
        resolver 8.8.8.8;
 
        if ($prerender = 1) {
            #setting proxyprerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
            #     local prerender
            #set $proxyprerender "127.0.0.1:3000";
            #     Or online service
            set $proxyprerender "service.prerender.io";
        
            proxy_intercept_errors on;
            # $http_x_forwarded_proto could be used instead of $scheme
            # if you are behind an ssl terminator with X-Forwarded-Proto set    
            rewrite .* /$scheme://$host$request_uri? break;
            proxy_pass http://$proxyprerender;
        }
        if ($prerender = 0) {
            rewrite .* /index.html break;
        }
    }
}

以上是关于ini 使用Prerender服务从Javascript页面/应用程序提供预渲染HTML的示例Nginx配置(https://github.com/collect的主要内容,如果未能解决你的问题,请参考以下文章

prerender.io 搜索引擎优化 部署成windows服务 实现开机自动开启服务

部署prerender服务器

javascript 从控制台清除prerender.io中的所有缓存页面

vue-cil 服务端预渲染 prerender-spa-plugin

如何使用 spa-prerender 插件在 VUE 中预渲染“*”(未找到)页面

错误 [prerender-spa-plugin] - 无法预渲染所有路由