Nginx 学习笔记(七)如何解决nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)...

出现:nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) 错误,有以下两种情况

1、80端口被占用

2、ipv4端口和ipv6端口冲突的问题

今天服务器安装了NodeJs,服务器实在卡的不行,就重启了,结果重启后,Nginx没有自动重启。果断的手动重启,结果问题来了

在ubuntu16.04上面尝试启动nginx,使用命令:

sudo /etc/init.d/nginx start

启动不了啊!出错了哎!提示的错误信息:

www@TinywanAliYun:~$ sudo /usr/local/openresty/nginx/sbin/nginx
nginx: [warn] could not build optimal variables_hash, you should increase either variables_hash_max_size: 1024 or variables_hash_bucket_size: 64; ignoring variables_hash_bucket_size
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

第一种情况,查看80端口是不是被占用了

www@TinywanAliYun:~$ netstat -anp |grep 80
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -               
tcp        0      0 内网IP:50852     外网IP:80      ESTABLISHED -               
unix  2      [ ACC ]     STREAM     LISTENING     13580    -                   /var/run/nscd/socket
unix  3      [ ]         STREAM     CONNECTED     13809    -       

问题上面怎么没有显示进程号

尽然没有仔细的看看这句话,要root权限啊!赶紧切换

这次看来是真有了,怎么是apache2,我都不知道什么时候安装的这个:ab 测试吗?

 立马卸掉

Nginx 学习笔记(七)如何解决nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)..._第1张图片

重启Openresty,看看情况

果断没报错呀,访问:https://www.tinywan.com/ 一切正常

 第二种情况

遇到这种问题我先用中文搜索了一下答案,发现大家都在装逼地说要杀nginx重复的进程。我试了下发现是扯淡,于是看了谷歌搜到的第一个英文页面,老外说是nginx先监听了ipv4的80端口之后又监听了ipv6的80端口,于是就重复占用了。更加坑人的是你去看了端口占用它又把80端口释放了,是不是很囧。  解决方案是编辑nginx的配置文件

我的配置

    # 配置HTTP请求重定向
    server {
        # 监听所有的ipv4的地址
        listen 80 default_server;
        # 监听所有的ipv6的地址
        listen [::]:80 default_server;
        server_name  _;
        # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
        return 301 https://$host$request_uri;
        # 过滤爬虫
        if ($http_user_agent ~* "python|curl|java|wget|httpclient|okhttp") {
            return 503;
        }
    }

修改这一段

listen [::]:80 default_server;

修改后

listen [::]:80 ipv6only=on default_server;

 果断重启后,正常

分析一下问题:

刚开始我是Nginx默认开机启动的,但是我今天又搞了个Openresty开启自启动,是不是冲突掉了

直接把Nginx启动脚本删掉 rm /etc/init.d/nginx ,又重启,结果还是不行

那就安装个sysv-rc-conf 工具看看,该命令可以查看到当前系统开启机动服务的情况。

Nginx 学习笔记(七)如何解决nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)..._第2张图片

果不其然,任然在开启启动项中呀!!!

我们再来看看通启动相应的服务的脚本文件是否存在

Nginx 学习笔记(七)如何解决nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)..._第3张图片

看到了吗!也在这里啊!啊哈

 

 

 

 

http://www.hankcs.com/appos/linux/fix-nginx-bind-err.html

 

你可能感兴趣的:(Nginx 学习笔记(七)如何解决nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)...)