nginx 上游 proxy_pass 不适用于heroku?
下面的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 justserver my-app.herokuapp.com
- In upstream directive, add
server my-app.herokuapp.com:443
instead of justserver my-app.herokuapp.com
. This gives timeout probably because heroku doesn't allow 443(or maybe I didn't configure it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题:我添加了错误的主机。对于heroku,由于某种原因,您需要添加主机标头,其值与您的应用程序名称完全相同。
如果您的herokuapp名称是
my-app.herokuapp.com
,那么您需要添加这一行:下面是完整的工作配置:
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:Full working config below: