onlyoffice使用Https访问

开发服务器用的是http,一切正常使用,部署到服务器后,由于服务器使用了Https,导致访问onlyoffice时控制台报错。Mixed Content: The page at 'http://xxxxx//' was loaded over HTTPS, but requested an insecure frame 'http://xxxxx//'.

This request has been blocked; the content must be served over HTTPS.

主要原因是https链接中嵌套了http的资源导致报错,我的onlyoffice部署在docker容器中,如果配置证书什么的,看着还是挺麻烦的。

最简单的办法就是在网页中增加一句代码:

复制代码
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

我用的是官方给的nodejs源码,所以在views/editor.ejs中增加即可。

页面可以正常打开了,但查看网页源代码,发现callback等需要回调地址仍然是http,所以修改docManager.js修改getServerHost函数,我直接把协议名写死了:

复制代码
DocManager.prototype.getServerHost = function getServerHost() {
  return `https://${this.req.headers['x-forwarded-host'] || this.req.headers.host}`
    + `${this.req.headers['x-forwarded-prefix'] || ''}`;
};

但使用中发现问题,文档在线编辑后,没有将文档数据回写到word实体文件中,肯定是回调过程出现了问题,而且从日志中也发现了,onlyoffice在访问/track回写数据时,会自动传过来服务器的url,发现是http,所以修改app.js的这个位置:

复制代码
} else if (bodyTrack.status === 2 || bodyTrack.status === 3) { // MustSave, Corrupted

将返回来的bodyTrack变量修改一下:

复制代码
bodyTrack.url = bodyTrack.url.replace("http", "https");
if (bodyTrack.hasOwnProperty('changesurl')) {
    bodyTrack.changesurl = bodyTrack.changesurl.replace("http", "https");
}

由于我的文档存储路径为了较深的目录,目录层级比较多,为了让创建文档的时候自动创建多级目录,可以修改docManager.js的createDirectory:

复制代码
DocManager.prototype.createDirectory = function createDirectory(directory) {
  if (!this.existsSync(directory)) {
    // 创建多级目录,增加了{ recursive: true }参数
    fileSystem.mkdirSync(directory, { recursive: true });
    //fileSystem.mkdirSync(directory);
  }
};

参考:

This request has been blocked; the content must be served over HTTPS._onlyoffice this request has been blocked; the cont-CSDN博客

相关推荐
Xzq2105091 小时前
以太网协议 —— 数据链路层
服务器·网络·网络协议
xUxIAOrUIII1 小时前
【WebSocket】原理介绍
网络·websocket·网络协议
白太岁1 小时前
通信:(10) 应用层(第5层):http 与 https
网络协议·http·https
liulilittle2 小时前
OPENPPP2静态隧道UDP中断问题排查与解决
网络·网络协议·ubuntu·udp·debian·信息与通信·通信
会员果汁2 小时前
TCP/IP网络-网络接口层标准
网络·网络协议·tcp/ip
有浔则灵3 小时前
Go 语言 net/http 包详解:从入门到实战
http·golang·xcode
吠品3 小时前
告别异步等待!UniApp uni.getSystemInfoSync:即时获取设备信息的效率利器
https·ssl
铭哥的编程日记3 小时前
DNS解析 HTTP TCP/IP ICMP/NAT/NAPT相关知识点
网络协议·tcp/ip·http
小江的记录本4 小时前
【AOP】AOP-面向切面编程 (系统性知识体系全解)
java·前端·后端·python·网络协议·青少年编程·代理模式
安逸sgr4 小时前
MCP 协议深度解析(一):MCP 协议概览与架构设计
服务器·网络·人工智能·网络协议·agent·mcp