Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

nginxでIPv6アドレスでのアクセスを受け付ける


LinuCエヴァンジェリストの鯨井貴博@opensourcetechです。


はじめに
nginxでIPv6アドレスで待ち受ける方法のメモです。


listenディレクティブ
IPv6での待ち受けには、listenディレクティブを使います。

マニュアルに記載されていますが、以下のようにすればOKです。

listen [::]:8000;
listen [::1];



やってみる
早速やってみましょう♪
※デフォルトのTCP80じゃなく、TCP81を使ってます。

[root@localhost ~]# cat /etc/nginx/conf.d/default.conf 
server {
    listen       81;
    listen       [::]:81;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


IPv6のアドレスを確認。
240f:32:57b8:1:5e06:7ace:4d2e:3456ですね。

[root@localhost ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:70:df:8a brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.185/24 brd 192.168.1.255 scope global noprefixroute dynamic enp0s3
       valid_lft 5275sec preferred_lft 5275sec
    inet6 240f:32:57b8:1:5e06:7ace:4d2e:3456/64 scope global noprefixroute dynamic 
       valid_lft 288sec preferred_lft 288sec
    inet6 fe80::81ce:f1ca:2a63:fbf3/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever


そして、アクセス。

[root@localhost ~]# curl 240f:32:57b8:1:5e06:7ace:4d2e:3456:81
<!DOCTYPE html>
<html>
<head>
<title>Welcome to my nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to my nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>


ブラウザからも。


firefoxからアクセスしてますが、http://[240f:32:57b8:1:5e06:7ace:4d2e:3456]:81/というようにURLを書きます。


おわりに
IPv6を直接使ってアクセスすることってこれまであまりなかったのですが、
URLの書き方など色々と発見があってよかったです♪

Opensourcetech by Takahiro Kujirai