将博客修改成后台用https访问,前台用http访问
域名的备案终于在申请后的3个月后申请成功了,终于不用需要先从国外转发回来了。
既然域名搞好了,不好好的设置一下服务器岂不是很浪费?╮(╯▽╰)╭
首先想到的就是启用ssl来保护后台。
wordpress原本已经有设置将登录和后台改变为ssl方式。这样的话,在https方式也能访问博客,但是由于现在启用了多说评论管理系统和用到了cdn,这两个都没有https的地址。在以https方式进入博客时浏览器就会拒绝http的连接,这样就使得多说和某些js不能够使用,从而影响体验。
我想到的解决方法就是博客的前台完全由http来访问,后台完全用https方式访问。当从http方式登录后台时,自动跳转到https地址来访问博客后台;当用户从https地址访问内容时,就将转跳会http地址访问。
我使用的是 Nginx,所以下面的设置都是关于nginx的
当在http下面检测到当前访问后台时,自动转跳到https地址
location ~ ^/(wp-admin)/* {
rewrite ^(.*) https://blog.shiniv.com$1 permanent;
}
当nginx监听到用户访问的是https端口时,如果当前地址不是后台地址,则转跳回http地址。
因为在nginx中,不能使用if else 来进行判断,所以必须要设置一个变量来判断当前是否为后台
set $var_admin 0;
if ($request_uri ~* "^/wp-admin/*") {
set $var_admin 1;
}
if ($var_admin !~ 1) {
rewrite ^(.*) http://blog.shiniv.com$1 permanent;
}
如果博客需要伪静态的话,需要在监听http的配置上加上一下代码
#重写URL
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
修改完成后,保存退出。
测试一下我们上面所改的代码是否正确
nginx -t
如果显示以下文字,则说明一切正常。
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
重新加载nginx配置文件
nginx -s reload
现在访问一下后台, 看看是不是已经变成了https地址了呢?
在测试一下在https地址下访问博文,看看能不能回到http地址。
一切正常的话,就会按照上面所说的效果一样了~~~~~
最后附上我的配置~~
server{
listen 80;
server_name blog.shiniv.com;
index index.html index.htm index.php;
root /htdocs/blog.shiniv.com;
access_log /htdocs/log/blog.shiniv.com.log;
error_log /htdocs/log/blog.shiniv.com_error.log;
location / {
location ~ ^/(wp-admin)/* {
rewrite ^(.*) https://blog.shiniv.com$1 permanent;
}
location /upload/ {
location ~ .*.(php)?$ {
deny all;
}
}
#重写URL
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~* .(?:js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
access_log off;
log_not_found off;
tcp_nodelay off;
break;
}
include location.conf;
}
server{
listen 443;
server_name blog.shiniv.com;
index index.html index.htm index.php;
root /htdocs/shiniv.com/blog.shiniv.com;
access_log /htdocs/log/blog.shiniv.com.log;
error_log /htdocs/log/blog.shiniv.com_error.log;
ssl on;
ssl_certificate /htdocs/certificate/shiniv.com.crt;
ssl_certificate_key /htdocs/certificate/shiniv.com.key;
ssl_session_timeout 5m;
#add_header Front-End-Https on;
location / {
location /upload/ {
location ~ .*.(php)?$ {
deny all;
}
}
set $var_admin 0;
if ($request_uri ~* "^/wp-admin/*") {
set $var_admin 1;
}
if ($var_admin !~ 1) {
rewrite ^(.*) http://blog.shiniv.com$1 permanent;
}
}
include location.conf;
}
【声明】本文 将博客修改成后台用https访问,前台用http访问 为柠之漠然原创文章,转载请注明出自
枫之落叶
并保留本文有效链接:https://blog.shiniv.com/2013/10/admin-use-https-and-user-use-http/ , 转载请保留本声明!
网站不错,雁过留痕,欢迎互访!