You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSL环境下Nginx无法启动(端口绑定失败)求助

修复WSL中Nginx绑定80/443端口失败的问题

1. 排查WSL内部端口占用

先在WSL终端执行命令,查看80、443端口的占用进程:

sudo lsof -i :80
sudo lsof -i :443

如果返回进程信息,说明WSL内有其他服务(如Apache、Docker容器服务)占用端口,直接杀掉对应进程:

sudo kill -9 <进程PID>

之后执行sudo nginx -t验证配置,无问题即可启动Nginx。

2. 检查Windows系统端口占用

WSL默认共享Windows网络端口,Windows侧的服务(如IIS、迅雷、HTTP.sys组件)可能占用80/443端口:

方式一:定位并结束占用进程

打开管理员权限的PowerShell/CMD,执行:

netstat -ano | findstr ":80"
netstat -ano | findstr ":443"

找到占用端口的PID,打开任务管理器「详细信息」标签,右键结束对应进程。

方式二:禁用Windows HTTP服务

若为HTTP.sys服务占用端口,可执行以下命令禁用:

# 停止服务
net stop http
# 设置为禁用自动启动
sc config http start= disabled

注意:禁用该服务会影响依赖它的IIS、打印服务等,按需操作

3. 修改Nginx监听端口(临时方案)

若不想处理端口占用,可修改Nginx配置切换到非特权端口:

  1. 打开Nginx主配置文件(通常路径为/etc/nginx/nginx.conf,站点配置在/etc/nginx/sites-available/目录):
sudo nano /etc/nginx/nginx.conf
  1. 将listen 80;和listen 443 ssl;修改为:
listen 8080;
listen 8443 ssl;
  1. 保存后验证配置:sudo nginx -t,重启Nginx:sudo systemctl restart nginx
  2. 访问时需加端口号,如http://localhost:8080

4. 配置WSL端口转发(可选)

若需保留Nginx监听自定义端口,同时映射到Windows的80/443:

  1. 先在WSL中设置Nginx监听自定义端口(如8080、8443)
  2. 打开管理员PowerShell,添加端口转发规则:
# 获取WSL的IP地址:在WSL终端执行 hostname -I
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=80 connectaddress=<WSL_IP> connectport=8080
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=443 connectaddress=<WSL_IP> connectport=8443
  1. 添加防火墙放行规则:
netsh advfirewall firewall add rule name="WSL Nginx 80" dir=in action=allow protocol=TCP localport=80
netsh advfirewall firewall add rule name="WSL Nginx 443" dir=in action=allow protocol=TCP localport=443

内容的提问来源于stack exchange,提问作者Maikon Panazzolo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 00:03:13