服务器80,443端口配置教程

一、端口作用

  • 80 端口:HTTP 明文访问,浏览器默认端口。
  • 443 端口:HTTPS 加密访问(需 SSL 证书)。

二、Linux 配置(以 Nginx 为例)

1. 放行防火墙端口

# Ubuntu/Debian(ufw)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

# CentOS/RHEL(firewalld)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Nginx 配置(HTTP+HTTPS)

写入:

# HTTP 强制跳转 HTTPS
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    return 301 https://$host$request_uri;
}

# HTTPS 主配置
server {
    listen 443 ssl;
    server_name yourdomain.com www.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    location / {
        root /var/www/yourdomain;
        index index.html;
    }
}

重启 Nginx 生效

sudo nginx -t          # 测试配置
sudo systemctl restart nginx

三、Windows 配置(IIS 为例)

1. 放行防火墙端口

  • 图形化:Win+R → 输入 wf.msc → 入站规则 → 新建规则 → 端口 → TCP 80,443 → 允许连接 → 全选配置文件 → 命名完成。
  • PowerShell(管理员):
New-NetFirewallRule -DisplayName "Allow 80" -Direction Inbound -LocalPort 80 -Protocol TCP -Action Allow
New-NetFirewallRule -DisplayName "Allow 443" -Direction Inbound -LocalPort 443 -Protocol TCP -Action Allow

2. IIS 绑定 80/443

  1. 打开 IIS 管理器 → 站点 → 右键 “添加网站”。
图片[1]-服务器80,443端口配置教程-七昀侠

绑定:

  • HTTP:IP 全部未分配,端口 80,主机名填域名。
图片[2]-服务器80,443端口配置教程-七昀侠

HTTPS:添加 → 类型 https,端口 443,选择 SSL 证书。

图片[3]-服务器80,443端口配置教程-七昀侠
  1. (可选)HTTP 自动跳转 HTTPS:选中站点 → 配置编辑器 → 系统.webServer/security/access → sslFlags 选 SslRequire;或安装 URL 重写规则。

四、SSL 证书获取(免费)

推荐 Let’s Encrypt(有效期 90 天,可自动续期):

# Ubuntu/Debian
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

五、验证端口是否正常

# Linux
netstat -tuln | grep -E "80|443"
curl -I http://yourdomain.com
curl -I https://yourdomain.com

# Windows(CMD)
netstat -ano | findstr "80"
netstat -ano | findstr "443"

六、常见问题

  • 端口被占用
    • Linux:sudo lsof -i :80sudo fuser -k 80/tcp
    • Windows:netstat -ano | findstr "80" → 找 PID 结束进程
  • HTTPS 无法访问:检查证书路径、域名匹配、防火墙 / 安全组是否放行 443。
本站代码模板仅供学习交流使用请勿商业运营,严禁从事违法,侵权等任何非法活动,否则后果自负!
© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容