Nginx bind to external IP only (listen hostname:port)

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. :-)

1
задан 11 November 2018 в 23:54
1 ответ

Оказывается, проблема заключалась в том, что я оставил следующее в / etc / hosts

127.0.0.1 localhost foobar.example.com.

и уже забыл, как он туда попал. :-) Удаление foobar.example.com. псевдоним 127.0.0.1 заставил nginx, настроенный с помощью приведенного выше фрагмента, прослушивать внешний IP-адрес только так, как я ожидал.

0
ответ дан 4 December 2019 в 03:34

Теги

Похожие вопросы