Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#681 closed enhancement (wontfix)

Pass custom Host in upstream module

Reported by: Aleš Kafka Owned by:
Priority: minor Milestone:
Component: nginx-module Version: 1.7.x
Keywords: upstream, host Cc:
uname -a:
nginx -V: nginx version: nginx/1.6.2
built by clang 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/Cellar/nginx-full/1.6.2 --with-http_ssl_module --with-pcre --with-ipv6 --sbin-path=/usr/local/Cellar/nginx-full/1.6.2/bin/nginx --with-cc-opt='-I/usr/local/include -I/usr/local/Cellar/pcre/8.35/include -I/usr/local/Cellar/openssl/1.0.1h/include' --with-ld-opt='-L/usr/local/lib -L/usr/local/Cellar/pcre/8.35/lib -L/usr/local/Cellar/openssl/1.0.1h/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-http_spdy_module --add-module=/usr/local/share/echo-nginx-module

Description

Currently, proxy_pass via upstream sends $host resolved in server_name or user-defined. This is usually good enough solution, but sometimes it's not, as I have experienced and I found other users having trouble with this too.

Use case is this:
You have multiple servers serving content. This could be VPS, dedicated servers, but also shared hostings. Problem is, that you can't configure shared hosting to server same content under two different hosts (original www.example.com and from proxy proxy-example.com). It routes to different folder. Now you want to use one proxy server to serve content from all those servers.

Solution is very easy:
extends server directive in upstream module to allow specifying host.

upstream example {
    server 1.2.3.4:80 host=www.example.com;
    server 1.2.3.3:80 host=www.example2.com;
}

when server 1.2.3.4:80 is chosen, it send header Host www.example.com; when server 1.2.3.3:80, it sends www.example2.com; if host is not specified, it sends Host resolved from server_name or specified with proxy_pass_header.

Change History (3)

comment:1 by Maxim Dounin, 6 years ago

Resolution: wontfix
Status: newclosed

In nginx, request to upstream is created only once, and then used regardless of which server was selected by the balancing method. If needed, the same request is reused in a request to another server as per proxy_next_upstream. There are no plans to change this logic.

If you want nginx to use different names in requests to different servers, consider using distinct upstream blocks and appropriate configuration.

comment:2 by Aleš Kafka, 6 years ago

I understand the logic. However, I would guess, that there is place where the request is sended to final server chosen by balancing method. And in this moment, there could be possibility to add extra header Host returned from balancing method together with chosen server. But that is just me guessing.

What do you mean by appropriate configuration? Can you give me some hint, which directive you think?

comment:3 by Maxim Dounin, 6 years ago

Exact configuration depends on your needs. E.g., you can select a backend server to use based on split_clients, or direct some parts of your site to one server and some to another using location blocks. The error_page directive can be used to configure fallback in case of errors, similar to what proxy_next_upstream does.

Note: See TracTickets for help on using tickets.