Nginx配置网站支持ipv6访问

2015年3月24日20:48:04网站建设25,843

如果你的nginx环境已经编译ipv6模块了,那么可以配置此nginx环境下所有网站都通过ipv6访问。本文就主要讲解怎样修改网站nginx配置文件使网站支持ipv4和ipv6同时访问。文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

例如你的nginx环境下一共配置了两台网站,两个网站的配置文件分别为:site1.conf,sit2.conf。如果你想让两个网站都同时支持ipv4和ipv6访问,你可以修改两个网站的配置文件。你只需要将site1.conf,sit2.conf两个文件中server段的listen语句改成:文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

listen [::]:80;文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

但是从Nginx 1.2的某个版本开始,默认ipv6only是打开的,也就是上面的语句只会监听IPv6的端口而不会监听IPv4的端口。所以为了考虑在各环境下的可用性,强烈建议你采用下方的设置方法:文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

listen 80;
listen [::]:80 default_server ipv6only=on;文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

备注:default_server ipv6only=on内容只需要在一个站点的配置文件内添加,例如我们可以在site1.conf中参照上面的配置,在site2.conf中配置成如下内容:文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

listen 80;
listen [::]:80;文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

此时site1为整个nginx环境下的默认站点,也就是如果通过ipv6地址直接访问,将进入到site1.文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

修改后在ssh下执行(本文以wdcp环境下的nginx为样例,请根据你环境的情况进行调整):文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

/www/wdlinux/nginx/sbin/nginx -t文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

如果出现如下语句,说明配置错误,请不要使用listen [::]:80;同时监听ipv6和ipv4地址,请修改为本文提到的第二种ipv4与ipv6同时监听的方法。文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

如返回如下语句,则说明配置正确:文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

nginx: the configuration file /www/wdlinux/nginx-1.2.9/conf/nginx.conf syntax is ok
nginx: configuration file /www/wdlinux/nginx-1.2.9/conf/nginx.conf test is successful文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

依次执行以下语句重启nginx:文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

killall nginx文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

/www/wdlinux/nginx-1.2.9/sbin/nginx文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

以上步骤完成后,就可以通过ipv6访问网站了。如通过http://[ipv6地址]就可以打开默认的site1.文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

如需通过域名访问,则请为site1和site2的域名分别添加AAAA解析。文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

延伸阅读:文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

监听指定的ipv6地址:文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

listen [2001:1:2:81::2]:80;文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

IPv6监听SSL(443)端口:文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

listen [::]:443 ssl;文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

查询你的网络是否支持ipv6,请访问:http://ipv6.sjtu.edu.cn/。如果网页上方显示你正使用IPv6地址,则说明你的网络支持ipv6。文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

本站已经支持ipv6访问,如果你的网络支持ipv6协议,那你可能正在通过ipv6访问本站。文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

相关阅读:文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

使WDCP的lnmp环境支持ipv6协议访问文章源自堕落的鱼-https://www.duoluodeyu.com/1225.html

匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

评论:2   其中:访客  1   博主  1
    • 肖振杰博客
      肖振杰博客 1

      好现像啊。看来以后。。。