NGINX proxy_pass to defined upstream instead of https url directly - nginx

I have an nginx config that looks similar to this (simplified):
http {
server {
listen 80 default_server;
location /api {
proxy_pass https://my-bff.azurewebsites.net;
proxy_ssl_server_name on;
}
}
}
Essentially, I have a reverse proxy to an API endpoint that uses https.
Now, I would like to convert this to an upstream group to gain access to keepalive and other features. So I tried this:
http {
upstream bff-app {
server my-bff.azurewebsites.net:443;
}
server {
listen 80 default_server;
location /api {
proxy_pass https:/bff-app;
proxy_ssl_server_name on;
}
}
}
Yet it doesn't work. Clearly I'm missing something.
In summary, how do I correctly do this "conversion" i.e. from url to defined upstream?
I have tried switching between http instead of https in the proxy_pass directive, but that didn't work either.
I was honestly expecting this to be a simple replacement. One upstream for another, but I'm doing something wrong it seems.

Richard Smith pointed me in the right direction.
Essentially, the issue was that the host header was being set to "bff-app" instead of "my-bff.azurewebsites.net" and this caused the remote server to close the connection.
Fixed by specifying header manually like below:
http {
upstream bff-app {
server my-bff.azurewebsites.net:443;
}
server {
listen 80 default_server;
location /api {
proxy_pass https:/bff-app;
proxy_ssl_server_name on;
# Manually set Host header to "my-bff.azurewebsites.net",
# otherwise it will default to "bff-app".
proxy_set_header Host my-bff.azurewebsites.net;
}
}
}

Related

nginx upstream proxy_pass not working for heroku?

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).
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 { }

nginx reverse proxy between 2 https servers

I'm a bit new to using nginx so I'm likely missing something obvious. I'm trying to create an nginx server that will reverse proxy to a set of web servers that use https.
I've been able to get it to work with one server list this:
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://<server1>.herokuapp.com;
}
}
However, as soon I try to add in the 'upstream' configuration element it no longer works.
upstream backend {
server <server1>.herokuapp.com;
}
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://backend;
}
}
I've tried adding in 443, but that also fails.
upstream backend {
server <server1>.herokuapp.com:443;
}
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://backend;
}
}
Any ideas what I'm doing wrong here?

Setting up Nginx - Nginx placing upstream name in URL

Why is nginx is nginx placing the upstream name in the redirected URL?
This is my nginx.conf:
worker_processes 1;
events {
worker_connections 1024;
}
http {
upstream servs {
server facebook.com;
}
server {
listen 80;
location / {
proxy_pass http://servs;
}
}
}
When I access the port 80, I get:
This site can’t be reached
servs.facebook.com’s server DNS address could not be found.
Why is it placing "servs." before facebook.com?
You are not setting the Host header in the upstream request, so nginx constructs a value from the proxy_pass directive. As you are using an upstream block, this value is the name of the upstream block, rather than the name of the server you are trying to access.
If you are using an upstream block, it may be advisable to set the Host header explicitly:
proxy_set_header Host example.com;
See this document for more.

How to get nginx host variable to work with proxy_pass

I am trying to setup an nginx proxy in front of any s3 website that I point at it. In the example below I have my DNS records customsite.com pointing to my proxy. When I don't use any variable for the destination of the proxy_pass there isn't an issue, but I want to be able to just dynamically pass the request using $host. How do I get the $host variable to behave with proxy_pass?
This Works
server {
listen 80;
location / {
add_header RequestedHost $host; # The host is returned as expected (customsite.com)
# go get it from s3
proxy_pass http://customsite.com.s3-website-us-east-1.amazonaws.com;
}
}
This Doesn't Work
server {
listen 80;
location / {
add_header RequestedHost $host;
# go get it from s3
proxy_pass http://$host.s3-website-us-east-1.amazonaws.com; # Doesn't resolve
}
}

nginx reverse proxy using upstream directive

I want to configure nginx to be a reverse proxy using upstream directive (and add there keepalive for example).
upstream my_backend {
server 127.0.0.1:3579;
}
server {
listen 80;
location / {
proxy_pass http://my_backend;
}
}
But the problem is that it returns Bad Request (Invalid host). And there is nothing in nginx error log to help me solve it.
Everything else being the same this configuration without upstream directive works as expected:
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:3579;
}
}
Aren't those two equivalent? And what do I have to do to make it work with upstream?

Resources