Nginx: Configure and Install With IPv6 Networking Support

How do I configure IPv6 networking under nginx? How do I assign and use an IPv6 address under nginx web server? How do I enable IPv6 Support in nginx?

You can easily configure IPv6 under nginx as follows. First, make sure IPv6 networking is working properly. See IPv6 CentOS / RHEL, FreeBSD, Debian / Ubuntu Linux, and Novell Suse/OpenSUSE Linux network configuration for more info.

ADVERTISEMENTS

How To Set Up Nginx with HTTP/2, SSL and IPv6+IPv4 Support on Linux or Unix

How To Set Up Nginx with HTTP/2, SSL and IPv6+IPv4 Support on Linux or Unix

Compile Nginx With IPv6 Support

You need to pass the –with-ipv6 option to configure command. Type the following command to compile it, enter (this is default on the latest version so you can skip this step):
# cd /path/to/nginx-src-code/
# ./configure --without-http_autoindex_module --without-http_userid_module \
--without-http_auth_basic_module --without-http_geo_module \
--without-http_fastcgi_module --without-http_empty_gif_module \
--with-poll_module --with-http_stub_status_module \
--with-http_ssl_module --with-ipv6
# make install

Verify IPv6 support in nginx

Verify the IPv6 support in nginx web server with the following command:
# /usr/local/nginx/sbin/nginx -V
OR
# nginx -V

nginx version: nginx/0.8.46
built by gcc 4.1.2 20080704 (Red Hat 4.1.2-48)
TLS SNI support disabled
configure arguments: --without-http_autoindex_module --without-http_userid_module --without-http_auth_basic_module --without-http_geo_module --without-http_fastcgi_module --without-http_empty_gif_module --with-poll_module --with-http_stub_status_module --with-http_ssl_module --with-ipv6

OR a simple short command:
# nginx -V 2>&1 >/dev/null | grep --color -o with-ipv6
Sample outputs:

with-ipv6

How do I enable IPv6 Support in nginx?

Make sure you pass options as per your setup. Please note that all IPv6 addresses are specified in square brackets.

Edit configuration file

Edit /usr/local/nginx/conf/nginx.conf or /etc/nginx/nginx.conf, enter:
# vi /usr/local/nginx/conf/nginx.conf
Make sure listen directive is updated as follows (this must be placed between server { … } directives ) :

# listen to all IPv4 and IPv6 interfaces for port 80
# IPv4
listen :80;
# IPv6
listen [::]:80;

In one line listen to both IPv4 and IPv6 addresses:

listen [::]:80;

Do not listen on IPv4 but only on IPv6:

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

How to configure nginx with HTTP/2+SSL and IPv6/IPv4 support?

The syntax is:

        # port 443 IPv4 with http2 and ssl
        listen 443 ssl http2;
        # port 443 IPv6 with http2 and ssl
        listen [::]:443 ssl http2;
        # port 80 IPv4
        listen *:80;
        listen [::]:80;
        # my server name :)
        server_name www.cyberciti.biz;

Reload nginx server

Reload and restart nginx configuration, enter:
# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload
OR
# nginx -t && nginx -s reload
OR
# systemctl reload nginx

How do I listen only on 2607:f0d0:1002:59::2 IPv6 address?

Edit config file as follows:
listen listen [2607:f0d0:1002:59::2]:80;

How Do I verify that both IPv6 and IPv4 are working?

Use the netstat command to verify ip binding:
# netstat -tulpna | grep nginx
Sample outputs:

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      33094/nginx         
tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN      33094/nginx         
tcp        0      0 :::80                       :::*                        LISTEN      33094/nginx 
tcp6       0      0 :::443                      :::*                        LISTEN      33094/nginx 

The line tcp6 indicates that my nginx is listing on IPv6. You can also use the wget command or a web browser to verify the same:
$ wget http://[2607:f0d0:1002:59::2]/
Please note that you need to update your DNS and set AAAA type of record of IPv6.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
4 comments… add one
  • Southafrica Aug 11, 2010 @ 10:21

    Hi,

    is NGINX able to acct as a reverseproxy and send client requests to backendserver with IPV6-adresses (client -> IPV4 -> NGINX -> IPV6 -> backendserver)?

    Greetings

  • George Mar 20, 2013 @ 21:48

    I’m running Ubuntu 12.10, and having both listen statements prevented ngnix from starting. The second listen statement binds both IPv4 and IPv6. Removing the first statement will show IPv4 address as mapped address in log files. Adding “ipv6only=on” to the seconds listen statement will prevent this and enable listening on IPv4 and IPv6. More details: http://serverfault.com/questions/489843/ngnix-startup-isse-ipv4-ipv6/489847

  • Giel Berkers Feb 25, 2015 @ 7:14

    Hi, enabling IPv6 causes my nginx server to come into a redirect loop. Any ideas on what might be going wrong?

  • Constantin Apr 11, 2017 @ 9:00


    listen [::]:80;

    Doesn’t seem to work with 1.10.3 it only listens to TCPv6, so I had to manually add

    listen *:80

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.