今天服务器安装了NodeJs,服务器实在卡的不行,就重启了,结果重启后,nginx没有自动重启。果断的手动重启,结果问题来了
在ubuntu16.04上面尝试启动nginx,使用命令:
sudo /etc/init.d/nginx start
启动不了啊!出错了哎!提示的错误信息:
[email protected]:~$ 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端口是不是被占用了
[email protected]:~$ 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 -
遇到这种问题我先用中文搜索了一下答案,发现大家都在装逼地说要杀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;
http://www.hankcs.com/appos/linux/fix-nginx-bind-err.html