出現: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 測試嗎?
立馬卸掉
重啟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