nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)报错解决记录

版权声明:本文是我的原创文章,转载注明出处就行,不注明我也管不着。 https://blog.csdn.net/Clark_Fitz817/article/details/79768126

问题描述

今天我在阿里云服务器上配置 nginx ,刚安装好nginx

sudo apt install nginx 

使用默认配置,运行

sudo nginx # 启动nginx

报错如下:

nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)

然后我运行

sudo nginx -t  # 检查nginx的配置是否正确

报错如下:

nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
nginx: configuration file /etc/nginx/nginx.conf test failed


系统信息:
OS: Debian 9.2 stretch
Kernel: x86_64 Linux 4.9.0-4-amd64



问题分析 在 /etc/nginx/sites-enabled/default 文件中,有 ```conf server{ listen 80 default_server; listen [::]:80 default_server; } ``` 其中 listen [::]:80 default_server; 这句是配置对 ipv6端口的监听。 ipv4和ipv6有什么区别呢? > TCP/IP协议是互联网发展的基石,其中IP是网络层协议,规范互联网中分组信息的交换和选路。目前采用的IPv4协议地址长度为32位,总数约43亿个IPv4地址已分配殆尽。 IPv6是IP地址的第六版协议,诞生于1999年,其最大的优点就是设计地址长度达128bit,可以提供2的128次方个IP地址,即使将地球上所有沙子都变成晶体管,依然会有足够的IP地址给电子设备使用。 近乎无穷的IP地址让IPv6能容得下海量设备,这将与5G等技术一起,支撑移动互联网、物联网、工业互联网、云计算、大数据、人工智能等新兴业态的快速发展。 ipv4和ipv6只是位数不同,版本不同,ipv6目前还没有普及。

目前国内很多服务器不支持ipv6,阿里云现在开始支持ipv6了,但是需要手动开启。
因为没有开启ipv6,所以会出现 Address family not supported by protocol 的报错。

解决问题:

sudo vim /etc/nginx/sites-enabled/default

将 listen [::]:80 default_server; 注释掉

server{
	listen 80 default_server;
	# listen [::]:80 default_server;
}

然后

sudo nginx -t  # 检查nginx的配置是否正确

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

这样nginx就可以正常启动了。或者还可以将服务器的ipv6开启,具体的方法百度一下挺多的。

Address family not supported by protocol

10-26

客户端异常:rn01-05 23:06:01.484: W/System.err(3265): java.net.SocketException: socket failed: EAFNOSUPPORT (Address family not supported by protocol)rn01-05 23:06:01.484: W/System.err(3265): at libcore.io.IoBridge.socket(IoBridge.java:573)rn01-05 23:06:01.484: W/System.err(3265): at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)rn01-05 23:06:01.484: W/System.err(3265): at java.net.Socket.startupSocket(Socket.java:559)rn01-05 23:06:01.484: W/System.err(3265): at java.net.Socket.tryAllAddresses(Socket.java:127)rn01-05 23:06:01.484: W/System.err(3265): at java.net.Socket.(Socket.java:177)rn01-05 23:06:01.484: W/System.err(3265): at java.net.Socket.(Socket.java:149)rn01-05 23:06:01.484: W/System.err(3265): at com.client.MainActivity$1.run(MainActivity.java:66)rn01-05 23:06:01.484: W/System.err(3265): at java.lang.Thread.run(Thread.java:856)rn01-05 23:06:01.484: W/System.err(3265): Caused by: libcore.io.ErrnoException: socket failed: EAFNOSUPPORT (Address family not supported by protocol)rn01-05 23:06:01.484: W/System.err(3265): at libcore.io.Posix.socket(Native Method)rn01-05 23:06:01.484: W/System.err(3265): at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)rn01-05 23:06:01.484: W/System.err(3265): at libcore.io.IoBridge.socket(IoBridge.java:558)rn01-05 23:06:01.484: W/System.err(3265): ... 7 morern客户端代码:rnpublic class MainActivity extends Activity implements OnClickListenerrn private static final String TAG = "SBSF";rn rn private TextView textMsg;rn private Button buttonSend;rn rn @Overridern protected void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.main_activity);rn textMsg = (TextView) findViewById(R.id.textMsg);rn buttonSend =(Button) findViewById(R.id.buttonSend);rn rn buttonSend.setOnClickListener(this);rn rnrn @Overridern public boolean onCreateOptionsMenu(Menu menu) rn // Inflate the menu; this adds items to the action bar if it is present.rn getMenuInflater().inflate(R.menu.main, menu);rn return true;rn rnrn @Overridern public void onClick(View v) rn switch(v.getId())rn case R.id.buttonSend:rn sendMsg();rn break;rn rn rnrn private void sendMsg() rn sendThread.start();rn rnrn rn private Thread sendThread = new Thread(new Runnable() rn @Overridern public void run() rn try rn Socket clientSocket = new Socket("192.168.1.112", 8888);rn DataOutputStream dataOutputStream = new DataOutputStream(clientSocket.getOutputStream());rn dataOutputStream.writeUTF(TAG);//输出信息给服务器rn DataInputStream dataInputStream = new DataInputStream(clientSocket.getInputStream());rn Message message = Message.obtain();rn message.obj = dataInputStream.readUTF();//输入信息显示在客户端rn sendHandler.sendMessage(message);rn clientSocket.close();rn dataInputStream.close();rn dataOutputStream.close();rn catch (UnknownHostException e) rn e.printStackTrace();rn catch (IOException e) rn e.printStackTrace();rn rn rn );rn rn private Handler sendHandler = new Handler()rnrn @Overridern public void handleMessage(Message msg) rn super.handleMessage(msg);rn textMsg.setText(msg.obj.toString());rn rn rn ;rnrn服务端代码:rnrnpublic class ServerMain rnrn /**rn * @param argsrn */rn public static void main(String[] args) rn try rn ServerSocket serverSocket = new ServerSocket(8888);rn Socket clientSocket = serverSocket.accept();rn DataInputStream dataInputStream = new DataInputStream(clientSocket.getInputStream());rn System.out.println("我来自客户端:"+dataInputStream.readUTF());rn DataOutputStream dataOutputStream = new DataOutputStream(clientSocket.getOutputStream());rn dataOutputStream.writeUTF("我来自服务端!");rn serverSocket.close();rn dataInputStream.close();rn dataOutputStream.close();rn catch (IOException e) rn e.printStackTrace();rn rn rnrnrn在有些Android设备上运行正常,有些又出现如题所示异常,跪求分析原因!

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试