首页 > 解决方案 > AngularJS 不适用于第三方预渲染服务

问题描述

我正在使用 AngularJS 和 SEO4Ajax。我使用 nginx 在 docker 容器中运行我的网站。我将所有 nginx 配置从 SEO4Ajax 复制到 docker 容器。Seo4Ajax 已创建快照,但以结尾的 url?_escaped_fragment_=不起作用。

AngularJS 标头

meta(name='fragment', content='!')

AngularJS 配置

$locationProvider.html5Mode(true).hashPrefix('!');

Nginx 配置

server {
    listen 80;
    sendfile off;
    expires 0;
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        try_files $uri @s4a_analyse $uri/ /index.html =404;

        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, OPTIONS';
    }

    ### This location determines if a request comes from bots
    location @s4a_analyse {

        ### If the request comes from a bot, proxy the request through /s4a_proxy location
        if ($http_user_agent ~* (google|bot|spider|pinterest|crawler|archiver|flipboardproxy|mediapartners|facebookexternalhit|insights|quora|whatsapp|slurp)) {
            rewrite ^(.*)$ /s4a_proxy last;
        }

        ### Uncomment the 3 following lines to support the _escaped_fragment_= parameter
        if ($args ~ "_escaped_fragment_=") { 
            rewrite  ^(.*)$  /s4a_proxy  last;
        }

        if ($http_from ~* .+) {
            rewrite ^(.*)$ /s4a_proxy last;
        }

        ### Otherwise serve /index.html
        rewrite ^(.*)$ /index.html last;
    }

    ### This location proxy requests coming from bots to SEO4Ajax
    ### You can update the resolver directive with your own DNS provider if needed
    location /s4a_proxy {
        set $s4a_domain 'https://api.seo4ajax.com/SEO4AJAX_TOKEN';
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        resolver 8.8.8.8 8.8.4.4;
        proxy_pass $s4a_domain$request_uri;
    }
}

标题

我试图卷曲 url 来检索标题,X-Powered-By: SEO4Ajax但没有产生。它应该基于this显示 seo4ajax 标头。

curl -H "User-Agent: Bot" -I http://www.mywebsite.net

HTTP/1.1 200 OK
Date: Sun, 14 Apr 2019 07:08:56 GMT
Content-Type: text/html
Connection: keep-alive
Set-Cookie: __cfduid=d464769ca8ded696b9c1dcfd4ed5bc14c1555225736; expires=Mon, 13-Apr-20 07:08:56 GMT; path=/; domain=.mywebsite.net; HttpOnly
Accept-Ranges: bytes
Access-Control-Allow-Methods: GET, PUT, POST, OPTIONS
Access-Control-Allow-Origin: *
Cache-Control: max-age=0
Expires: Sun, 14 Apr 2019 07:08:56 GMT
Last-Modified: Sun, 14 Apr 2019 05:00:06 GMT
Strict-Transport-Security: max-age=315360000; includeSubdomains; preload
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Xss-Protection: 1; mode=block
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 4c73d9f24871a542-NRT

对于服务器,我使用 cloudflare 转发到我的数字海洋服务器液滴 IP。

预期输出:

curl -H“用户代理:机器人”-I https://www.mywebsite.net

应该在标题中产生X-Powered-By: SEO4Ajax

标签: angularjsnginxseoprerender

解决方案


_escaped_fragment_ 自 2018 年中期以来已被弃用。您可能会尽量避免使用它。prerender.io可能是您的选择。

参考:

  1. https://developers.google.com/search/docs/ajax-crawling/docs/specification
  2. https://medium.com/finnovate-io/googlebot-no-longer-picking-up-content-in-prerender-io-pages-ae21d9710459

推荐阅读