Nginx绑定0.0.0.0:80/81端口失败(错误98)求助
Nginx启动失败:bind()端口报错但无进程占用
错误信息
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Unknown error) nginx: [emerg] bind() to [::]:80 failed (98: Unknown error) nginx: [emerg] bind() to 0.0.0.0:81 failed (98: Unknown error) nginx: [emerg] bind() to [::]:81 failed (98: Unknown error) (上述信息重复5次) nginx: [emerg] still could not bind()
已确认信息
- 配置文件语法检测通过:
root@YumYummity:/# sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
- 配置文件详情:
nginx.conf
user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; } http { sendfile on; tcp_nopush on; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
site1.conf
server { listen 80; listen [::]:80 ipv6only=on; server_name beta.DOMAIN.TLD; location / { proxy_pass http://127.0.0.1:5000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
site2.conf
server { listen 81; listen [::]:81 ipv6only=on; server_name DOMAIN.TLD; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
- 系统信息:
root@YumYummity:/# hostnamectl Static hostname: YumYummity Icon name: computer-vm Chassis: vm Machine ID: 6f2d2e6f6e3e444c9dc7b36422cc2803 Boot ID: 16bcf7164313496ca23ee6cf51a04394 Virtualization: microsoft Operating System: Ubuntu 22.04.1 LTS Kernel: Linux 5.15.0-86-generic Architecture: x86-64 Hardware Vendor: Vultr Hardware Model: HFC
- 已尝试操作:
sudo rebootsudo systemctl stop nginxsudo fuser -k 80/tcp及sudo fuser -k 81/tcpsudo systemctl restart nginxsudo killall nginx
- 端口占用检查:
netstat未发现80/81端口被占用:
root@YumYummity:/# sudo netstat -tulpn Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 714/systemd-resolve tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 846/postgres tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 793/sshd: /usr/sbin tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 760/memcached tcp6 0 0 ::1:5432 :::* LISTEN 846/postgres tcp6 0 0 :::22 :::* LISTEN 793/sshd: /usr/sbin udp 0 0 127.0.0.53:53 0.0.0.0:* 714/systemd-resolve udp 0 0 censored_ip:68 0.0.0.0:* 712/systemd-network
lsof无结果返回:
root@YumYummity:/# sudo lsof -i:80 root@YumYummity:/#
解决方案
针对端口无占用但bind失败的场景,按以下步骤排查:
1. 检查AppArmor限制
Ubuntu默认启用AppArmor,可能限制Nginx绑定端口:
- 查看AppArmor状态:
sudo aa-status
- 临时关闭测试:
sudo systemctl stop apparmor sudo systemctl disable apparmor
若关闭后Nginx能启动,需修改AppArmor规则:
编辑/etc/apparmor.d/usr.sbin.nginx,添加:
network tcp bind 80, network tcp bind 81,
重启AppArmor:
sudo systemctl restart apparmor
2. 检查内核端口预留
内核可能预留了80/81端口,查看预留列表:
sudo sysctl net.ipv4.ip_local_reserved_ports
若包含80或81,编辑/etc/sysctl.conf移除对应端口,执行:
sudo sysctl -p
3. 检查Nginx权限
确保Nginx有绑定端口的权限:
- 查看当前Nginx进程:
ps aux | grep nginx
- 尝试以root直接启动测试:
sudo nginx -c /etc/nginx/nginx.conf
4. 排查IPv6配置问题
若服务器IPv6未正确配置,会导致绑定[::]:80/81失败:
- 临时禁用IPv6测试:
编辑/etc/sysctl.conf添加:
net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1
执行sudo sysctl -p后重启Nginx。
若有效,可修改Nginx配置,仅绑定IPv4:删除site配置中的listen [::]:80 ipv6only=on;和listen [::]:81 ipv6only=on;,保留listen 80;和listen 81;即可。
5. 清理Nginx残留进程/套接字
可能存在僵尸进程或残留套接字:
- 查看PID文件并强制杀死进程:
sudo kill -9 $(cat /run/nginx.pid)
- 清理残留套接字:
sudo rm -rf /run/nginx.sock
内容的提问来源于stack exchange,提问作者YumYummity
相关产品推荐
相关产品推荐

