suziwen / markdownxiaoshujiang

小书匠是一款本地优先,去中心化,分布式,支持选择性同步的全平台覆盖知识管理笔记软件。
http://soft.xiaoshujiang.com
1.52k stars 196 forks source link

[wiki]小书匠WEB端配置自定义couchdb时候如何解决跨域问题(涉及http和https两种场景) #1534

Open lqdflying opened 3 years ago

lqdflying commented 3 years ago

WEB端自定义couchdb同时失败,本地PC客户端同步正常 image

suziwen commented 3 years ago

可以在浏览器里按 F12 ,然后切换到 网络/network 标签, 再点击同步,就可以看到具体报什么错误了。

一般客户端可以用,网页端出错的,基本就是跨域访问的问题。

如果你是直接访问 couchdb 端口的话,需要自己在 couchdb 配置文件上修改,可以参考官方文档。或者自己到 couchdb 后台服务器 http://xxx.xxx.xxx.xxx:5984/_utils 进行修改设置

[httpd]
enable_cors = true

[cors]
origins = *

如果你是通过 apache 或者 nginx 等 http 服务器反向代理的话,可能这些 http 服务器需要另外单独配置 cors.

lqdflying commented 3 years ago

配置确认已经修改,以下是F12的报错: image

lqdflying commented 3 years ago

找到问题了,把https://markdown.xiaoshujiang.com/换成http://markdown.xiaoshujiang.com/就好了,两个协议浏览器不让互访,除非conchdb前边配置nginx+https也行,这样vps就必须要在国外,不能在国内了,因为得备案

lqdflying commented 3 years ago

再次补充,我配置了Apache2https反向代理,同时开启了cors,结果再次报错,我F12打开console提示

The 'Access-Control-Allow-Origin' header contains multiple values'*', but only one is allowed

终于发现我过量配置了,解决办法:

注意: 如果用Apache2或者Nginxreverse proxy,这里正常配置即可,不需要二次开启cors 因为conchdb中的[httpd]已经开启了一次了,这里不需要重复开启 前置Apache2conchdb中的cors保留一个即可

lqdflying commented 3 years ago

我把标题改成了wiki,来帮助有需要的人

lqdflying commented 3 years ago

po一组测试过的配置: couchdb+apache2, debian10

[httpd] enable_cors = true

- apache2.vh.config
```config
<IfModule mod_ssl.c>
    <VirtualHost *:80>
        ServerName couchdb.your_domain
        RewriteEngine on
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [L,R]
    </VirtualHost>
    <VirtualHost *:443>
        DocumentRoot /var/www/html
        ServerName couchdb.your_domain
        SSLEngine on
        SSLCertificateFile /etc/apache2/cert/your_cert.crt
        SSLCertificateKeyFile /etc/apache2/cert/your_cert.key
        Header add Access-Control-Allow-Headers "accept, authorization, content-type, origin, referer"
        Header add Access-Control-Allow-Methods "GET, PUT, POST, HEAD, DELETE"
        ProxyPreserveHost On
        ProxyPass / http://localhost:5984/
        ProxyPassReverse / http://localhost:5984/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
</IfModule>
suziwen commented 3 years ago

感谢分享 :+1: