欢迎光临
我们一直在努力

宝塔面板开启SSL强制跳转后实现指定目录不跳转HTTPS

前言

虽然使用加密连接https更加安全,但在一些特定情况下却只能使用http不能使用加密连接;
在宝塔面板中开启SSL强制跳转后全站所有目录都会跳转到https并没有例外的选项;
本来以为加一条if指定条件不跳转就行了,结果不能实现,原来Nginx不支持if逻辑和嵌套语法;
最后找了不少资料,终于可以实现开启强制跳转后指定目录不跳转https的方案了;

方案

宝塔面板开启强制跳转httpsNginx的配置文件是如下代码;

    <span class="hljs-attribute"><span class="kwd">if</span></span> <span class="pun">(</span><span class="hljs-variable"><span class="pln">$server_port</span></span> <span class="pun">!</span><span class="hljs-regexp"><span class="pun">~</span> <span class="lit">443</span><span class="pun">)</span></span><span class="pun">{</span>
        <span class="hljs-attribute"><span class="pln">rewrite</span></span><span class="hljs-regexp"> <span class="pun">^(/.*)</span><span class="pln">$</span></span><span class="pln"> https</span><span class="pun">:</span><span class="com">//</span><span class="hljs-variable"><span class="com">$host</span></span><span class="hljs-variable"><span class="com">$1</span></span> <span class="hljs-literal"><span class="com">permanent</span></span><span class="com">;</span>
    <span class="pun">}</span>

然后我们在网站-设置-配置文件中用以下代码替换掉上面的代码即可;

    <span class="hljs-attribute"><span class="kwd">set</span></span> <span class="hljs-variable"><span class="pln">$flag</span></span> <span class="hljs-number"><span class="lit">0</span></span><span class="pun">;</span>
    <span class="hljs-attribute"><span class="kwd">if</span></span> <span class="pun">(</span><span class="hljs-variable"><span class="pln">$server_port</span></span> <span class="pun">!</span><span class="hljs-regexp"><span class="pun">~</span> <span class="lit">443</span><span class="pun">)</span></span> <span class="pun">{</span>
        <span class="hljs-attribute"><span class="kwd">set</span></span> <span class="hljs-variable"><span class="pln">$flag</span></span> <span class="hljs-string"><span class="str">"</span><span class="hljs-variable"><span class="str">${flag}</span></span><span class="str">1"</span></span><span class="pun">;</span>
    <span class="pun">}</span>
    <span class="hljs-attribute"><span class="kwd">if</span></span> <span class="pun">(</span><span class="hljs-variable"><span class="pln">$request_uri</span></span> <span class="pun">!</span><span class="hljs-regexp"><span class="pun">~</span> <span class="str">"/AAA"</span><span class="pun">)</span></span><span class="pun">{</span>
        <span class="hljs-attribute"><span class="kwd">set</span></span> <span class="hljs-variable"><span class="pln">$flag</span></span> <span class="hljs-string"><span class="str">"</span><span class="hljs-variable"><span class="str">${flag}</span></span><span class="str">2"</span></span><span class="pun">;</span>
    <span class="pun">}</span>
    <span class="hljs-attribute"><span class="kwd">if</span></span> <span class="pun">(</span><span class="hljs-variable"><span class="pln">$flag</span></span> <span class="pun">=</span> <span class="hljs-string"><span class="str">"012"</span></span><span class="pun">){</span>
         <span class="hljs-attribute"><span class="pln">rewrite</span></span><span class="hljs-regexp"> <span class="pun">^(.*)</span><span class="pln">$</span></span><span class="pln">  https</span><span class="pun">:</span><span class="com">//</span><span class="hljs-variable"><span class="com">$host</span></span><span class="hljs-variable"><span class="com">$1</span></span> <span class="hljs-literal"><span class="com">permanent</span></span><span class="com">;</span>
    <span class="pun">}</span>

条件解释:满足非443端口并且域名目录非/AAA时跳转,包含/AAA目录不跳转;

说明

我们指定不跳转的目录是/AAA但如果有同名目录,比如/AAAA/AAABB同样也不会跳转;

赞(0)
未经允许不得转载:798VPS » 宝塔面板开启SSL强制跳转后实现指定目录不跳转HTTPS
分享到: 更多 (0)