Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

网站迁移到cloudflare踩坑 #43

Open
myml opened this issue Aug 21, 2024 · 0 comments
Open

网站迁移到cloudflare踩坑 #43

myml opened this issue Aug 21, 2024 · 0 comments

Comments

@myml
Copy link
Owner

myml commented Aug 21, 2024

nginx allow指令失效

部分网站在nginx通过allowdeny指令配置了ip白名单,在迁移到cloudflareremote_addr全部是从cloudflare回源的ip, 导致原来的配置失效。使用 real_ip_header X-Forwarded-For; 让 nginx 基于 X-Forwarded-For 头信息做ip匹配,但黑客自己也可以在http header 添加 X-Forwarded-For,这样就失去了白名单的作用,所以还需要使用 set_real_ip_from 信任哪些来源传递的 X-Forwarded-For 头信息。

整体配置如下,在allow前面加上。

real_ip_header X-Forwarded-For;
# 信任cloudflare 回源 IP
# https://www.cloudflare.com/ips-v4
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;
allow x.x.x;
deny all;

调用 minio StatObject 报错 Access denied

cloudflare 的缓存机制会将HEAD请求转为GET请求,这是导致 minio 报错的主要原因。可以在服务容器给 minio 服务添加 hosts, 调用 minio 时直接回原不经过 cloudflare。

还有一个解决办法是禁止对minio操作的缓存,但需要注意 cloudflare 对图片格式好像有着特殊处理,无论是用缓存规则的绕过,还是页面规则的缓存等级。 jpg png 等图片后缀的文件都会出现 HEAD 请求转为 GET 请求的问题,去掉对象存储key末尾的后缀即可。

如果因为一些原因不能去掉,可以在后缀后面再追加查询字符串,例如ex.jpg?skip_cache,这样的名字可以避免cloudflare的特殊处理。

cloudflare 概述页面有个基础功能一键开启

图片

其中的浏览器完整性检查会影响跨主机的API调用,因为API调用会被认为是非完整浏览器,如果服务部署在不同机器,互相通过域名调用,需要关闭这个选项 。

Hotlink 保护即防盗链功能,但 cloudflare 无法配置防盗链白名单,如果多个站点之间有资源引用,不建议开启。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant