undefinedfix
Sign in

Configuring proxy with nginx_ set_ What is the role of header host $host?

DaPythonprogrammer edited in Fri, 15 Jul 2022

Nginx requests 400 to configure proxy after configuring upstream load balancing_ set_ Header host $host succeeded. What's the principle? Proxy_ set_ What's the use of header host $host?

Upstream configuration:

clipboard.png

Routing configuration: proxy was not configured at first_ set_ Header host $host and other parameters, the request will always report 400 (bad request)

clipboard.png

However, if load balancing is not configured, only one server can be configured

clipboard.png

2 Replies
jakab922
commented on Fri, 15 Jul 2022

proxy_ set_ The function of the header host $host line is to put the host field in the header of the original HTTP request into the forwarding request. If this line is not added, there will be no host field in the request header forwarded by nginx, and the server will use this host value to distinguish which domain name resource you are requesting.

cuongnm306
commented on Fri, 15 Jul 2022

The back-end server of the proxy can know the real domain name of the user through the host header. If it is not set, it will get the IP of the proxy service (nginx), so that the back-end server can return the correct URL in the page for the dynamically spliced URL