Nginx proxy_pass 通过外部服务器代理到同一台机器时随机出现超时错误

Nginx proxy_pass 通过外部服务器代理到同一台机器时随机出现超时错误

免责声明-我已经在 cloudflare 论坛上问过这个问题,但经过一番挖掘后,我意识到这更多的是一个 nginx 问题。

我有example.com一个通过 运行的域cloudflare,并且位于12.34.56.78 DNS:

记录类型 姓名 内容 代理
A 示例.com 12.34.56.78 是的
A * 12.34.56.78 是的

我也app1.com不是通过cloudflare运行,也位于12.34.56.78

记录类型 姓名 内容
A app1.com 12.34.56.78
A www.app1.com 12.34.56.78

我正在尝试通过 proxy_passapp1.comapp1.example.com获取这个nginx块:

# minimized version
server {
    listen 443 ssl http2;
    server_name app1.com www.app1.com;
    
    location /{
        proxy_pass app1.example.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Ssl on;
        proxy_ssl_server_name on;
    }
    ssl_certificate /usr/local/directadmin/data/users/deploy/domains/app1.com.cert.combined;
    ssl_certificate_key /usr/local/directadmin/data/users/deploy/domains/app1.com.key;
}

现在,如果我删除 cloudflare 代理选项,app1.com 就可以正常工作,不会出现任何超时错误。但如果我使用 cloudflare 代理,我会随机收到超时错误,如下所示

我的发现:

  • 如果我将 app1.com 定向到不同的服务器,比如说,111.222.333.444然后使用完全相同的 nginx 块,那么它就不会出现超时错误 - 只有当它们都在同一台服务器上时才会发生超时错误。所以我猜想这与它的循环有关:

app1.com --> 12.34.56.78 --> app1.example.com (因此是 cloudflare 服务器) ---> 12.34.56.78 (返回原始服务器)

  • 如果我删除,proxy_ssl_server_name on;我会收到错误 421/403
  • 当然,如果我从 cloudflare 中删除代理选项,那么它也可以正常运行。
  • 我可以通过添加始终重试失败的请求来减轻加载时间,proxy_connect_timeout 3s;从而迫使它们更快地重试。

我考虑过使用upstream,但是因为我使用的是 Passenger,所以我实际上没有私有 IP + 端口,例如.com:

# minimized version
server{
    listen 443 ssl http2;
    server_name example.com *.example.com;
    
    passenger_ruby /home/deploy/.rbenv/shims/ruby;
    passenger_enabled on;
    rails_env production;


    ssl_certificate /usr/local/directadmin/data/users/deploy/domains/example.com.cert.combined;
    ssl_certificate_key /usr/local/directadmin/data/users/deploy/domains/example.com.key;
}
  • 注意每次不同的请求都会发生错误

总结:proxy_pass当 app1.com 和 app1.example.com(使用 cloudflare 代理)位于同一台服务器上时,我该如何使用它将 app1.com 定向到 app1.example.com?谢谢

2024/04/04 21:24:43 [error] 885944#0: *17 upstream timed out (110: Connection timed out) while connecting to upstream, client: <my_home_public_ip>, server: app1.com, request: "GET /assets/website-theme/transparent.png HTTP/2.0", upstream: "https://[2a06:98c1:3121::7]:443/assets/website-theme/transparent.png", host: "app1.com", referrer: "https://app1.com/"

2024/04/04 21:24:43 [error] 885944#0: *17 upstream timed out (110: Connection timed out) while connecting to upstream, client: <my_home_public_ip>, server: app1.com, request: "GET /cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js HTTP/2.0", upstream: "https://[2a06:98c1:3120::7]:443/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js", host: "app1.com", referrer: "https://app1.com/"

2024/04/04 22:21:54 [error] 890667#0: *633 upstream timed out (110: Connection timed out) while connecting to upstream, client: <my_home_public_ip>, server: app1.com, request: "GET /assets/website-theme/custom-colors/colour-blue-dark.min.css HTTP/2.0", upstream: "https://[2a06:98c1:3120::7]:443/assets/website-theme/custom-colors/colour-blue-dark.min.css", host: "app1.com", referrer: "https://app1.com/"

在此处输入图片描述

相关内容