1

I've configured nginx as a front-end load-balancer across three nodes of a web application I've constructed. nginx continually returns 400/bad request - invalid hostname errors regardless of the values i use in upstream.server and server.server_name. I've tried localhost and 127.0.0.1 for both of those values and issued requests using matching cURL/Postman requests to no avail.

I've also tried setting the value for server.server_name including the port number to better match the incoming HTTP HOST header to no avail.

nginx.conf

events {
  worker_connections  1024; 
}

http { 
  upstream myapp {
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
    server 127.0.0.1:8003;
  }

  server {
    listen 8000;
    server_name 127.0.0.1;
    location / {
      proxy_pass http://myapp;
    }
  }
}

cURL requests result in the following (no difference between using localhost and 127.0.0.1).

C:\>curl -v http://127.0.0.1:8000/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Server: nginx/1.17.1
< Date: Mon, 22 Jul 2019 14:29:22 GMT
< Content-Type: text/html; charset=us-ascii
< Content-Length: 334
< Connection: keep-alive
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>
</BODY></HTML>
* Connection #0 to host 127.0.0.1 left intact
11
  • That 400 error comes from IIS, not from nginx. – Michael Hampton Jul 22 '19 at 15:32
  • I don't think that's the case for two reasons. 1) I'm not using IIS. 2) the server header in the response shows 'nginx' < HTTP/1.1 400 Bad Request < Server: nginx/1.17.1 – joelc Jul 22 '19 at 15:34
  • Exactly what are you using? And how did a message that IIS generates come to appear there, if you aren't using IIS? – Michael Hampton Jul 22 '19 at 15:36
  • Using HttpListener/http.sys on Windows. Is there something in the message you see that indicates that it's coming from IIS? I can see how http.sys could produce a message similar to IIS, but because of the server header I'm of the opinion that it is nginx generating it. Cheers – joelc Jul 22 '19 at 15:39
  • 1
    In the absence of a proxy_set_header Host statement, the Host header is set to "myapp". – Richard Smith Jul 22 '19 at 15:58
1

Try with this configuration:

events {
  worker_connections  1024; 
}

http { 
  upstream myapp {
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
    server 127.0.0.1:8003;
  }

  server {
    listen 8000;
    server_name 127.0.0.1;
    location / {
      proxy_pass http://myapp;
      proxy_set_header Host $host;
    }
  }
}
3
  • Trying to hijack Michael's points? :) – joelc Jul 23 '19 at 15:03
  • @joelc Yes and No. Actually, it's just a right to the point answer for future reference if somebody googles it. :) – Daniel Gordi Jul 24 '19 at 12:01
  • Awesome thanks Daniel - have a good one! – joelc Jul 24 '19 at 16:27
0

I received the same error, but I am using nginx in front of IIS. But it was intermittent. It was every other request that failed which was odd.

Turns out I had bound my IIS hostname to a specific IP (as opposed to all IPs) and IIS was load balancing but failing to resolve the other IP.

Telling IIS to bind to all IPs resolved it. Only took a couple hours to figure that out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.