Typecho Nginx 开启https(解决其他页面404问题)

本文假设你已经申请好了证书,并已经配置到服务器

1、在项目根目录下的配置文件config.inc.php中添加如下代码,让后台访问https资源,不加的话后台登录仍然访问http;

define('__TYPECHO_SECURE__',true);

2、nginx配置文件中,在你解析443端口的server中,在localhost中添加如下代码,地址带参数跳转,不加会导致其他页面404;

try_files $uri $uri/ /index.php?$query_string;
if (!-e $request_filename){  
    rewrite ^/(.*) /index.php last;  
} 

例如:

location ~ .*\.php(\/.*)*$ {
    try_files $uri $uri/ /index.php?$query_string;
    if (!-e $request_filename){
        rewrite ^/(.*) /index.php last;
    }
   ...
}

3、在项目代码中header.php中加入如下代码,默认访问https(可选);

if ($_SERVER["HTTPS"] <> "on")
{
    $xredir = "https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    header("Location: ".$xredir);
}
标签:Nginx 发布于:2019-11-11 17:28:52