Nginx 學習筆記(七)如何解決nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

出現:nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) 錯誤,有如下兩種狀況html

一、80端口被佔用java

二、ipv4端口和ipv6端口衝突的問題python

今天服務器安裝了NodeJs,服務器實在卡的不行,就重啓了,結果重啓後,Nginx沒有自動重啓。果斷的手動重啓,結果問題來了linux

在ubuntu16.04上面嘗試啓動nginx,使用命令:nginx

sudo /etc/init.d/nginx start

啓動不了啊!出錯了哎!提示的錯誤信息:apache

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    -       

問題上面怎麼沒有顯示進程號ubuntu

盡然沒有仔細的看看這句話,要root權限啊!趕忙切換服務器

此次看來是真有了,怎麼是apache2,我都不知道何時安裝的這個:ab 測試嗎?app

 立馬卸掉curl

重啓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 工具看看,該命令能夠查看到當前系統開啓機動服務的狀況。

果不其然,任然在開啓啓動項中呀!!!

咱們再來看看通啓動相應的服務的腳本文件是否存在

看到了嗎!也在這裏啊!啊哈

 

 

 

 

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

相關文章
相關標籤/搜索