nginx 400 Bad Request (Invalid Hostname)

8,891

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;
    }
  }
}
Share:
8,891

Related videos on Youtube

joelc
Author by

joelc

Updated on September 21, 2022

Comments

  • joelc
    joelc 3 months

    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
    
    • Michael Hampton
      Michael Hampton over 3 years
      That 400 error comes from IIS, not from nginx.
    • joelc
      joelc over 3 years
      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
    • Michael Hampton
      Michael Hampton over 3 years
      Exactly what are you using? And how did a message that IIS generates come to appear there, if you aren't using IIS?
    • joelc
      joelc over 3 years
      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
      joelc over 3 years
      By the way, when I do a request directed to one of the upstream nodes (localhost:8001 as an example) the request succeeds.
    • Michael Hampton
      Michael Hampton over 3 years
      Well, http.sys is close enough. You only see Server: nginx because the response comes through nginx, not from it. It is actually coming from the upstream servers. Now you know where it's coming from and where to look for the problem.
    • joelc
      joelc over 3 years
      Is there a way I can see what nginx is submitting on the back side so I can examine its host header? My app is responding correctly when the host header is set correctly. The request isn't getting far enough up the stack for my app to see it.
    • Richard Smith
      Richard Smith over 3 years
      In the absence of a proxy_set_header Host statement, the Host header is set to "myapp".
    • joelc
      joelc over 3 years
      You just solved my issue. Can you create a solution response/answer so I can accept it and get you the points you definitely deserve :) <3
    • joelc
      joelc over 3 years
      Also if you want to post the answer here I'll accept it: stackoverflow.com/questions/57148312/…
    • joelc
      joelc over 3 years
      Not that you need the points :)
  • joelc
    joelc over 3 years
    Trying to hijack Michael's points? :)
  • joelc
    joelc over 3 years
    Awesome thanks Daniel - have a good one!

Related