How to do dual stack IPv4/IPv6 with virtual hosts on nginx 1.4

burnersk asked:

I’m using nginx 1.4.1 (now, before it was 1.2.1) and would like to support dual stack (IPv4 and IPv6). I’m always getting duplicate listen options for xyz errors from nginx.

server {
    listen 80 default_server;
    listen [::]:80 ipv6only=on default_server;
    server_name domain1;
}
server {
    listen 443 ssl default_server;
    listen [::]:443 ssl ipv6only=on default_server;
    server_name domain1;
}

server {
    listen 80;
    listen [::]:80 ipv6only=on;
    server_name domain2;
}
server {
    listen 443 ssl;
    listen [::]:443 ssl ipv6only=on;
    server_name domain2;
}

If I use only listen 80 I’m getting only IPv4. Otherwise if I use listen [::]:80 I’m getting only IPv6.

How to setup nginx 1.4.x to support virtual host dual stack?

My answer:


You’ve used default_server in more than one server block. This is only meant to be used to define the default virtual host for a given IP address/port combination. So it should appear only once per IP/port pair in your entire configuration. Which host should be the default virtual host is up to you. (Or you did, until you edited this part out.)

Also, ipv6only can appear only once in the entire configuration. Since its default in 1.4 and higher is on, you can safely omit it entirely.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.