6

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

 2 years ago
source link: https://www.huhexian.com/26803.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

2022-02-1509:34:39评论528字

虽然使用加密连接https更安全,但在某些特定情况下,只能使用http来代替加密连接。在宝塔面板中开启ssl强制跳转后,全站所有目录都会无一例外的跳转到https。本来以为加个if指定条件不跳转就够了,结果没办法实现。nginx不支持if逻辑和嵌套语法。最后找到了很多信息,终于可以实现强制跳转开启后指定目录不跳转https的方案。

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

方案

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

  1. if ($server_port !~ 443){
  2. rewrite ^(/.*)$ https://$host$1 permanent;

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

  1. set $flag 0;
  2. if ($server_port !~ 443) {
  3. set $flag "${flag}1";
  4. if ($request_uri !~ "/AAA"){
  5. set $flag "${flag}2";
  6. if ($flag = "012"){
  7. rewrite ^(.*)$ https://$host$1 permanent;

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

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK