突然我得到下面的nginx错误
* Restarting nginx
* Stopping nginx nginx
...done.
* Starting nginx nginx
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
...done.
...done.
如果我跑
lsof -i :80 or sudo fuser -k 80/tcp
我什么也得不到。80端口上没有东西
然后我运行如下:
sudo netstat -pan | grep ":80"
tcp 0 0 127.0.0.1:8070 0.0.0.0:* LISTEN 15056/uwsgi
tcp 0 0 10.170.35.97:39567 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39564 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39584 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39566 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39571 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39580 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39562 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39582 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39586 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39575 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39579 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39560 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39587 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39591 10.158.58.13:8080 TIME_WAIT -
tcp 0 0 10.170.35.97:39589 10.158.58.13:8080 TIME_WAIT -
我被难住了。我如何调试这个?
我在端口8070上使用带有代理通道的uwsgi。Uwsgi正在运行。Nginx则不然。我使用的是ubuntu 12.4
下面是我的nginx conf文件的相关部分
upstream uwsgi_frontend {
server 127.0.0.1:8070;
}
server {
listen 80;
server_name 127.0.0.1;
location = /favicon.ico {
log_not_found off;
}
location / {
include uwsgi_params;
uwsgi_buffering off;
uwsgi_pass 127.0.0.1:8070;
}
}
下面是我在ubuntu 12.04上安装nginx的方法
nginx=stable;add-apt-repository ppa:nginx/$nginx;
apt-get update
apt get install nginx-full
[::]:80是ipv6地址。
如果你有一个nginx配置,监听端口80和端口[::]:80,就会导致这个错误。
我有以下在我的默认网站可用文件:
listen 80;
listen [::]:80 default_server;
你可以通过添加ipv6only=on到[::]:80来解决这个问题,就像这样:
listen 80;
listen [::]:80 ipv6only=on default_server;
有关更多信息,请参见:
http://forum.linode.com/viewtopic.php?t=8580
http://wiki.nginx.org/HttpCoreModule#listen
首先修改apache监听端口80为8080
在/etc/apache2/ports.conf中包含Apache
Listen 1.2.3.4:80 to 1.2.3.4:8080
sudo service apache2 restart
or
sudo service httpd restart // in case of centos
然后添加nginx作为反向代理服务器,将监听apache端口
server {
listen 1.2.3.4:80;
server_name some.com;
access_log /var/log/nginx/something-access.log;
location / {
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^.+\.(jpg|js|jpeg|png)$ {
root /usr/share/nginx/html/;
}
location /404.html {
root /usr/share/nginx/html/40x.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# put code for static content like js/css/images/fonts
}
修改后重启nginx服务器
sudo service nginx restart
现在所有流量都将由nginx服务器处理,并将所有动态请求发送到apache,静态内容由nginx服务器提供。
对于像缓存这样的高级配置:
https://www.linode.com/docs/web-servers/nginx/slightly-more-advanced-configurations-for-nginx/#basic-nginx-caching
我在letsencrypt (certbot)和nginx中遇到了同样的问题,
裁判:https://github.com/certbot/certbot/issues/5486
此错误还没有解决方案
因此,a更改了一个cron为renew(在renew后重新加载)(使用certbot的建议)
-- in /etc/cron.d/certbot
from
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew
to
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --pre-hook "service nginx stop" --post-hook "service nginx start"
日志(短):
-- in /var/log/syslog
Jun 10 00:14:25 localhost systemd[1]: Starting Certbot...
Jun 10 00:14:38 localhost certbot[22222]: nginx: [error] open() "/run/nginx.pid$
Jun 10 00:14:41 localhost certbot[22222]: Hook command "nginx" returned error c$
Jun 10 00:14:41 localhost certbot[22222]: Error output from nginx:
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] still could not bind()
Jun 10 00:14:41 localhost systemd[1]: Started Certbot.
-- in /var/log/nginx/error.log
2018/06/10 00:14:27 [notice] 22233#22233: signal process started
2018/06/10 00:14:31 [notice] 22237#22237: signal process started
2018/06/10 00:14:33 [notice] 22240#22240: signal process started
2018/06/10 00:14:34 [notice] 22245#22245: signal process started
2018/06/10 00:14:38 [notice] 22255#22255: signal process started
2018/06/10 00:14:38 [error] 22255#22255: open() "/run/nginx.pid" failed (2: No $
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: still could not bind()
我也有同样的问题,但我看到Nginx监听了80端口:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9730/nginx
但是当我试图重新启动它时,我有错误:
service nginx restart
Stopping nginx: [FAILED]
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 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:443 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:443 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:443 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:443 failed (98: Address already in use)
nginx: [emerg] still could not bind()
我的问题是在配置文件,我设置PID文件,似乎系统不能正确捕获它:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
当我取出它时,它工作了。