本文介绍了无法使用Nginx + Prerender + Meteor设置SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

使用HTTPS时,我无法将nginx配置为返回预渲染的html.

I am having trouble configuring nginx to return the prerendered html when using HTTPS.

  • nginx,prerender和我的流星应用程序在同一服务器上运行.
  • prerender位于端口3033
  • 流星应用程序位于端口112

在流星中,我已将其配置为指向localhost:3033进行预渲染.

In meteor I have configured it to to point to the localhost:3033 for prerendering.

使用以下 no-SSL 配置,Facebook的工具能够成功抓取我的网站:

With the following no-SSL configuration, Facebook's tool is able to scrape my site successfully:

server {
    listen 80;
    server_name sample.com www.sample.com;

    # strip the "www" subdomain
    if ($host ~* ^www\.(.*)) {
        set $host_without_www $1;
        rewrite ^(.*) http://$host_without_www$1 permanent;
    }

    location / {
        # app is running in port 112 in the same server
        proxy_pass http://127.0.0.1:112;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

但是,当我开始使用SSL时,Facebook无法刮取该网站.

However when I started using SSL Facebook is not able to scrape the site.

server {
    listen 443 ssl;
    server_name sample.com www.sample.com;

    ssl_certificate /etc/letsencrypt/live/sample.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/sample.com/privkey.pem;

    # strip the "www" subdomain
    if ($host ~* ^www\.(.*)) {
        set $host_without_www $1;
        rewrite ^(.*) http://$host_without_www$1 permanent;
    }

    location ~ /.well-known {
        allow all;
    }
    location / {
        # app is running in port 112 in the same server
        proxy_pass http://127.0.0.1:112;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
# redirect to https
server {
    listen 80;
    server_name sample.com www.sample.com;
    return 301 https://$host$request_uri;
}

一个观察结果是,每当我使用HTTPS运行FB工具时说https://sample.com/,在预渲染日志中都说得到http://sample.com(不是HTTPS!).

One observation is that every time I run the FB tool with an HTTPS say https://sample.com/, in prerender logs it says getting http://sample.com (not HTTPS!).

手动运行curl命令成功

curl http://sample.com:3033/https://sample.com

介于两者之间的某个位置显然可以将协议https转换为http.

Somewhere in between is clearly converting the protocol https to http.

推荐答案

看来我的nginx配置毕竟还不错.

It seems that my nginx configuration was fine after all.

我最终遵循了代码此处.

  1. 我从npm添加了 prerender-node

meteor npm install --save prerender-node

我创建了/client/prerender-head.html

<head><meta name="fragment" content="!"></head>

我创建了/server/prerender.js

请注意我们强制协议为https的部分(类似于@ Prerender.io建议的内容)

Note the part that we are forcing the protocol to be https (similar to what @Prerender.io suggested)

var prerenderio = Npm.require('prerender-node');
var token;
var serviceUrl;
var settings = Meteor.settings.PrerenderIO;

// token
token = process.env.PRERENDERIO_TOKEN || (settings && settings.token);

// service url (support `prerenderServiceUrl` (for historical reasons) and `serviceUrl`)
serviceUrl = settings && (settings.prerenderServiceUrl || settings.serviceUrl);
serviceUrl = process.env.PRERENDERIO_SERVICE_URL || serviceUrl;

if (token) {
  if (serviceUrl) prerenderio.set('prerenderServiceUrl', serviceUrl);
  prerenderio.set('prerenderToken', token);

  prerenderio.set('afterRender', function afterRender(error) {
    if (error) {
      console.log('prerenderio error', error); // eslint-disable-line no-console
      return;
    }
  });

  prerenderio.set('protocol', 'https');

  WebApp.rawConnectHandlers.use(prerenderio);
}
  1. 创建了/settings.json

将serviceUrl更改为运行预渲染实例的任何位置.

Change the serviceUrl to wherever your prerender instance is running.

{
  "PrerenderIO": {
      "serviceUrl": "http://localhost:3033/",
      "token": "yourtoken"
  }
}
  1. 使用meteor --settings settings.json
  2. 运行应用
  1. run the app with meteor --settings settings.json

这篇关于无法使用Nginx + Prerender + Meteor设置SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 02:37