反向代理上的重定向循环

Redirect loop on reverse proxy

提问人:BackSlash 提问时间:11/13/2023 更新时间:11/13/2023 访问量:19

问:

我在同一台机器的不同端口上有两个 nginx 网站(我们称它们为 A、B),我正在尝试使用特定的主机标头将 A 代理给 B。该请求由 cloudflare worker 发起,因此需要修改主机。

场景是:

  • 用户打开my.website.com
  • Cloudflare 工作线程加载,但无法覆盖主机标头website-a.website.com:65112
  • nginx 处理对 webiste A 的请求
  • nginx 在端口 65113 上代理对网站 B 的请求,将主机标头改回 - 这是至关重要的,因为网站 B 需要主机标头是原始主机my.website.com

这是我目前的配置:

# website A
server {
  listen                  65112;
  client_max_body_size    10G;

  location / {
    proxy_pass            http://127.0.0.1:65113;
    proxy_redirect        off;
    proxy_set_header      Host my.website.com;
    proxy_read_timeout    1800;
    proxy_connect_timeout 1800;
    proxy_send_timeout    1800;
    send_timeout          1800;
  }
}

# website B
server {
  listen               65113;
  client_max_body_size 10G;
  index index.php;

  location / {
    root /var/www/html/nextcloud;
    try_files $uri /index.html index.php;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }
}

worker 代码非常简单:

export default {
  async fetch(request) {
    return fetch(
      `http://website-a.website.com:65112`,
      request,
    );
  },
};

我的期望:

  • 用户打开my.website.com
  • 工作负荷website-a.website.com:65112
  • nginx 反向代理到正确的标头127.0.0.1:65113Host
  • 网站 B 回答正确,网页显示

相反会发生什么:

  • 用户打开my.website.com
  • 有一个无限重定向循环my.website.com

如果我从网站 A 中删除该指令,则正确显示 nginx 默认页面,因此这让我认为问题出在代理本身上。proxy_passmy.website.com

有没有办法解决这个问题?

nginx nginx-reverse-proxy cloudflare-workers

评论


答: 暂无答案