8

Since an update my nginx server is only available via IPv6 and not IPv4 while my config contained

listen   [::]:80 default_server;

Now I've changed this to

listen   [::]:80 default_server ipv6only=off;

and the server is available via IPv4 again. This seams to be due to the changes in nginx 1.3.4 (section “Changes with nginx 1.3.4”).

Since I have more than one server configuration on the same mashine I would like to set the ipv6only=off globally is there any possibility to do so?

white_gecko
  • 257
  • 1
  • 3
  • 11
  • It's not very clear in the answers here, but `ipv6only` now defaults to enabled if you are using the single-line listen directive in your question example. It's better to explicitly define a listen line for both IPv4 and another for IPv6, and the you don't need to worry about `ipv6only` at all (delete the phrase if exists). – Jesse Nickles Aug 01 '22 at 16:13

2 Answers2

11

I just went through this with an upgrade from 1.0 to 1.4.

Since only a single listener is actually bound to any given port, it's sufficient to specify ipv6only=off in any one of your listen directives.

So, in my default server blocks, I have:

    listen [::]:80 ipv6only=off default_server;

and

    listen [::]:443 ipv6only=off default_server;

All of the other server blocks merely specify the original listen directives. And it works; any virtual host is reachable via either IPv4 or IPv6.

You could also add it to all of them, but that's not really required. Adding it to any one of them is sufficient.

However, because the ipv6only= flag is Linux-only, I do not use this construct anymore. My current directives specify both IPv4 and IPv6 explicitly, such as:

listen [::]:80 default_server;
listen 80 default_server;
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • What listen block did you write in the other servers? For my all requests to the other servers on IPv4 end up on the default_server. – white_gecko May 30 '13 at 20:22
  • It's in `/etc/nginx/conf.d/default.conf` which serves the `server_name _`. If you're having trouble, check nginx's error log; if you just did a `service nginx reload` it will say OK even if the configuration file check failed. – Michael Hampton May 30 '13 at 20:24
  • Sorry, I don't understand. What exactly did you add to `/etc/nginx/conf.d/default.conf`? – white_gecko May 30 '13 at 20:30
  • I put the above `listen` directive in that particular `server` block. The rest I left untouched. – Michael Hampton May 30 '13 at 20:33
  • Ah, ok because I have my server configs in `/etc/nginx/sites-available` resp. `/etc/nginx/sites-enabled`. – white_gecko May 30 '13 at 20:38
  • OK, so you're on Debian rather than Red Hat. There should be a default server enabled in there somewhere that you can use. – Michael Hampton May 30 '13 at 20:42
  • Dear god, the cringe from seeing space as the delimiter for parameters to options in a .conf file, shudder. – artifex May 31 '13 at 20:14
  • When using two directives for explicitly listening on both IPv4 and IPv6, it it necessary to put the directives on all the server blocks, or just one? – mcont Sep 17 '18 at 10:31
  • @mcont Yes, both are necessary; otherwise nginx will not match requests to that `server` block for the not `listen`ing protocol. – Michael Hampton Sep 18 '18 at 21:39
4

An alternative to @Michael Hampton's answer (but you have to touch all files) is to insert

listen 80;      # listen for IPv4
listen [::]:80; # listen for IPv6

for all servers.


Strange is, that inserting listen [::]:80 ipv6only=off for more than one server results in

nginx: [emerg] duplicate listen options for [::]:80 in /etc/nginx/sites-enabled/default.conf:3

And a mix of listen 80; and ipv6only=off (in different servers) results in

nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
white_gecko
  • 257
  • 1
  • 3
  • 11