I
I
Ivan2021-03-12 16:00:13
Nginx
Ivan, 2021-03-12 16:00:13

How to setup prerender for nginx?

I use nginx + laravel + react.js, there was a problem in SEO.
I decided to use prerender.io

The prerender itself works well, but now we need to connect this whole thing to nginx.

Here is a link to the nginx doc.

Here is my config:

server {
    listen 80;
    server_name www.mysite.com mysite.com;
    root   /var/www/mysite.com/public;
    index index.php;
    client_max_body_size 10M;

    location / {
        try_files $uri $uri/ /index.php?$query_string @prerender;
    }

  location @prerender {
            proxy_set_header X-Prerender-Token YOUR_PRERENDER_TOKEN_GOES_HERE;
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;
            }
            resolver 8.8.8.8;
if ($prerender = 1) {
                rewrite .* /$scheme://$host$request_uri? break;
                proxy_pass http://localhost:3000;
            }
            if ($prerender = 0) {
                rewrite .* /index.html break; 
                proxy_pass https://mysite.com;
            }
        }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
 
}


I change user agents, but nothing changes ((
Question - what is wrong with the config and how to configure it correctly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question