A
A
Alexey Yarkov2016-12-20 13:24:16
Nginx
Alexey Yarkov, 2016-12-20 13:24:16

How to use prerender for home page?

Site on Angular 1.6.0.

/**
 * Режим html5 для адресной строки и роутера
 */
$locationProvider.html5Mode({
  enabled: true,
  requireBase: false
});

The meta tag <meta name="fragment" content="!">is present.
I cloned myself on VPS prerender and set up Nginx.
In general, it works for all pages, except for the main one.
If you write https://domain.ru/about/?_escaped_fragment_=, then everything is as it should be.
And if https://domain.ru/?_escaped_fragment_=, then an unrendered page is returned.
Piece of Nginx config:
upstream app {
    # API запущен на 8080 порту
    server 127.0.0.1:8080;
}

server {
    listen 80;
    server_name domain.ru www.domain.ru;
    proxy_set_header Host domain.ru;

    location / {
        rewrite ^(.*)$ https://domain.ru$1 permanent;
    }

    return 301 https://domain.ru$request_uri;
}

server {
    listen       443 ssl http2;
    server_name  domain.ru www.domain.ru;
    root /var/www/domain.ru;

    index index.html;

    location / {
        try_files $uri $uri/ @prerender;
    }

    location @prerender {
        #proxy_set_header X-Prerender-Token YOUR_TOKEN;
        
        set $prerender 0;

        if ($http_user_agent ~* ".*(Yandex|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;
        }
 
        if ($prerender = 1) {
            # prerender запущен на 8000 порту
            set $prerender "127.0.0.1:8000";
            rewrite .* /$scheme://$host$request_uri? break;
            proxy_pass http://$prerender;
        }

        if ($prerender = 0) {
            rewrite .* /index.html break;
        }
    }

    location /api/ {
        proxy_pass http://app;
    }

}

Superfluous, such as static caching, removed. What do I need to change?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2016-12-21
@Casufi

1) See the logs of the prerender itself
2) If the logs are not created, install the prerender locally, run it, skip the main page and see what it pours into the console.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question