Tips / Nginx

How to enable Nginx IPv6 support

IPv4 protocol is almost gone, today more and more servers are serving IPv6 traffic. And the big question for nginx fans is: How do I enable / configure IPv6 support on Nginx? Answer is simple:

First, check if you don’t have ipv6 already enabled:

nginx -V

Then you should see an output like this:

nginx version: nginx/1.4.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/ --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

In this case, a precompiled CentOS Nginx package already shows ‘–with-ipv6’ option enabled. But if that is not your case, there are other ways to compile ipv6 support for Nginx, see below. Note: precompiled packages from CentOS, Ubuntu and Debian should have ipv6 support enabled by default.

Configure nginx with IPv6 support

To achieve this you just need to pass the –with-ipv6 option when you are running ‘configure’ command. For example:

./configure --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-file-aio --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
make install

Verify the results with the following command:

nginx -V

If you see –with-ipv6 option, then it’s compiled.

Edit your Nginx configuration file

pico -w /etc/nginx/nginx.conf

or (depending on your distro or compilation)

pico -w /usr/local/nginx/conf/nginx.conf

Search for “listen” variable, and set to:

listen [::]:80;

That will enable both, ipv4 and ipv6 support.

How can I just enable ipv6 and disable ipv4 support?

listen [::]:80 default ipv6only=on;

Is there any way to listen only on a specific ipv6 address?

Of course! This time just change listen setting in this way:

listen [2607:f0d0:1002:51::4]:80;

Restart Nginx to apply the changes:

service nginx restart

How can I check ipv6 is working under Nginx?

netstat -nlp | grep nginx

That should show you nginx running with ipv6 protocol.

Popular search terms:

  • nginx ipv6
  • nginx disable ipv6
  • nginx how disable ipv6
  • how to enable ipv6 in nginx?

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • How to enable ipv6 on SSL (port = 443).

    • Remco

      listen [2607:f0d0:1002:51::4]:443 ssl;