Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx Reverse Proxy upstream not working

I'm having trouble figuring out load balancing on Nginx. I'm using: - Ubuntu 16.04 and - Nginx 1.10.0.

In short, when I pass my ip address directly into "proxy_pass", the proxy works:

server {
    location / {
            proxy_pass http://01.02.03.04;
    }
}

When I visit my proxy computer, I can see the content from the proxy ip... but when I use an upstream directive, it doesn't:

upstream backend {
     server 01.02.03.04;
}

server {
    location / {
            proxy_pass http://backend;
    }
}

When I visit my proxy computer, I am greeted with the default Nginx server page and not the content from the upstream ip address.

Any further assistance would be appreciated. I've done a ton of research but can't figure out why "upstream" is not working. I don't get any errors. It just doesn't proxy.

like image 219
Avery Smith Avatar asked Nov 14 '16 01:11

Avery Smith


1 Answers

Okay, looks like I found the answer...

two things about the backend servers, at least for the above scenario when using IP addressses:

  1. a port must be specified
  2. the port cannot be :80 (according to @karliwsn the port can be 80 it's just that the upstream servers cannot listen to the same port as the reverse proxy. I haven't tested it yet but it's good to note).

backend server block(s) should be configured as following:

server {

    # for your reverse_proxy, *do not* listen to port 80
    listen 8080;
    listen [::]:8080;

    server_name 01.02.03.04;

    # your other statements below
    ...
}

and your reverse proxy server block should be configured like below:

upstream backend {
    server 01.02.03.04:8080;
}

server {
    location / {
        proxy_pass http://backend;
     }
}

It looks as if a backend server is listening to :80, the reverse proxy server doesn't render it's content. I guess that makes sense, since the server is in fact using default port 80 for the general public.

Thanks @karliwson for nudging me to reconsider the port.

like image 174
Avery Smith Avatar answered Nov 14 '22 03:11

Avery Smith