2

I'd like to specify an nginx server that listens only on external IP of an hostname given in listen directive. My server has domain name foobar.example.com which resolves to public IP 1.2.3.4.

When I configure nginx server like this

server {

    #listen 3330;
    listen foobar.example.com:3330;
    server_name foobar.example.com;

    location / {
        proxy_pass http://127.1.0.1:3330;
        proxy_set_header  Host               $host;
        proxy_set_header  X-Real-IP          $remote_addr;
        proxy_set_header  X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto  $scheme;
    }
}

it listens on 127.0.0.1:3330 though. Why is that? How can I make it to bind to 1.2.3.4:3330 instead without hardcoding the IP address in the config file?

# dig +noall +answer foobar.example.com
foobar.example.com.     2648    IN  A   1.2.3.4
# dig +noall +answer -x 1.2.3.4
4.3.2.1.in-addr.arpa. 2828  IN  PTR foobar.example.com.
# dig +noall +answer -x 127.0.0.1
1.0.0.127.in-addr.arpa. 0   IN  PTR foobar.example.com.
1.0.0.127.in-addr.arpa. 0   IN  PTR foobar.

Just FYI: When I use just listen 3330 and keep the rest unchanged, nginx listens on 0.0.0.0:3330 and, as you suspect, all hell breaks loose on first request to http://foobar.example.com:3330. :-)

woky
  • 245
  • 3
  • 10

1 Answers1

1

Turns out problem was that I've left the following in /etc/hosts

127.0.0.1 localhost foobar.example.com.

and I've already forgotten how it got there. :-) Removing foobar.example.com. alias of 127.0.0.1 made nginx configured with the snippet above listen on external IP only as I expected.

woky
  • 245
  • 3
  • 10