如何解决Nginx 代理 + CDN
我们正在使用运行 nginx 代理的虚拟机(缓存关闭)将主机重定向到正确的 CDN 网址。
我们在代理显示与 CDN 上显示的内容不匹配的旧内容时遇到问题。我们使用的 CDN 提供商是 Verizon(通过 Azure - Microsoft CDN 通过 Verizon)。
当我们对源进行更新时,我们会自动向 CDN 发送清除请求。这些可以是手动和自动动态更新,也可以是单个 url 清除和通配符。似乎正在发生的事情是当我们及时关闭 2 个 pruge 请求时。第一个通过代理,但第二个没有。虽然直接访问CDN url时两者都显示正确。
值得一提的是,此问题仅发生约 30%。
nginx 示例配置:
server {
resolver 8.8.8.8;
server_name <CUSTOM HOST>;
location / {
# Turn off all caching
proxy_no_cache 1;
proxy_cache_bypass 1;
proxy_redirect off;
expires -1;
proxy_cache off;
# Proxy settings
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_headers_hash_max_size 512;
proxy_headers_hash_bucket_size 128;
# Override cache headers to set 2min browser cache
proxy_hide_header Cache-Control;
add_header Cache-Control max-age=120;
proxy_pass "<CDN URL>request_uri";
}
}
nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile off;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
以下是 CDN 显示比代理更新的内容的示例,我们有一个 Last-Modifed 不匹配:
我已经尝试通过 VPN 来查看代理命中的特定 POP 是否有任何内容,但所有 POP:s 都显示正确的内容。
出现错误时,从代理向 CDN 发送 curl 请求将导致相同的错误标头。
在我们执行清除后,几个请求会直接传递到源,直到 CDN 再次开始提供缓存版本。
我们在大约 1 分钟后收到第一个 HIT。
我开始假设这可能与 Azure 和 Verizon 内部有关。 所以我在亚马逊上创建了一个完全重复的代理,但错误似乎很准确。 nginx 中是否还有其他原因会导致此行为?
解决方法
尝试添加只是为了查看是否每次点击都检索到页面。
add_header Last-Modified $date_gmt;
add_header Cache-Control 'no-store,no-cache,must-revalidate,proxy-revalidate,max-age=0';
if_modified_since off;
expires off;
etag off;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。