树莓派/Debian HTTP 到 HTTPS 端口重定向
<h2 id="一前言">一、前言</h2><p>前文我们讲了 树莓派/Debian Apache2 配置自建 CA 实现 HTTPS(SSL) 服务 ,实现了通过 SSL 建立安全连接,但依然可以通过 HTTP 访问。本文讲述如何将 Raspberry/Debian HTTP 默认的 80 端口,重定向到 HTTPS 的 443 端口。</p>
<h2 id="二修改-http-配置文件-000-defaultconf">二、修改 HTTP 配置文件 000-default.conf</h2>
<h3 id="1-开始编辑-000-defaultconf">1. 开始编辑 000-default.conf</h3>
<pre><code class="language-shell">sudo vim /etc/apache2/sites-available/000-default.conf
</code></pre>
<h3 id="2-添加语句">2. 添加语句</h3>
<p>在任意一处添加语句:</p>
<pre><code class="language-conf">ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</code></pre>
<ul>
<li>
<p>如果<strong>没有申请域名</strong>,可替换为 IP 地址:</p>
<pre><code>ServerName 192.168.0.130
ServerAlias 192.168.0.130
Redirect permanent / https://192.168.0.130/
</code></pre>
<ul>
<li>如果替换的 IP 地址是公网 IP ,不会影响。</li>
<li>如果替换的 IP 地址是本地 IP ,则只能由同一网段内的 计算机 访问。(比如我用到的 192.168.0.130)</li>
</ul>
</li>
</ul>
<h3 id="3-wq-保存退出">3. <code>:wq</code> 保存退出</h3>
<h2 id="三修改-https-配置文件-default-sslconf">三、修改 HTTPS 配置文件 default-ssl.conf</h2>
<h3 id="1-开始编辑-000-defaultconf-1">1. 开始编辑 000-default.conf</h3>
<pre><code class="language-shell">sudo vim /etc/apache2/sites-available/default-ssl.conf
</code></pre>
<h3 id="2-修改语句">2. 修改语句</h3>
<p>修改 <code>ServerName</code> 后面的字符串为 <strong>域名</strong> 。</p>
<pre><code>ServerName example.com
</code></pre>
<ul>
<li>
<p>如果<strong>没有申请域名</strong>,可替换为 IP 地址,例:</p>
<pre><code class="language-conf">ServerName 192.168.0.130
</code></pre>
</li>
</ul>
<h3 id="2-添加语句-1">2. 添加语句</h3>
<p>在 <code>ServerName</code> 语句后添加语句:</p>
<pre><code class="language-conf">ServerAlias www.example.com
Protocols h2 http:/1.1
</code></pre>
<ul>
<li>
<p>如果<strong>没有申请域名</strong>,可替换为 IP 地址:</p>
<pre><code>ServerAlias 192.168.0.130
Protocols h2 http:/1.1
</code></pre>
<ul>
<li>如果替换的 IP 地址是公网 IP ,不会影响。</li>
<li>如果替换的 IP 地址是本地 IP ,则只能由同一网段内的 计算机 访问。(比如我用到的 192.168.0.130)</li>
</ul>
</li>
</ul>
<p>使用上述配置,我们将强制使用 HTTPS 。</p>
<h2 id="四-重新加载apache服务以使更改生效">四、 重新加载Apache服务以使更改生效</h2>
<pre><code class="language-shell">sudo systemctl reload apache2
sudo /etc/init.d/apache2 restart
</code></pre>
<h2 id="五成功测试">五、成功测试</h2>
<p>完成上述步骤且无错误后,打开浏览器输入:</p>
<pre><code>http://<域名或IP>
</code></pre>
<p>就会跳转到:</p>
<pre><code>https://<域名或IP>
</code></pre>
<h2 id="六https-实现-www-重定向到-非www-网页">六、HTTPS 实现 www 重定向到 非www 网页</h2>
<p>请参考博客 HTTPS 实现 www 重定向到 非www 网页 。</p><br><br>
来源:https://www.cnblogs.com/Yogile/p/12493961.html
頁:
[1]