There are questions remain, We'll search for the answers together. But one thing we known for sure,the future is not set!

【原创文章】WDlinux WDCP实现全站http跳转到https的方法

系统防护 百蔬君 7385℃ 已收录 0评论

Google对ssl加密的重视带动了https加密协议的利用,在面向国外站长中引起了风潮,我也想给baishujun.com加一个https加密访问,但是由于百度对这类网站非常不友好,这个我自己没有测试哈,是看到网站的资料这么说,所以我也就没有去尝试了。最近因为有客户需要在google推广,gogle硬性规定推广网站的用户登录页面和付款页面必须是https加密协议,需求这种环境,在架设的过程中顺便就记录了这个过程,记录在了《开启WDINLUX WDCP 双引擎系统网站的HTTPS访问支持》这篇文章中。架设好了https,那么我们就需要将以前的http的访问全部转向到https,这里我们可以只是启动部分是https访问,比如用户登录和付款页面,这个修改只需要修改网站中相关部分的代码就好了,href中带上https的地址就行。

但是既然google对https友好,那么我们也可以开启全站https访问,那么我们就需要对整个网站的http多301重定向到https。我们知道http访问默认端口是80端口,而ssl加密协议默认使用的是443端口。这种跳转在网站的伪静态中设置可能没有效果,比如.htaccess我设置没有成功。那么我们可以从“根”来入手,我们来“治本”。

在WDCP系统中,每一个网站都有自己的配置文件,那么我们在配置文件中启动ssl监听就好了,WDCP的网站配置文件目录为/www/wdlinux/nginx/conf/vhost/。以百蔬君网站为例,我们使用的是nginx+apache+mysql的环境系统,前台程序都是nginx来处理,处理静态文件是nginx的优势。那么开启https的功能就需要在nginx的配置中去修改,百蔬君的配置文件为/www/wdlinux/nginx/conf/vhost/,在上文中已经介绍了怎么配置这个网址来支持https协议。代码如下

server {
listen 80;
listen 443 ssl;
ssl_certificate /www/ssl/2_www.baishujun.com.key;
ssl_certificate_key /www/ssl/1_www.baishujun.com_bundle.crt;
ssl_session_timeout 5m;
server_name baishujun.com www.baishujun.com;
root /www/web/baishujun_com/public_html;
index index.html index.php;
error_page 400 /errpage/400.html;
error_page 403 /errpage/403.html;
error_page 404 /errpage/404.html;
error_page 405 /errpage/405.html;
location ~ \.php$ {
proxy_pass http://127.0.0.1:88;
include naproxy.conf;
}
location / {
try_files $uri @apache;
}
location @apache {
proxy_pass http://127.0.0.1:88;
include naproxy.conf;
}
}

添加了listen 443 ssl这四行。那么全站跳转需要怎么做呢,我想到的办法是把监听80端口和443端口的配置分开。

server {
listen 80;
server_name www.www.baishujun.com www.baishujun.com;
root /www/web/www.baishujun_com/public_html;
index index.html index.php;
error_page 400 /errpage/400.html;
error_page 403 /errpage/403.html;
error_page 404 /errpage/404.html;
error_page 405 /errpage/405.html;
location ~ \.php$ {
proxy_pass http://127.0.0.1:88;
include naproxy.conf;
}
location / {
try_files $uri @apache;
}
location @apache {
proxy_pass http://127.0.0.1:88;
include naproxy.conf;
}
}
server {
listen 443 ssl;
ssl_certificate /www/wdlinux/nginx/conf/ssl/AddTrustExternalCARoot.crt;
ssl_certificate_key /www/wdlinux/nginx/conf/ssl/www.www.baishujun.com.nopass.key;
ssl_session_timeout 5m;

server_name www.baishujun.com baishujun.com;
root /www/web/www_baishujun_com/public_html;
index index.html index.php;
error_page 400 /errpage/400.html;
error_page 403 /errpage/403.html;
error_page 404 /errpage/404.html;
error_page 405 /errpage/405.html;
location ~ \.php$ {
proxy_pass http://127.0.0.1:88;
include naproxy.conf;
}
location / {
try_files $uri @apache;
}
location @apache {
proxy_pass http://127.0.0.1:88;
include naproxy.conf;
}
}

这样就完成了443端口和80端口分开的配置了。

之后在80端口的配置中加一个301跳转,
return 301 https://www.baishujun.com$request_uri;
或者
rewrite ^(.*)$ https://$host$1 permanent;

这样就将所有绑定的域名重定向到 https://www.baishujun.com去了,不管80端口绑定了什么,全部重定向走了。在这里提一个问题,如何将https://baishujun.com重定向到https://www.baishujun.com呢?在这里卖一个小小的关子了,欢迎讨论。

如果觉得有用,帮我点赞哦。

将所有子域名强制跳转到www的方法,这个关子卖的,差点自己都忘掉了,鄙视下自己。

方法有两个:
第一个方法是修改nginx的网站配置文件,比如百蔬君博客的配置文件/www/wdlinux/nginx/conf/vhost/baishujun.com.conf
listen 443 ssl;下面添加一个判断语句就可以了。

listen 443 ssl;
server_name baishujun.com  www.baishujun.com;
if ($host != 'www.baishujun.com') {rewrite ^/(.*)$ https://www.baishujun.com/$1 permanent;} 

第二个方法是修改伪静态文件,就是网站根目录的.htaccess文件

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.baishujun\.com
RewriteRule (.*) https://www.baishujun.com/$1 [R=301,L]

如果网站域名不是www.baishujun.com就跳转到https://www.baishujun.com,个人推荐第二种方法,在网站目录,更容易维护。网站配置文件,一般情况还是不动为好!

Updated:20170111

WDCP种http to https跳转的问题,及一个问题的修正

像上面的方法,http到https的跳转是没有问题的,但是就像我在 《开启WDINLUX WDCP 双引擎系统网站的HTTPS访问支持》文章说的一样,这样的修改之后,只要在wdcp后台对这个网站进行一次配置,那么这个所有手工修改的信息就没有了,所以我们把server{listen 443 ssl;.........}这个语句块移动到nginx的配置文件中,不放在虚拟主机的配置文件中了。这样只是实现了https的搭建。我尝试将这个网站在80端口的监听也搬到nginx.inf的配置文件中去,没有成功,很显然对于同一个端口的监听是先按照虚拟主机的配置文件来执行,然后再按照nginx.inf的配置文件去去执行。在baishujun.com.conf端口已经执行80端口的配置了,在nginx.inf就没有执行,但是执行搬到了这里的对443端口的监听。
那么强制将http跳转到https的方法有2个方向。
第一个方向,在wdcp每次配置好网站之后,然后手动修改虚拟主机配置文件,比如在baishujun.com.conf,在这个文件里面按照上面介绍的方法,添加一条跳转或者是重写,将所有的80端口的访问转发到https去处理。
第二个方向,就是在网站程序里面进行检查,用php程序或者js程序来判断是否启用了SSL。
php判断语句

function is_HTTPS(){  
    if(!isset($_SERVER['HTTPS']))  return FALSE;  
    if($_SERVER['HTTPS'] === 1){  //Apache  
        return TRUE;  
    }elseif($_SERVER['HTTPS'] === 'on'){ //IIS  
        return TRUE;  
    }elseif($_SERVER['SERVER_PORT'] == 443){ //其他  
        return TRUE;  
    }  
    return FALSE;  
}  

js判断语句

var ishttps = 'https:' == document.location.protocol ? true: false;  
if(ishttps){  
alert('https');  
}else{  
alert('http');  
} 

WDCP后台的301跳转要注意,在设置跳转到https方面来说,不建议使用。作者还要加油把这个更新好啊。

设置之后虚拟主机配置文件是这样,

就算把他的这个 rewrite ^/(.*)$ https://$host$1 permanent;语句修改正确,也是没法访问的。

慎用!

Updated:20170112
2015年双12广东种业博览会全国优良蔬菜品种田间表现-广东省农业科学院蔬菜研究所汇丰二号辣椒-009.jpg

 

 

转载请注明:百蔬君 » 【原创文章】WDlinux WDCP实现全站http跳转到https的方法

喜欢 (6)or分享 (0)
发表我的评论
取消评论

请证明您不是机器人(^v^):

表情