NGINX、Yii2,怎样从域名跳转到登录页

在NGINX中配置:


location / { 
    if (!-e $request_filename){
        rewrite ^/(.*) /index.php last;
}

这样,当NGINX找不到文件的时候,就会将URL重写为index.php。对于URL中只有域名的情况,因为不存在对应的文件,所以会匹配到。

再配置:

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
}

这样,以.php结尾的URL会被提交到php-fpm的入口,即,index.php文件。这个提交是以参数形式提交的,这里不多说,参见:https://www.cnblogs.com/liuwei-a/p/10197876.html。

至此,就进入了Yii的控制范围。

在Yii的配置文件(web.php)中配置默认路由:

'defaultRoute' => 'xxx/xxxx/xxxxx'

这样,就进入了Yii的代码范围,module xxx,controller xxxx,action xxxxx。

在代码中检查是否登录过,例如,通过 session 检查,如果没有登录过,则跳转到登录页面。

也有其它配置方式,参见:https://blog.csdn.net/eddy23513/article/details/79311179

至此,逻辑完成。

猜你喜欢

转载自www.cnblogs.com/liuwei-a/p/10348088.html