<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html"><channel><title><![CDATA[.NET 与软件架构博客 — Shady Nagy]]></title><description><![CDATA[Shady Nagy 撰写的 .NET、Angular 和 Azure 实用教程。]]></description><link>https://www.shadynagy.com/zh/</link><image><url>https://www.shadynagy.com/logo.png</url><title>.NET 与软件架构博客 — Shady Nagy</title><link>https://www.shadynagy.com/zh/</link></image><generator>GatsbyJS</generator><lastBuildDate>Sun, 12 Jul 2026 06:34:31 GMT</lastBuildDate><atom:link href="https://www.shadynagy.com/zh/rss.xml" rel="self" type="application/rss+xml"/><webMaster><![CDATA[info@shadynagy.com (Shady Nagy)]]></webMaster><atom:link href="https://pubsubhubbub.appspot.com/" rel="hub"/><item><title><![CDATA[我如何修复网站SSL证书问题：完整指南]]></title><description><![CDATA[一切问题的开始 想象一下这个场景：我刚刚在我的网站shadynagy.com上安装了SSL证书。从我这边看一切都很完美——证书有效,由Sectigo颁发,距离过期还有很长时间(265天!)。但当我在 WhyNoPadlock.com 上运行快速SSL测试时,却看到了一些令人失望的红色X标记: ❌  强制HTTPS：未强制使用SSL ❌  中间证书无效：缺少中间(bundle)证书 我的心一沉。如果SSL…]]></description><link>https://www.shadynagy.com/zh/how-i-fixed-ssl-certificate-issues-on-my-website-a-complete-guide/</link><guid isPermaLink="false">https://www.shadynagy.com/zh/how-i-fixed-ssl-certificate-issues-on-my-website-a-complete-guide/</guid><category><![CDATA[Linux]]></category><pubDate>Wed, 11 Feb 2026 00:00:00 GMT</pubDate><dc:creator><![CDATA[Shady Nagy]]></dc:creator><atom:updated>2026-02-11T00:00:00.000Z</atom:updated><content:encoded><![CDATA[<img src="https://www.shadynagy.com/static/1c20b3e7aade9776484e9285bb5ab52e/flyd-mT7lXZPjk7Utghj6.jpg" alt="我如何修复网站SSL证书问题：完整指南" /><h2 id="一切问题的开始">一切问题的开始</h2><p>想象一下这个场景：我刚刚在我的网站shadynagy.com上安装了SSL证书。从我这边看一切都很完美——证书有效,由Sectigo颁发,距离过期还有很长时间(265天!)。但当我在<a href="https://www.whynopadlock.com/">WhyNoPadlock.com</a>上运行快速SSL测试时,却看到了一些令人失望的红色X标记:</p><p>❌ <strong>强制HTTPS：未强制使用SSL</strong><br/>
❌ <strong>中间证书无效：缺少中间(bundle)证书</strong></p><p>我的心一沉。如果SSL证书无法正常工作,那它有什么用?我的访问者可能会看到安全警告,搜索引擎可能会降低我的排名,最糟糕的是——我的网站看起来不专业。</p><p>但好消息是:我修复了所有问题,现在我将一步步向你展示我是如何做到的。</p><h2 id="理解问题所在">理解问题所在</h2><p>在深入解决方案之前,让我们先理解这些错误究竟意味着什么:</p><h3 id="问题1缺少中间证书">问题1:缺少中间证书</h3><p><strong>什么是中间证书?</strong></p><p>将SSL证书想象成一条信任链:</p><ul><li>你的浏览器信任某些”根”证书颁发机构(如Sectigo)</li><li>这些机构向下级机构颁发”中间”证书</li><li>这些下级机构向你的网站颁发证书</li></ul><p>你的网站需要同时向浏览器发送你的证书和中间证书。没有中间证书,浏览器无法验证信任链。</p><p><strong>实际影响:</strong></p><ul><li>某些浏览器显示安全警告</li><li>移动设备经常无法连接</li><li>你的网站对访问者来说看起来不安全</li></ul><h3 id="问题2没有http到https重定向">问题2:没有HTTP到HTTPS重定向</h3><p><strong>这意味着什么?</strong></p><p>当有人输入<code>http://shadynagy.com</code>(没有’s’)时,应该自动重定向到<code>https://shadynagy.com</code>。如果没有这个重定向:</p><ul><li>访问者可以通过不安全的HTTP访问你的网站</li><li>搜索引擎看到重复的内容(HTTP和HTTPS版本)</li><li>你失去了HTTPS带来的SEO优势</li></ul><h2 id="解决方案1修复缺失的中间证书">解决方案1:修复缺失的中间证书</h2><h3 id="步骤1理解根本原因">步骤1:理解根本原因</h3><p>我发现我的nginx配置使用了<strong>错误的指令</strong>。这是我原来的配置:</p><pre><code class="language-nginx"># ❌ 错误配置
ssl_certificate /etc/nginx/ssl/shadynagy.com.crt;
ssl_trusted_certificate /etc/nginx/ssl/shadynagy.com.ca-bundle;
</code></pre><p><strong>问题在于:</strong><code>ssl_trusted_certificate</code>用于OCSP stapling(一个性能特性),而不是向浏览器发送证书链!</p><h3 id="步骤2创建完整链证书">步骤2:创建完整链证书</h3><p>解决方案是创建一个”fullchain”证书,将你的证书与中间证书合并。</p><p><strong>命令:</strong></p><pre><code class="language-bash">cat /etc/nginx/ssl/shadynagy.com.crt /etc/nginx/ssl/shadynagy.com.ca-bundle &gt; /etc/nginx/ssl/shadynagy.com-fullchain.crt
</code></pre><p><strong>这个命令做什么:</strong></p><ul><li><code>cat</code> - 连接(合并)文件</li><li>第一个文件:你的网站证书</li><li>第二个文件:中间CA bundle</li><li><code>&gt;</code> - 输出到一个名为”fullchain”的新文件</li></ul><p><strong>示例解释:</strong>
想象你有两块拼图:</p><ol><li>你的证书(A块)</li><li>中间证书(B块)</li></ol><p>你正在把它们粘在一起,创建一个浏览器能够理解的完整拼图。</p><h3 id="步骤3更新nginx配置">步骤3:更新Nginx配置</h3><p>打开你的SSL配置文件:</p><pre><code class="language-bash">nano /etc/nginx/conf.d/shadynagy.com-ssl.conf
</code></pre><p><strong>更新为以下内容:</strong></p><pre><code class="language-nginx">server {
    listen 443 ssl http2;  # 端口443启用SSL和HTTP/2
    
    # ✅ 正确 - 使用fullchain证书
    ssl_certificate /etc/nginx/ssl/shadynagy.com-fullchain.crt;
    ssl_certificate_key /etc/nginx/ssl/shadynagy.com.key;
    
    # 可选:保留用于OCSP stapling(性能提升)
    ssl_trusted_certificate /etc/nginx/ssl/shadynagy.com.ca-bundle;
    
    root /var/www/shady-nagy.com/html;
    index index.html index.htm;
    server_name shadynagy.com www.shadynagy.com;
    
    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;
    
    location / {
        try_files $uri $uri/ =404;
    }
}
</code></pre><p><strong>关键更改说明:</strong></p><ol><li><p><strong><code>listen 443 ssl http2;</code></strong></p><ul><li><code>443</code> = HTTPS端口</li><li><code>ssl</code> = 启用SSL</li><li><code>http2</code> = 启用HTTP/2以获得更好性能</li><li>这替代了已弃用的<code>ssl on;</code>指令</li></ul></li><li><p><strong><code>ssl_certificate</code></strong> 现在指向fullchain而不仅仅是你的证书</p></li><li><p><strong><code>ssl_trusted_certificate</code></strong> 保留用于OCSP stapling(可选但推荐)</p></li></ol><h3 id="步骤4测试并应用更改">步骤4:测试并应用更改</h3><p><strong>测试配置:</strong></p><pre><code class="language-bash">nginx -t
</code></pre><p><strong>预期输出:</strong></p><pre><code>nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
</code></pre><p>如果看到任何错误,请仔细检查文件路径和语法!</p><p><strong>应用更改:</strong></p><pre><code class="language-bash">systemctl reload nginx
</code></pre><p><strong>为什么用reload而不是restart?</strong></p><ul><li><code>reload</code>在不断开连接的情况下应用更改</li><li><code>restart</code>会短暂使你的网站离线</li></ul><h3 id="-此修复的好处">✅ 此修复的好处</h3><ul><li>✅ 所有浏览器都能验证你的SSL证书</li><li>✅ 不再出现”无效证书”警告</li><li>✅ 移动设备正常连接</li><li>✅ 更好的信任度和SEO排名</li><li>✅ 专业的外观</li></ul><h2 id="解决方案2强制https重定向">解决方案2:强制HTTPS重定向</h2><h3 id="调查过程">调查过程</h3><p>首先,我检查了HTTP配置:</p><pre><code class="language-bash">cat /etc/nginx/conf.d/shadynagy.com.conf
</code></pre><p>我发现了这个:</p><pre><code class="language-nginx"># ❌ 错误配置
server {
    listen 80;
    root /var/www/shady-nagy.com/html;
    server_name shadynagy.com www.shadynagy.com;
    
    location / {
        try_files $uri $uri/ =404;
    }
    
    return 301 https://$server_name$request_uri;  # 永远不会执行到!
}
</code></pre><p><strong>问题在于:</strong> Nginx从上到下读取配置。当请求进来时:</p><ol><li>它匹配<code>location /</code>块</li><li>处理请求</li><li>永远不会执行到重定向行</li></ol><p>这就像在道路之后才放置”绕行”标志!</p><h3 id="步骤1创建正确的重定向配置">步骤1:创建正确的重定向配置</h3><p><strong>修复方案:</strong></p><pre><code class="language-nginx"># ✅ 正确配置
server {
    listen 80;
    listen [::]:80;  # 同时监听IPv6
    server_name shadynagy.com www.shadynagy.com;
    
    # 将所有HTTP流量重定向到HTTPS
    return 301 https://$host$request_uri;
}
</code></pre><p><strong>各部分的作用:</strong></p><ul><li><strong><code>listen 80;</code></strong> - 监听HTTP请求(端口80)</li><li><strong><code>listen [::]:80;</code></strong> - 同时监听IPv6 HTTP请求</li><li><strong><code>server_name</code></strong> - 此配置适用于哪些域名</li><li><strong><code>return 301</code></strong> - 发送永久重定向(301状态码)</li><li><strong><code>$host</code></strong> - 用户输入的域名(保留www与非www)</li><li><strong><code>$request_uri</code></strong> - 他们请求的路径(例如/about或/contact)</li></ul><p><strong>实际示例:</strong></p><p>如果有人访问:<code>http://www.shadynagy.com/how-i-fixed-ssl-certificate-issues-on-my-website-a-complete-guide</code><br/>
他们会被重定向到:<code>https://www.shadynagy.com/how-i-fixed-ssl-certificate-issues-on-my-website-a-complete-guide</code></p><h3 id="步骤2应用并测试">步骤2:应用并测试</h3><p><strong>测试配置:</strong></p><pre><code class="language-bash">nginx -t
</code></pre><p><strong>重新加载nginx:</strong></p><pre><code class="language-bash">systemctl reload nginx
</code></pre><p><strong>测试重定向:</strong></p><pre><code class="language-bash">curl -I http://shadynagy.com
</code></pre><p><strong>预期输出:</strong></p><pre><code>HTTP/1.1 301 Moved Permanently
Location: https://shadynagy.com/
</code></pre><p>完美!但是等等…</p><h3 id="意外发现防火墙阻止了端口80">意外发现:防火墙阻止了端口80!</h3><p>当我在浏览器中测试时,仍然不起作用!经过一番调查:</p><pre><code class="language-bash">firewall-cmd --list-all
</code></pre><p><strong>输出:</strong></p><pre><code>services: https
</code></pre><p><strong>注意到缺少什么了吗?HTTP(端口80)!</strong></p><p>重定向配置完全正确,但我的防火墙阻止了传入的HTTP流量。这就像安装了前门,却在前面建了一堵墙!</p><h3 id="步骤3打开防火墙">步骤3:打开防火墙</h3><p><strong>添加HTTP服务:</strong></p><pre><code class="language-bash">firewall-cmd --permanent --add-service=http
</code></pre><p><strong>重新加载防火墙:</strong></p><pre><code class="language-bash">firewall-cmd --reload
</code></pre><p><strong>验证是否成功:</strong></p><pre><code class="language-bash">firewall-cmd --list-all
</code></pre><p><strong>现在你应该看到:</strong></p><pre><code>services: http https
</code></pre><h3 id="-此修复的好处-1">✅ 此修复的好处</h3><ul><li>✅ 所有访问者自动使用HTTPS</li><li>✅ SEO没有重复内容问题</li><li>✅ 访问者获得更好的安全性</li><li>✅ 改善搜索引擎排名</li><li>✅ 浏览器中显示绿色挂锁</li><li>✅ 与访问者建立信任</li></ul><h2 id="完整诊断命令">完整诊断命令</h2><p>以下是我在故障排除期间使用的所有命令:</p><h3 id="检查nginx配置">检查Nginx配置</h3><pre><code class="language-bash"># 查找所有监听端口80的server块
grep -r &quot;listen 80&quot; /etc/nginx/

# 测试nginx配置语法
nginx -t

# 显示完整nginx配置
nginx -T

# 查看特定server块
nginx -T 2&gt;/dev/null | grep -A 15 &quot;server_name shadynagy.com&quot;
</code></pre><h3 id="检查nginx状态">检查Nginx状态</h3><pre><code class="language-bash"># nginx是否运行?
systemctl status nginx

# 重新加载nginx(无停机应用更改)
systemctl reload nginx

# 重启nginx(短暂停机)
systemctl restart nginx

# 检查错误日志
tail -20 /var/log/nginx/error.log
</code></pre><h3 id="测试http到https重定向">测试HTTP到HTTPS重定向</h3><pre><code class="language-bash"># 测试重定向(绕过浏览器缓存)
curl -I http://shadynagy.com

# 跟随所有重定向
curl -IL http://shadynagy.com
</code></pre><h3 id="检查端口状态">检查端口状态</h3><pre><code class="language-bash"># nginx是否在端口80上监听?
ss -tlnp | grep :80

# nginx是否在端口443上监听?
ss -tlnp | grep :443
</code></pre><h3 id="检查防火墙">检查防火墙</h3><pre><code class="language-bash"># 列出所有防火墙规则
firewall-cmd --list-all

# 永久添加HTTP
firewall-cmd --permanent --add-service=http

# 永久添加HTTPS
firewall-cmd --permanent --add-service=https

# 应用更改
firewall-cmd --reload
</code></pre><h2 id="最终配置文件">最终配置文件</h2><h3 id="http配置端口80">HTTP配置(端口80)</h3><p><strong>文件:</strong> <code>/etc/nginx/conf.d/shadynagy.com.conf</code></p><pre><code class="language-nginx">server {
    listen 80;
    listen [::]:80;
    server_name shadynagy.com www.shadynagy.com;
    
    return 301 https://$host$request_uri;
}
</code></pre><h3 id="https配置端口443">HTTPS配置(端口443)</h3><p><strong>文件:</strong> <code>/etc/nginx/conf.d/shadynagy.com-ssl.conf</code></p><pre><code class="language-nginx">server {
    listen 443 ssl http2;
    
    ssl_certificate /etc/nginx/ssl/shadynagy.com-fullchain.crt;
    ssl_certificate_key /etc/nginx/ssl/shadynagy.com.key;
    ssl_trusted_certificate /etc/nginx/ssl/shadynagy.com.ca-bundle;
    
    root /var/www/shady-nagy.com/html;
    index index.html index.htm index.nginx-debian.html;
    server_name shadynagy.com www.shadynagy.com;
    
    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;
    
    location / {
        add_header Cache-Control &quot;no-cache, no-store, must-revalidate&quot;;
        add_header Pragma &quot;no-cache&quot;;
        add_header Expires 0;
        try_files $uri $uri/ =404;
    }
}
</code></pre><h2 id="结果一切修复完成">结果:一切修复完成!✅</h2><p>在实施所有修复后,我在WhyNoPadlock.com上的SSL测试显示:</p><p>✅ <strong>SSL连接</strong> - 通过<br/>
✅ <strong>有效证书</strong> - SSL证书正确安装<br/>
✅ <strong>强制HTTPS</strong> - Web服务器强制使用SSL<br/>
✅ <strong>域名匹配</strong> - 证书与域名匹配<br/>
✅ <strong>签名</strong> - 使用sha256WithRSAEncryption<br/>
✅ <strong>过期日期</strong> - 证书当前有效(2026-11-03过期)<br/>
✅ <strong>混合内容</strong> - 无混合内容  </p><p><strong>满分!</strong></p><h2 id="常见问题解答">常见问题解答</h2><h3 id="q1为什么需要中间证书">Q1:为什么需要中间证书?</h3><p><strong>答:</strong>中间证书创建了从你的网站到受信任根证书颁发机构的”信任链”。没有它:</p><ul><li>浏览器无法验证你的证书是否合法</li><li>访问者会看到可怕的安全警告</li><li>移动设备经常无法连接</li><li>你的网站看起来不专业且不安全</li></ul><p>可以这样想:如果有人自我介绍说”我是约翰,莎拉的朋友”,你可能不会信任他。但如果莎拉是你最好的朋友,她为约翰担保,你就会信任他。中间证书就是莎拉为你的网站证书担保。</p><h3 id="q2nginx中ssl_certificate和ssl_trusted_certificate有什么区别">Q2:nginx中<code>ssl_certificate</code>和<code>ssl_trusted_certificate</code>有什么区别?</h3><p><strong>答:</strong>很好的问题!</p><ul><li><p><strong><code>ssl_certificate</code></strong>:这是nginx发送给浏览器以证明你网站身份的内容。它应该包含你的证书和中间证书(fullchain)。</p></li><li><p><strong><code>ssl_trusted_certificate</code></strong>:这用于OCSP stapling,一个性能特性。Nginx使用它代表客户端验证证书吊销状态。它不会发送给浏览器。</p></li></ul><p><strong>类比:</strong> </p><ul><li><code>ssl_certificate</code> = 你向他人展示的身份证</li><li><code>ssl_trusted_certificate</code> = 你自己参考的规则副本</li></ul><h3 id="q3为什么在重定向中使用host而不是server_name">Q3:为什么在重定向中使用<code>$host</code>而不是<code>$server_name</code>?</h3><p><strong>答:</strong> </p><ul><li><strong><code>$host</code></strong>:用户在浏览器中输入的确切域名(例如<a href="http://www.shadynagy.com%E6%88%96shadynagy.com">www.shadynagy.com或shadynagy.com</a>)</li><li><strong><code>$server_name</code></strong>:配置中的第一个server_name</li></ul><p><strong>示例场景:</strong></p><ul><li>你的配置有:<code>server_name shadynagy.com www.shadynagy.com;</code></li><li>用户访问:<code>http://www.shadynagy.com</code></li></ul><p>使用<code>$server_name</code>:重定向到<code>https://shadynagy.com</code>(去掉www)<br/>
使用<code>$host</code>:重定向到<code>https://www.shadynagy.com</code>(保留www)</p><p>使用<code>$host</code>尊重用户输入的内容,避免不必要的额外重定向。</p><h3 id="q4为什么重定向在curl中有效但在浏览器中无效">Q4:为什么重定向在curl中有效但在浏览器中无效?</h3><p><strong>答:</strong>这通常是以下三个问题之一:</p><ol><li><p><strong>防火墙阻止端口80</strong>(我的问题!) - 重定向配置正确,但传入的HTTP流量从未到达nginx。</p></li><li><p><strong>浏览器缓存</strong> - 浏览器会积极缓存重定向。解决方案:在无痕模式下测试或清除缓存。</p></li><li><p><strong>DNS缓存</strong> - 你的计算机可能有旧的DNS记录。解决方案:刷新DNS缓存或等待几个小时。</p></li></ol><p><strong>专业提示:</strong>总是先用<code>curl -I</code>测试,因为它绕过所有缓存!</p><h3 id="q5应该使用return还是rewrite进行https重定向">Q5:应该使用<code>return</code>还是<code>rewrite</code>进行HTTPS重定向?</h3><p><strong>答:</strong>总是使用<code>return</code>进行重定向!</p><p><strong><code>return</code>(推荐):</strong></p><pre><code class="language-nginx">return 301 https://$host$request_uri;
</code></pre><ul><li>更快(nginx立即停止处理)</li><li>意图更清晰</li><li>不易出错</li></ul><p><strong><code>rewrite</code>(避免用于简单重定向):</strong></p><pre><code class="language-nginx">rewrite ^ https://$host$request_uri permanent;
</code></pre><ul><li>较慢(使用正则表达式引擎)</li><li>更复杂</li><li>容易创建无限循环</li></ul><p><strong>经验法则:</strong>使用<code>return</code>进行重定向,只在需要修改URL结构时使用<code>rewrite</code>。</p><h3 id="q6nginx的reload和restart有什么区别">Q6:nginx的<code>reload</code>和<code>restart</code>有什么区别?</h3><p><strong>答:</strong></p><p><strong><code>systemctl reload nginx</code></strong>(推荐):</p><ul><li>优雅地应用配置更改</li><li>保持现有连接活动</li><li>无停机时间</li><li>应用前测试配置(如果配置损坏则不会重新加载)</li></ul><p><strong><code>systemctl restart nginx</code></strong>(谨慎使用):</p><ul><li>完全停止nginx,然后重新启动</li><li>断开所有活动连接</li><li>短暂停机(通常几毫秒到几秒)</li><li>当reload不起作用时有用</li></ul><p><strong>何时使用restart而不是reload:</strong></p><ul><li>添加新模块时</li><li>reload无法获取更改时</li><li>调试奇怪问题时</li></ul><h3 id="q7如何知道防火墙是否阻止流量">Q7:如何知道防火墙是否阻止流量?</h3><p><strong>答:</strong>运行这些诊断命令:</p><pre><code class="language-bash"># 检查nginx是否在端口80上监听
ss -tlnp | grep :80
# 如果在这里看到nginx,说明它正在监听

# 检查防火墙规则
firewall-cmd --list-all
# 在服务列表中查找&#x27;http&#x27;

# 从服务器外部测试
curl -I http://your-domain.com
# 如果超时,防火墙可能正在阻止
</code></pre><p>如果nginx正在监听但从外部curl超时→防火墙就是问题所在!</p><h3 id="q8我的ssl测试显示混合内容警告这是什么意思">Q8:我的SSL测试显示”混合内容”警告。这是什么意思?</h3><p><strong>答:</strong>当你的HTTPS页面通过HTTP加载资源(图片、脚本、CSS)时就会发生混合内容。</p><p><strong>问题示例:</strong></p><pre><code class="language-html">&lt;!-- ❌ 错误 - 在HTTPS页面上通过HTTP加载图片 --&gt;
&lt;img src=&quot;http://shadynagy.com/image.jpg&quot;&gt;
</code></pre><p><strong>解决方案:</strong></p><pre><code class="language-html">&lt;!-- ✅ 好 - 使用HTTPS --&gt;
&lt;img src=&quot;https://shadynagy.com/image.jpg&quot;&gt;

&lt;!-- ✅ 好 - 使用协议相对URL --&gt;
&lt;img src=&quot;//shadynagy.com/image.jpg&quot;&gt;

&lt;!-- ✅ 最佳 - 使用相对URL --&gt;
&lt;img src=&quot;/image.jpg&quot;&gt;
</code></pre><p><strong>检查混合内容:</strong></p><ol><li>在Chrome中打开你的网站</li><li>按F12(开发者工具)</li><li>在Console标签中查找警告</li></ol><h3 id="q9每次续订ssl证书都需要这样做吗">Q9:每次续订SSL证书都需要这样做吗?</h3><p><strong>答:</strong>部分需要。</p><p><strong>需要再次做的:</strong></p><ul><li>创建新的fullchain证书(合并新证书+中间证书)</li><li>替换旧的fullchain文件</li></ul><p><strong>不需要再次做的:</strong></p><ul><li>修改nginx配置文件(它们将引用相同的路径)</li><li>更改防火墙规则</li><li>设置重定向</li></ul><p><strong>专业提示:</strong>使用Let’s Encrypt/Certbot自动化证书续订,它会自动处理fullchain!</p><h3 id="q10在正式上线前可以测试ssl配置吗">Q10:在正式上线前可以测试SSL配置吗?</h3><p><strong>答:</strong>当然可以!方法如下:</p><p><strong>1. 测试nginx配置语法:</strong></p><pre><code class="language-bash">nginx -t
</code></pre><p><strong>2. 使用curl在本地测试:</strong></p><pre><code class="language-bash"># 测试重定向
curl -I http://localhost

# 测试HTTPS(本地测试可能会出现证书警告)
curl -Ik https://localhost
</code></pre><p><strong>3. 使用在线工具:</strong></p><ul><li><a href="https://www.ssllabs.com/ssltest/">SSL Labs</a> - 综合SSL测试</li><li><a href="https://www.whynopadlock.com/">WhyNoPadlock</a> - 快速SSL检查</li><li><a href="https://www.sslshopper.com/ssl-checker.html">SSL Shopper</a> - 证书链检查器</li></ul><p><strong>4. 在测试环境中测试:</strong>
如果可能,设置一个测试子域(如staging.shadynagy.com)并先在那里测试。</p><h3 id="q11什么是http2为什么要添加它">Q11:什么是HTTP/2,为什么要添加它?</h3><p><strong>答:</strong>HTTP/2是HTTP协议的更新、更快版本。</p><p><strong>好处:</strong></p><ul><li>✅ 多路复用:多个文件通过一个连接同时下载</li><li>✅ 头部压缩:更小的请求/响应</li><li>✅ 服务器推送:服务器可以在浏览器请求前发送文件</li><li>✅ 更好的移动性能</li></ul><p><strong>如何启用:</strong></p><pre><code class="language-nginx">listen 443 ssl http2;  # 只需在这里添加&#x27;http2&#x27;!
</code></pre><p><strong>要求:</strong></p><ul><li>必须启用HTTPS(HTTP/2需要SSL)</li><li>Nginx 1.9.5或更高版本</li><li>OpenSSL 1.0.2或更高版本</li></ul><p><strong>验证:</strong></p><pre><code class="language-bash">curl -I --http2 https://shadynagy.com
# 在响应中查找&quot;HTTP/2 200&quot;
</code></pre><h3 id="q12如果忘记使防火墙规则永久化会怎样">Q12:如果忘记使防火墙规则永久化会怎样?</h3><p><strong>答:</strong>如果不使用<code>--permanent</code>,你的防火墙规则在重启服务器后会消失!</p><p><strong>非永久化(重启后丢失):</strong></p><pre><code class="language-bash">firewall-cmd --add-service=http  # ❌ 重启后消失
</code></pre><p><strong>永久化(重启后保留):</strong></p><pre><code class="language-bash">firewall-cmd --permanent --add-service=http  # ✅ 重启后保留
firewall-cmd --reload  # 立即应用
</code></pre><p><strong>检查规则是否永久化:</strong></p><pre><code class="language-bash"># 显示运行时(当前)规则
firewall-cmd --list-all

# 显示永久(已保存)规则
firewall-cmd --permanent --list-all
</code></pre><p>如果它们不匹配,你需要使更改永久化!</p><h3 id="q13应该多久检查一次ssl证书">Q13:应该多久检查一次SSL证书?</h3><p><strong>答:</strong>这是一个好的维护计划:</p><p><strong>每月:</strong></p><ul><li>检查证书过期日期</li><li>在WhyNoPadlock.com运行快速SSL测试</li></ul><p><strong>过期前:</strong></p><ul><li>在过期前30天续订证书</li><li>立即测试新证书</li><li>如果可能设置自动续订</li></ul><p><strong>服务器更新后:</strong></p><ul><li>在任何nginx/OpenSSL更新后测试SSL</li><li>验证重定向仍然有效</li></ul><p><strong>设置监控:</strong>
许多服务提供免费SSL监控:</p><ul><li>UptimeRobot</li><li>Pingdom</li><li>SSL Labs监控</li></ul><p>它们会在证书过期前通过电子邮件通知你!</p><h3 id="q14如果一台服务器上有多个域名怎么办">Q14:如果一台服务器上有多个域名怎么办?</h3><p><strong>答:</strong>你需要为每个域名创建单独的server块。</p><p><strong>多域名示例:</strong></p><pre><code class="language-nginx"># 域名1:shadynagy.com
server {
    listen 80;
    server_name shadynagy.com www.shadynagy.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name shadynagy.com www.shadynagy.com;
    ssl_certificate /etc/nginx/ssl/shadynagy.com-fullchain.crt;
    ssl_certificate_key /etc/nginx/ssl/shadynagy.com.key;
    root /var/www/shadynagy.com;
}

# 域名2:myotherdomain.com
server {
    listen 80;
    server_name myotherdomain.com www.myotherdomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name myotherdomain.com www.myotherdomain.com;
    ssl_certificate /etc/nginx/ssl/myotherdomain.com-fullchain.crt;
    ssl_certificate_key /etc/nginx/ssl/myotherdomain.com.key;
    root /var/www/myotherdomain.com;
}
</code></pre><p><strong>替代方案:</strong>使用通配符证书或多域名(SAN)证书,用一个证书覆盖多个域名。</p><h3 id="q15为什么即使完成所有这些修复后我的证书仍显示为不安全">Q15:为什么即使完成所有这些修复后,我的证书仍显示为”不安全”?</h3><p><strong>答:</strong>检查这些常见问题:</p><ol><li><p><strong>证书名称不匹配:</strong></p><ul><li>证书是为<code>www.shadynagy.com</code>签发的,但你正在访问<code>shadynagy.com</code></li><li>解决方案:获取覆盖两者的证书(SAN证书)</li></ul></li><li><p><strong>证书过期:</strong></p><ul><li>检查过期日期:<code>openssl x509 -in certificate.crt -noout -dates</code></li><li>解决方案:续订证书</li></ul></li><li><p><strong>混合内容:</strong></p><ul><li>HTTPS页面加载HTTP资源</li><li>检查浏览器控制台中的警告</li></ul></li><li><p><strong>nginx配置中的域名错误:</strong></p><ul><li><code>server_name</code>与你访问的域名不匹配</li><li>解决方案:将所有域名变体添加到<code>server_name</code></li></ul></li><li><p><strong>证书不受信任:</strong></p><ul><li>使用自签名证书</li><li>解决方案:从受信任的CA获取证书(Let’s Encrypt是免费的!)</li></ul></li></ol><p><strong>快速诊断:</strong></p><pre><code class="language-bash"># 检查正在提供什么证书
openssl s_client -connect shadynagy.com:443 -servername shadynagy.com
</code></pre><h2 id="有用的在线ssl测试工具">有用的在线SSL测试工具</h2><p>🔗 <strong><a href="https://www.ssllabs.com/ssltest/">SSL Labs</a></strong> - 最全面的SSL测试,给你A-F评级</p><p>🔗 <strong><a href="https://www.whynopadlock.com/">WhyNoPadlock</a></strong> - 快速视觉检查常见SSL问题</p><p>🔗 <strong><a href="https://www.sslshopper.com/ssl-checker.html">SSL Shopper</a></strong> - 检查证书安装和链</p><p>🔗 <strong><a href="https://securityheaders.com/">Security Headers</a></strong> - 测试HTTP安全头</p><p>🔗 <strong><a href="https://www.htbridge.com/ssl/">High-Tech Bridge</a></strong> - 详细的SSL/TLS和安全评估</p><h2 id="关键要点">关键要点</h2><h3 id="关于中间证书">关于中间证书:</h3><p>✅ 始终创建fullchain证书(证书+CA bundle)<br/>
✅ 对fullchain使用<code>ssl_certificate</code>指令<br/>
✅ 仅对OCSP stapling使用<code>ssl_trusted_certificate</code><br/>
✅ 安装后使用在线工具测试  </p><h3 id="关于https重定向">关于HTTPS重定向:</h3><p>✅ 使用<code>return 301</code>进行重定向(不是<code>rewrite</code>)<br/>
✅ 将重定向放在location块之前<br/>
✅ 使用<code>$host</code>保留用户的域名输入<br/>
✅ 记得在防火墙中打开端口80!  </p><h3 id="关于测试">关于测试:</h3><p>✅ 重新加载前始终用<code>nginx -t</code>测试nginx配置<br/>
✅ 使用<code>curl</code>绕过浏览器缓存<br/>
✅ 用<code>firewall-cmd --list-all</code>检查防火墙规则<br/>
✅ 用<code>ss -tlnp</code>验证端口监听  </p><h3 id="关于维护">关于维护:</h3><p>✅ 设置证书过期提醒<br/>
✅ 使防火墙规则永久化<br/>
✅ 保持nginx和OpenSSL更新<br/>
✅ 定期测试SSL配置  </p><h2 id="最后的思考">最后的思考</h2><p>SSL配置一开始可能看起来令人生畏,但一旦理解了各个组成部分,就会觉得很有逻辑:</p><ol><li><strong>证书链</strong> - 证明你的身份</li><li><strong>HTTPS重定向</strong> - 强制安全连接</li><li><strong>防火墙规则</strong> - 允许流量到达你的服务器</li></ol><p>将问题分解成这些组件使诊断和修复变得更加容易。</p><p>最重要的教训?<strong>当某些东西不起作用时,逐层检查:</strong></p><ul><li>nginx配置是否正确?(<code>nginx -t</code>)</li><li>nginx是否在监听?(<code>ss -tlnp</code>)</li><li>防火墙是否允许流量?(<code>firewall-cmd --list-all</code>)</li><li>本地是否工作?(<code>curl -I http://localhost</code>)</li></ul><p>这种系统化的方法为我节省了数小时的挫折!</p><h2 id="反馈和问题">反馈和问题</h2><p>我们很乐意听到你对本教程的反馈!如果你有任何问题或改进建议,请随时联系。你可以在下面留言,或通过以下渠道联系我们:</p><ol><li>电子邮件:<a href="mailto:info@shadynagy.com">info@shadynagy.com</a></li><li>Twitter:<a href="https://twitter.com/ShadyNagy_">@ShadyNagy_</a></li><li>LinkedIn:<a href="https://www.linkedin.com/in/shadynagy/">Shady Nagy</a></li><li>GitHub:<a href="https://github.com/shadynagy/">ShadyNagy</a></li></ol><p><strong>觉得有帮助?</strong>与正在为SSL配置苦恼的人分享吧!</p>]]></content:encoded></item></channel></rss>