如何将prerender.io集成到nginx配置中?

y4ekin9u  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(147)

我希望添加prerender.io更好的搜索引擎优化,因为我正在运行一个Vue前端和Node后端通过Nginx服务器上的Docker容器进行通信。我的理解是,如果没有预渲染或SSR,opengraph标签将不会被某些机器人读取。SSR是相当参与,并不完全必要,因此prerender似乎是所有我需要的。
以下是我的默认文件:

# HTTP - redirect all traffic to HTTPS
server {
  listen 80;
  listen [::]:80 default_server ipv6only==on;
  return 301 https://$host$request_uri;
}

# HTTPS - proxy all requests to Node app
server {
  # Enable Http/2
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server-name example.com;

 # USE LETSENCRYPT CERTIFICATES
 ssl_certificate /etc..... .pem;
 ssl_certificate /etc..... .pem;

 # INCLUDE THE SSL CONFIG FROM CIPHERLIST.ST
 include snippets/ssl-params.conf;

 location /api/ {
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header Host $host;
   proxy_pass http://localhost:5000/;
}

 location / {
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Nginx-Proxy true;
   proxy_set_header Host $host;
   proxy_pass http://localhost:8080;
   proxy_ssl_session_reuse off;
  }
}

下面是来自prerender github的代码片段:Nginx/Prerender snippet
我的问题是,我是将这些片段部分拼接到现有的块中,还是添加额外的http和服务器块。我是新手,当涉及到服务器和配置Nginx。

6qqygrtg

6qqygrtg1#

您可以尝试prerender.io在现有的Nginx配置中包含www.example.com作为某些路由的中间件。可以为要预渲染的管线添加位置块。这可以在您现有的服务器块中完成。在您的情况下,您可能希望预呈现用于SEO目的的URL。我觉得这样也行,

location /seo-page {
    # Specify the Prerender.io server URL
    set $prerender "http://your-prerender-server-url";

    if ($args ~ "_escaped_fragment_") {
        set $prerender "${prerender}${uri}";
    }

    # Check if a user agent should be ignored (e.g., crawlers and bots)
    if ($http_user_agent ~* "googlebot|bingbot|yahoo|baiduspider|yandexbot") {
        set $prerender 0;
    }

    # If $prerender is set, proxy to Prerender.io
    if ($prerender) {
        proxy_set_header X-Prerender-Token "your-prerender-token";
        proxy_pass $prerender;
    }

    # If not, proxy to your Vue.js app
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Nginx-Proxy true;
    proxy_set_header Host $host;
    proxy_pass http://localhost:8080;
    proxy_ssl_session_reuse off;
}

相关问题