宝塔加密访问(Nginx)导致伪静态失效

背景说明

  • 当设置根目录加密访问,会导致伪静态失效
server
{
    ...

    #Directory protection rules, do not manually delete
    include /www/server/panel/vhost/nginx/dir_auth/abc.com/*.conf;
    # 这个是加密访问配置规则

    ...

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/abc.com.conf;
    #REWRITE-END
    # 这个是伪静态规则

    ...
}
  • 加密访问配置规则
location ~* ^/* {
    #AUTH_START
    auth_basic "Authorization";
    auth_basic_user_file /www/server/pass/abc.com/root.pass;
    include enable-php-72.conf;
    #AUTH_END
}
  • 伪静态规则
location / {
     if (!-e $request_filename){
            rewrite ^/index.php(.*)$ /index.php?p=$1 last;
                rewrite ^(.*)$ /index.php?s=$1 last;
     }
}
  • 理论上 Nginx 配置文件读取是从上到下的,所以理论上将伪静态规则放在加密访问规则前面,即可
  • 但是实践中不生效
  • 加密规则的匹配优先级比伪静态规则高

解决方法

  • 注释掉加密规则文件
  • 将加密规则写进伪静态配置内
  • 配置文件
server
{
    ...

    #Directory protection rules, do not manually delete
    #include /www/server/panel/vhost/nginx/dir_auth/abc.com/*.conf;
    # 这个是加密访问配置规则 注释掉 把内容写到下面的伪静态规则里面

    ...

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/abc.com.conf;
    #REWRITE-END
    # 这个是伪静态规则

    ...
}
  • 伪静态
location / {
    #AUTH_START
    auth_basic "Authorization";
    auth_basic_user_file /www/server/pass/abc.com/root.pass;
    include enable-php-72.conf;
    #AUTH_END
     if (!-e $request_filename){
            rewrite ^/index.php(.*)$ /index.php?p=$1 last;
            rewrite ^(.*)$ /index.php?s=$1 last;
     }

}

本文链接:

https://www.antixu.com/archives/436/
1 + 1 =
快来做第一个评论的人吧~