当前位置:首页 > 行业动态 > 正文

https配置服务器

配置HTTPS服务器需获取SSL证书(如Let’s Encrypt),将证书文件与私钥部署至服务器,修改Web服务配置绑定443端口,启用TLS协议,设置强制HTTP跳转,验证域名解析,重启服务后通过浏览器安全

HTTPS 服务器配置指南

HTTPS 基础概念

  • HTTPS:基于 HTTP 协议,通过 SSL/TLS 加密传输数据,确保通信安全。
  • SSL/TLS 证书:由受信任的 CA(证书颁发机构)签发,用于验证服务器身份并加密通信。

获取 SSL/TLS 证书

证书类型 特点
免费证书 Let’s Encrypt(最常用)、ZeroSSL
付费证书 支持更长有效期、企业级验证(如域名验证 DV、组织验证 OV、扩展验证 EV)

获取步骤(以 Let’s Encrypt 为例):

  1. 安装 certbot 工具:
    # Debian/Ubuntu
    sudo apt install certbot
    # CentOS/RHEL
    sudo yum install certbot
  2. 申请证书:
    sudo certbot certonly --standalone -d example.com -d www.example.com
    • 生成的证书文件:/etc/letsencrypt/live/example.com/fullchain.pem(证书链)和 privkey.pem(私钥)。

服务器配置示例

Nginx 配置

编辑 /etc/nginx/sites-available/default(或对应配置文件):

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;  # 强制跳转 HTTPS
}
server {
    listen 443 ssl;
    server_name example.com www.example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;  # 禁用老旧协议
    ssl_ciphers HIGH:!aNULL:!MD5;  # 安全加密套件
    location / {
        root /var/www/html;
        index index.html;
    }
}

重启 Nginx:

sudo systemctl restart nginx

Apache 配置

编辑 /etc/apache2/sites-available/000-default.conf

<VirtualHost :80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost :443>
    ServerName example.com
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    DocumentRoot /var/www/html
    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

启用 SSL 模块并重启 Apache:

sudo a2enmod ssl
sudo systemctl restart apache2

验证 HTTPS 配置

  1. 检查证书有效性
    curl -I https://example.com
    # 输出应包含 `HTTP/2 200` 和 `expire` 日期
  2. 浏览器访问:直接访问 https://example.com,确认绿色锁图标。
  3. 强制 HTTPS 跳转:确保所有 HTTP 请求自动重定向到 HTTPS。

常见问题与解决

问题 解决方案
证书过期 使用 certbot renew 或配置自动续期(如 cron 任务)。
端口冲突(443) 检查其他服务是否占用 443 端口,使用 sudo lsof -i:443 排查。
浏览器提示不安全 检查是否存在混合内容(HTTP 资源),确保所有链接使用 https://
HSTS 未生效 在服务器配置中添加 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains"

相关问题与解答

问题 1:如何选择适合的 SSL/TLS 证书?

解答

  • 个人站点/小型项目:推荐免费证书(如 Let’s Encrypt),自动化续期且兼容性好。
  • 企业站点:选择付费证书(如 DigiCert、GlobalSign),支持组织验证或扩展验证,提升用户信任度。
  • 通配符证书:若需要覆盖多个子域名(如 .example.com),可购买通配符证书。

问题 2:HTTP 和 HTTPS 可以同时启用吗?

解答

  • 建议禁用 HTTP:保留 HTTPS 并强制跳转,避免安全风险。
  • 若需共存:需确保 HTTP 仅用于跳转,且服务器不暴露敏感数据。
  • 风险提示:HTTP 流量易被劫持或改动
0