nginx reverse proxy using upstream directive - nginx

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?

Related

NGINX proxy_pass to defined upstream instead of https url directly

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;
}
}
}

Why Nginx HTTP forward proxy does not work?

I have 2 ways of doing a forward proxy in nginx.
My problem is that only one of them works. The second one does not work.
This one works
stream {
server {
listen 8200;
proxy_pass 127.0.0.1:8000;
}
}
This one does not work
http{
server {
listen 8200;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
}
My question is why the second method does not work for me?

configure NGINX as a simple 2 ARM load balancer

I would like to configure NGINX as a simple 2 ARM load balancer. This is the target scenario:
I have tried this configuration:
http {
upstream backend1 {
server 192.168.1.3;
server 192.168.1.2;
}
server {
listen 80;
location / {
proxy_pass http://backend1;
}
}
}
but it is not working. What am I doing wrong?
http block redefined in default.conf, you could just keep server block in default.conf and move upstream to http block defined in /etc/nginx/nginx.conf
Edit /etc/nginx/site-enabled/default.conf, just keep the server block
server {
listen 80;
location / {
proxy_pass http://backend1;
}
}
Edit /etc/nginx/nginx.conf, insert your upstream configure
http {
...
// insert upstream before the following two `include` commands
upstream backend1 {
server 192.168.1.3;
server 192.168.1.2;
}
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Restart nginx systemctl restart nginx to make your changes take effect.

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?

Nginx load balancing does not forward correctly

Nginx forwards call in a wrong way when configured with upstream!
Not working
upstream search {
server some.server.com;
}
server {
listen 80;
location / {
proxy_pass http://search;
}
}
Working good
upstream search {
server some.server.com;
}
server {
listen 80;
location / {
proxy_pass http://some.server.com;
}
}
When configured with upstream - target server returns "404 - Resource not found"
What do I do wrong?
Problem was in 'HOST' header
in case of upstream header 'HOST' is set to search.
To fix that you need to replace 'HOST' header in request
location / {
proxy_set_header Host some.server.com;
proxy_pass http://search;
}

Resources