将 prerender.io 与我现有的nginx配置一起使用



我正在尝试为谷歌实现动态渲染。

我在Laravel Forge上有一台服务器,用于为Nuxt.js应用程序提供服务,我想使用 prerender.io

Prerender.io 提供了一个nginx配置供使用,但我当前的配置似乎与它有很大不同。我没有任何经验,所以如果有人可以提供帮助,我会寻求帮助。

这是我当前的nginx配置


# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/beta.example.com/before/*;

map $sent_http_content_type $expires {
    "text/html"                 epoch;
    "text/html; charset=utf-8"  epoch;
    default                     off;
}
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name beta.example.com;
    root /home/forge/beta.example.com/dist;
    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/beta.example.com/50331/server.crt;
    ssl_certificate_key /etc/nginx/ssl/beta.example.com/50331/server.key;
    ssl_protocols TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    charset utf-8;
    gzip            on;
    gzip_types      text/plain application/xml text/css application/javascript;
    gzip_min_length 1000;
    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/beta.example.com/server/*;
    location / {
        expires $expires;
        proxy_redirect off;
        proxy_set_header Host               $host;
        proxy_set_header X-Real-IP          $remote_addr;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto  $scheme;
        proxy_read_timeout          1m;
        proxy_connect_timeout       1m;
        proxy_pass                          http://127.0.0.1:3000; # set the adress of the Node.js
    }
    access_log off;
    error_log  /var/log/nginx/beta.example.com-error.log error;
    location ~ /.(?!well-known).* {
        deny all;
    }
}
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/beta.example.com/after/*;

这就是它应该的样子https://gist.github.com/thoop/8165802

我的主要问题基本上是当前的location /块会发生什么。

您有两种方法可以实现目标:

  • Nuxt.js有自己的预渲染解决方案。 你可以在这里找到它。
  • 如果您仍想使用 prerender.io,下面是一个示例:

我看到您的 Nginx 正在将所有流量代理到 http://127.0.0.1:3000,这可能是用 Node.js 编写的后端。因此,您可以直接更新location /块以使用 prerender.io 来捕获所有内容。

...
location / {
    proxy_set_header X-Prerender-Token YOUR_TOKEN;
    set $prerender 0;
    if ($http_user_agent ~* "googlebot|bingbot|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;
    }
    #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;
    }
    try_files @backend;
}
location @backend {
    expires $expires;
    proxy_redirect off;
    proxy_set_header Host               $host;
    proxy_set_header X-Real-IP          $remote_addr;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto  $scheme;
    proxy_read_timeout          1m;
    proxy_connect_timeout       1m;
    proxy_pass                          http://127.0.0.1:3000; # set the adress of the Node.js
}
...

最新更新