0

I very appreciated if someone can give me some help. I just tried to learn the nginx reverse proxy but have experienced some problem about upstream name syntax.

Please refer to this photo Photo: nginx reverse proxy server structure

When I curl 192.168.189.140, the proxy server shows

  1. [debug] vhost.c(811): [client 192.168.189.140] [strict] Invalid host name 'bbs_server_pools', problem near: _serve

  2. [debug] vhost.c(902): [client 192.168.189.140] Client sent malformed Host header: bbs_server_pools

After I changed upstream name bbs_server_pools to bbs, it works all good. Is this something to do with the version of nginx or something else?

  1. error Nginx.conf file

    worker_processes  1;
    error_log logs/error.log error;
    
    events {
    worker_connections  1024;
    }
    
    http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format main '$remote_addr - $remote_user [$time_local] "$request"'
                    '$status $body_bytes_sent "$http_referer"'
                    '"$http_user_agent" "$http_x_forwarded_for"';
    
    server {
    listen       80;
    server_name  bbs.etiantian.org;
    location / {
         proxy_pass http://bbs_server_pools;   
       } 
    }    
    
    upstream bbs_server_pools {
    server 192.168.189.137:80;
    server 192.168.189.138:80 weight=3;
    }  
    
    
    }
    
  2. good nginx.conf file after changing bbs_server_pools to bbs

    worker_processes  1;
    error_log logs/error.log error;
    
    events {
    worker_connections  1024;
    }
    
    http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format main '$remote_addr - $remote_user [$time_local] "$request"'
                    '$status $body_bytes_sent "$http_referer"'
                    '"$http_user_agent" "$http_x_forwarded_for"';
    
    server {
    listen       80;
    server_name  bbs.etiantian.org;
    location / {
         proxy_pass http://bbs;   
       } 
    }    
    
    upstream bbs {
    server 192.168.189.137:80;
    server 192.168.189.138:80 weight=3;
    }  
    
    
    }
    
Alex
  • 3
  • 2

1 Answers1

0

This error has nothing to do with the nginx itself (well, almost nothing). It is returned by your upstream backend. The reason is that two HTTP request headers including the Host one are always gets redefined unless some other behavior specified explicitly using the proxy_set_header directive:

By default, only two fields are redefined:

proxy_set_header Host $proxy_host;
proxy_set_header Connection close;

The Host header value of the forwarded request will be the bbs_server_pools with your first configuration and bbs with the second one. However valid characters for hostnames are ASCII letters from a to z, the digits from 0 to 9, and the hyphen (-). While underscore is a valid character for the nginx upstream name, it isn't valid for the hostname, and that is the cause of the error you get from your upstream. Usually it is a good idea to keep the Host header value from the original request using

proxy_set_header Host $host;

(or $http_host, you can check the difference here). However there can be cases when you should not do it; check this answer to find out more information on this subject.

Ivan Shatsky
  • 2,360
  • 2
  • 6
  • 17
  • Hi Ivan! Thank you very much! Your answer solved my problem altogther – Alex Jun 01 '22 at 08:41
  • The ServerFault way of thanking for the working answer is described [here](https://serverfault.com/tour#:~:text=The%20person%20who%20asked%20can%20mark%20one%20answer%20as%20%22accepted%22.) :) – Ivan Shatsky Jun 01 '22 at 10:59