Forum: NGINX Set up reverse proxy and loadbalancing without hostname

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
De7b680154f831d87d8ea48743852f14?d=identicon&s=25 bai030805 (Guest)
on 2016-07-09 17:24
(Received via mailing list)
Hi Gurus

My lab environment is

Nginx IP: 192.168.16.206
Four Web Server: 192.168.16.201-204

My nginx.conf is

http {
    upstream myapp1 {
        server 192.168.16.201;
        server 192.168.16.202;
        server 192.168.16.203;
        server 192.168.16.204;
    }
    server {
        listen 80;
        location / {
            proxy_pass http://myapp1;
        }
    }
}

from web brower, i use http://192.168.16.206 to access the web server.
the
web brower redirect "http://192.168.16.206" to "https://myapp1/"
then i got error "myapp1’s server DNS address could not be found."
error.

Could you please give me some suggestions about this? thanks so much for
your feedback.

Posted at Nginx Forum:
https://forum.nginx.org/read.php?2,268209,268209#msg-268209
62ff150ef4a0b0bc0c2f89db62a20685?d=identicon&s=25 Dewangga Alam (Guest)
on 2016-07-09 17:28
(Received via mailing list)
Hello!

On 7/9/2016 10:23 PM, bai030805 wrote:
>     upstream myapp1 {
>     }
> }

Have you tried using `proxy_redirect off;` under your proxy_pass
configuration?
36a8284995fa0fb82e6aa2bede32adac?d=identicon&s=25 Francis Daly (Guest)
on 2016-07-09 18:27
(Received via mailing list)
On Sat, Jul 09, 2016 at 11:23:41AM -0400, bai030805 wrote:

Hi there,

> Nginx IP: 192.168.16.206
> Four Web Server: 192.168.16.201-204

Is there one Host: header that you can send in requests to each of the
four web servers, so that they will all return the content that you
want?

If so, use that. If not, send none. That is...

>         location / {
>             proxy_pass http://myapp1;

nginx will make a request of the upstream server including "Host:
myapp1".

You can change that by using "proxy_set_header Host" with your preferred
name. (Or you can use that name instead of "myapp1" here, and in the
"upstream" definition.)

Possibly

  proxy_set_header Host "";

is what you want here.

Depending on what your upstream servers send, you may need more config
in nginx to get everything to work the way that you want it to.

>         }

> }
>
> from web brower, i use http://192.168.16.206 to access the web server. the
> web brower redirect "http://192.168.16.206" to "https://myapp1/"

If there really is a switch from http to https, that suggests that
something extra is happening.

What response do you get from

  curl -v -H Host:myapp1 http://192.168.16.201/

? Because that is more-or-less the request than nginx makes.

Good luck with it,

  f
--
Francis Daly        francis@daoine.org
187c77fbd13c54f990b63d0df95a6cb1?d=identicon&s=25 Pratyush Kumar (Guest)
on 2016-07-09 19:53
(Received via mailing list)
Hi there

try this

http {
 upstream myapp1 {
 server 192.168.16.201;
 server 192.168.16.202;
 server 192.168.16.203;
 server 192.168.16.204;
 }
 server {
 listen 80;
 location / {
 proxy_pass http://myapp1/;
 proxy_set_header Host $host;
 #proxy_redirect https://myapp1/ http://$host/;
 }
 }
}

Basically "proxy_set_header Host $host;" is passing the host name as
seen by nginx to the upstream servers, so any redirect originating at
upstream will redirect to the same host which it was called with.
without this statement upstream servers sees host name as myapp1 which
nginx uses to request them.

If this one doesn't work as expected you might like to un-comment
"#proxy_redirect https://myapp1 (https://myapp1/) http://$host/;" line,
although in my opinion it wont be necessary.
Regards
Pratyush Kumar
http://erpratyush.me (http://erpratyush.me)
live and let live go vegan

July 9 2016 9:56 PM, "Francis Daly"  wrote:On Sat, Jul 09, 2016 at
11:23:41AM -0400, bai030805 wrote:

Hi there,
 Nginx IP: 192.168.16.206
Four Web Server: 192.168.16.201-204

Is there one Host: header that you can send in requests to each of the
four web servers, so that they will all return the content that you
want?

If so, use that. If not, send none. That is...
 location / {
proxy_pass http://myapp1 (http://myapp1);

nginx will make a request of the upstream server including "Host:
myapp1".

You can change that by using "proxy_set_header Host" with your preferred
name. (Or you can use that name instead of "myapp1" here, and in the
"upstream" definition.)

Possibly

proxy_set_header Host "";

is what you want here.

Depending on what your upstream servers send, you may need more config
in nginx to get everything to work the way that you want it to.
 }

}

from web brower, i use http://192.168.16.206 to access the web server.
the
web brower redirect "http://192.168.16.206" to "https://myapp1
(https://myapp1)"

If there really is a switch from http to https, that suggests that
something extra is happening.

What response do you get from

curl -v -H Host:myapp1 http://192.168.16.201/

? Because that is more-or-less the request than nginx makes.

Good luck with it,

f
--
Francis Daly francis@daoine.org (mailto:francis@daoine.org)
De7b680154f831d87d8ea48743852f14?d=identicon&s=25 bai030805 (Guest)
on 2016-07-10 16:41
(Received via mailing list)
Hi All

thanks so much for your help!!!

Posted at Nginx Forum:
https://forum.nginx.org/read.php?2,268209,268228#msg-268228
This topic is locked and can not be replied to.