nginx 上游 proxy_pass 不适用于heroku?

发布于 2025-01-20 07:24:28 字数 999 浏览 2 评论 0原文

下面的nginx配置工作正常如果我硬码我的herokuapp(后端API)在proxy_pass部分中:

http { 
    server {
        listen 8080;
        
        location / {
            proxy_pass http://my-app.herokuapp.com;
        }
    }
}

events { }

但是,如果我尝试将其添加到上游指令中,则它将达到<<代码> 404 页面。我想在上游指令中添加它,因为我还有其他Herokuapp,我想加载我的请求平衡。

这是不起作用的配置:

http { 

    upstream backend {
        server my-app.herokuapp.com;
    }

    server {
        listen 8080;
        
        location / {
            proxy_pass http://backend;
        }
    }
}

events { }

这些是我在检查其他后尝试的所有内容,因此答案:

  • 在代理传递时添加主机标头。 proxy_set_header主机$ host;
  • 在后端结束时添加额外的斜线。
  • 在上游指令中,添加服务器my-app.herokuapp.com:80而不是server my-app.herokuapp.com
  • 在上游指令中,添加server my my my my my -app.herokuapp.com:443而不是服务器my-app.herokuapp.com。这可能是因为Heroku不允许443(或者我没有配置)。

The below nginx config is working fine if I hardcode my herokuapp(backend API) in proxy_pass section:

http { 
    server {
        listen 8080;
        
        location / {
            proxy_pass http://my-app.herokuapp.com;
        }
    }
}

events { }

However if I try to add this in the upstream directive, its going to 404 page. I want to add this in upstream directive because I have other herokuapps as well where I want to load balance my requests.

This is the config which is not working:

http { 

    upstream backend {
        server my-app.herokuapp.com;
    }

    server {
        listen 8080;
        
        location / {
            proxy_pass http://backend;
        }
    }
}

events { }

These are all the things I tried after checking other SO answers:

  • add Host header while proxy passing. proxy_set_header Host $host;
  • add an extra slash at the end of backend.
  • In upstream directive, add server my-app.herokuapp.com:80 instead of just server my-app.herokuapp.com
  • In upstream directive, add server my-app.herokuapp.com:443 instead of just server my-app.herokuapp.com. This gives timeout probably because heroku doesn't allow 443(or maybe I didn't configure it).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几度春秋 2025-01-27 07:24:28

发现问题:我添加了错误的主机。对于heroku,由于某种原因,您需要添加主机标头,其值与您的应用程序名称完全相同。

如果您的herokuapp名称是my-app.herokuapp.com,那么您需要添加这一行:

proxy_set_header Host my-app.herokuapp.com;

下面是完整的工作配置:

http { 

    upstream backend {
        server my-app.herokuapp.com;
    }

    server {
        listen 8080;
        
        location / {
            proxy_pass http://backend;
            proxy_set_header Host my-app.herokuapp.com;
        }
    }
}

events { }

Found the Issue: I was adding the wrong host. For heroku, for some reason you need to add host header with value as exactly what your app name is.

If your herokuapp name is my-app.herokuapp.com, then you need to add this line for sure:

proxy_set_header Host my-app.herokuapp.com;

Full working config below:

http { 

    upstream backend {
        server my-app.herokuapp.com;
    }

    server {
        listen 8080;
        
        location / {
            proxy_pass http://backend;
            proxy_set_header Host my-app.herokuapp.com;
        }
    }
}

events { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文