Home > OS >  Nginx windows load balancing reverse proxy: Bad Request - Invalid Hostname
Nginx windows load balancing reverse proxy: Bad Request - Invalid Hostname

Time:07-27

Using Nginx on windows to load balance and reverse proxy. Trying to get load balancing working on local machine. I have two applications running on https://localhost:44308/ and https://localhost:44309/.

But I get the following error:

Bad Request - Invalid Hostname

Here is my nginx.conf:

events {
 worker_connections  1024;
}


http {
  include       mime.types;
  default_type  application/octet-stream;
  keepalive_timeout  65;

  upstream samplecluster {
    server localhost:44308;
    server localhost:44309;
  }

  server {
    listen       8070;
    server_name  example.com;
    
    location /api/values {
        proxy_pass https://samplecluster;
    }
  }

So now when I try to access http://example.com:8070/api/values, I get the error.

It works fine when not using the load balancer.

location /api/values {
        proxy_pass https://localhost:44308;
    }

Note: 127.0.0.1->example.com in host file

CodePudding user response:

It seems the error is coming from your services running on ports 44308 and 44309. The problem likely is they accept only a configured set of hosts, and localhost is one (if not the only one) that works.

So you simply need to set the Host header to pass it through:

location /api/values {
    proxy_set_header Host localhost;
    proxy_pass https://samplecluster;
}
  • Related