1

I have 1 main IP, and one secondary IP. The secondary IP is meant for a special Go webserver that listens only on this IP for a reason that is irrelevant to this question.

Unfortunately it doesn't work because Nginx decided to listen on all IPs and so my custom webserver cannot bind to that IP and port. I can't use a different port, it has to be this port... that's why I bought a second IP address, for this exclusive purpose.

How do I make Nginx only listen on the main IP instead of on all IPs?

I'm on Ubuntu 18.04.

Alasdair
  • 111
  • 1
  • 1
  • 6

1 Answers1

2

Here is a example config for http only. Https doesn't really differ:

server {
  #This is the important listen block to specify:
  listen      <ip-address>:80;
  server_name example.com;
  root        /var/www/html;
  index index.html index.php;

  location / { 
    try_files $uri $uri.html $uri/ /index.html;
  }

  error_page 404 /404.html;
}

If you have this in your config, you can remove it to disable ipv6 if you wish to do so:

listen [::]:80 default_server

If you then test it with netstat:

test@computer:/etc/nginx/conf.d# netstat -na | grep -i "Listen" | grep ":80"
tcp        0      0 <ip-address>:80       0.0.0.0:*               LISTEN  
Lorem ipsum
  • 892
  • 5
  • 15
  • 1
    Why do you say to not listen on IPv6? This will cripple the web server and make it not respond on IPv6, but there is no good reason for this. – Michael Hampton Aug 25 '20 at 16:17
  • As far as is understood the question he wanted to make nginx listen only on two ipv4 addresses he wants to specify. That's why i included it in the answer. I added a note to my answer. – Lorem ipsum Aug 25 '20 at 16:22