IPv6 support in nginx

To enable IPv6 support in nginx, we need to check whether it has been compiled with --with-ipv6 flag. To check, fire up the terminal and type in this command :

[code language=”bash”]nginx -V[/code]

The results should be something like this :

[code language=”bash”]nginx version: nginx/0.7.65
TLS SNI support enabled
configure arguments: –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –pid-path=/var/run/nginx.pid –lock-path=/var/lock/nginx.lock –http-log-path=/var/log/nginx/access.log –http-client-body-temp-path=/var/lib/nginx/body –http-proxy-temp-path=/var/lib/nginx/proxy –http-fastcgi-temp-path=/var/lib/nginx/fastcgi –with-debug –with-http_stub_status_module –with-http_flv_module –with-http_ssl_module –with-http_dav_module –with-http_gzip_static_module –with-http_realip_module –with-mail –with-mail_ssl_module –with-ipv6 –add-module=/build/buildd/nginx-0.7.65/modules/nginx-upstream-fair[/code]

Pre-compiled Debian/Ubuntu packages already has IPv6 support built-in.

Now we need to edit the configuration file to tell nginx to bind to IPv6 addresses(as well as IPv4 addresses).

[code language=”bash”]sudo nano /etc/nginx/sites-available/default[/code]

Depending on your configuration, this file might be located somewhere else. (Check /usr/local/nginx/conf/nginx.conf if you compiled nginx manually)

Search for listen directives and change them as follows:
[code]listen [::]:80;[/code]
This will make nginx bind to both IPv6 and IPv4 addresses.

In order to bind to IPv6 addresses only (no IPv4), use the following:
[code]listen [::]:80 default ipv6only=on;[/code]

In order to bind to a specific IPv6 address:
[code]listen [2001:6f8:1c00:16d::2]:80;[/code]

When you are done, reload the nginx configuration file by typing in :
[code language=”bash”]nginx -s reload[/code]

Now we have to check whether nginx is listening to both IPv6 and IPv4 requests:
[code language=”bash”]netstat -tulpna | grep nginx[/code]
You’ll get something like this :
[code]tcp6 0 0 :::80 :::* LISTEN 1891/nginx[/code]

You have successfully configured nginx to respond to IPv6 requests.