ExpressJS 安装SSL证书,ExpressJS 环境配置https证书教程

ExpressJS 安装SSL证书,ExpressJS 环境配置https证书教程

ExpressJS 安装ssl需要对编码有点基础,购买证书后,下载nginx格式的证书 (解压后包含2个文件,  key 私钥, cert 域名证书+根证书)
 //Test : curl -k https://localhost/

var express = require('express');
var https = require('https');
var http = require('http'); 

const fs = require('fs');

const options = {
  key: fs.readFileSync('path/yourdomain.com.key'),
  cert: fs.readFileSync('path/yourdomain.com.crt')
}; 

var app = express();


http.createServer(app).listen(80);
https.createServer(options, app).listen(443); 

Nginx 自动跳转到HTTPS Nginx设置http自动跳转https SSL配置

Nginx 自动跳转到HTTPS Nginx设置http自动跳转https SSL配置

方法一

rewrite ^(.*)$ https://$host$1 permanent;

方法二

适用于 80端口和443 配置在同一个 server{}内

if ($server_port = 80) {
      rewrite ^(.*)$ https://$host$1 permanent;
}

其他情况, 站点绑定很多域名,只让某个域名跳转:

set $redirect_https 1;
if ($server_port = 80) {
   set $redirect_https "${redirect_https}2";
}
if ($http_host = 'www.yourdomain.com') {
   set $redirect_https "${redirect_https}3";
}
if ($http_host = 'yourdomain.com') {
   set $redirect_https "${redirect_https}3";
}

if ($redirect_https = "123") {
rewrite ^(.*)$ https://$host$1 permanent;
}

示例

server {
        listen       80;
        server_name  www.getssl.cc;
        rewrite ^ https://$http_host$request_uri? permanent;
}

server {
        listen 443;
        ssl on;
        ssl_certificate /etc/ssl/cacert.pem;
        ssl_certificate_key /etc/ssl/privkey.pem;
        server_name www.yourdomain.com;  

        
        server_tokens off;

        location / {
          
                fastcgi_param   HTTPS               on;
                fastcgi_param   HTTP_SCHEME         https;

        }
        
}

Nginx安装SSL证书 Nginx配置https证书

Nginx安装SSL证书 Nginx配置https证书
首先拿到Nginx服务器证书
yourdomain.com.crt     (服务器证书)
yourdomain.com.key   (私钥文件)如果为空请将生成CSR时保存的私钥内容粘贴在文件中

环境检测,检测命令如下(测试nginx是否支持SSL)

nginx -V

如果有显示 –with-http_ssl_module 表示已编译openssl,支持安装ssl;

如果没有安装请下载nginx源码重新编译;

./configure --with-http_stub_status_module --with-http_ssl_module
make && make install

配置Nginx

server {
 listen 80;
 listen 443 ssl;
 server_name www.yourdomain.com;

 ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
 ssl_certificate /etc/ssl/yourdomain.com.crt;
 ssl_certificate_key /etc/ssl/yourdomain.com.key;
 ssl_prefer_server_ciphers on;
 
 #自动跳转到HTTPS (可选)
 if ($server_port = 80) {
 rewrite ^(.*)$ https://$host$1 permanent;
 }
 
 location / {
 root /home/yourdomain/;
 index index.php;
 }
}

请将域名修改为您自己的

以上配置仅供参考,其他参数请根据生产环境需要添加。安装后重启nginx使其生效

centos6
service nginx restart
centos7
systemctl restart nginx

Apache自动跳转到 HTTPS Apache设置http跳转https

Apache自动跳转到 HTTPS Apache设置http跳转https

网站根目录新建 .htaccess

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

站点绑定多个域名,只允许www.example.com 跳转

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

******把网址更改为自己的******

高级用法 (可选)

RewriteEngine on

# 强制HTTPS
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{SERVER_PORT} 80
# 某些页面强制
RewriteCond %{REQUEST_URI} ^something_secure [OR]
RewriteCond %{REQUEST_URI} ^something_else_secure
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# 强制HTTP
RewriteCond %{HTTPS} =on [OR]
RewriteCond %{SERVER_PORT} 443
# 某些页面强制
RewriteCond %{REQUEST_URI} ^something_public [OR]
RewriteCond %{REQUEST_URI} ^something_else_public
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Apache + mod_ssl.so APACHE安装SSL证书 https配置安装教程

Apache + mod_ssl.so APACHE安装SSL证书 https配置安装教程
1.需要有Apache服务器环境的SSL证书

yourdomain.com.crt     (服务器证书)
yourdomain.com.key   (私钥文件)如果为空请将生成CSR时保存的私钥内容粘贴在文件中
yourdomain.com.ca-bundle  (根证书链)

首要条件就是 apache 已经安装了 mod_ssl.so 模块 。
检测方法使用以下命令:

httpd -M | grep mod_ssl

如果有显示 mod_ssl.so 表示已经安装了apache模块。
CentOS/Redhat安装mod_ssl.so

yum install mod_ssl

Debian/Ubuntu

sudo a2enmod ssl
sudo service apache2 restart

Apache SSL配置

Listen 443  (如果配置已经存在就不要加)
LoadModule ssl_module modules/mod_ssl.so (如果配置已经存在就不要加)
NameVirtualHost *:443 (非必须,配置多个SSL站点会需要)

<VirtualHost *:443>
ServerName www.getssl.cn
ServerAlias getssl.cn
DocumentRoot /var/www/html
SSLEngine on
SSLProtocol all -SSLv2 -SSLv3
SSLCertificateFile /etc/ssl/yourdomain.com.crt 
SSLCertificateKeyFile /etc/ssl/yourdomain.com.key 
SSLCertificateChainFile /etc/ssl/yourdomain.com.ca-bundle
</VirtualHost>

以上只是SSL配置范例,尽量不要直接应用于生产环境,请根据80端口的站点配置修改。
证书可以放在其他位置,没有强制要求
ServerName 和 ServerAlias 需要修改

配置完成重启Apache使其生效

service httpd restart
service httpd restart

IIS7 IIS8 设置http自动跳转到HTTPS

IIS7 IIS8 设置http自动跳转到HTTPS

IIS7需要先确认是否安装 “URL 重写” 或者 “URL Rewrite” 模块 , 如果您已经安装可以跳过

“URL重写” 模块下载地址

微软下载地址(64位):http://www.microsoft.com/zh-cn/download/details.aspx?id=7435
微软下载地址(32位):http://www.microsoft.com/zh-cn/download/details.aspx?id=5747

1. 选择站点,  “URL 重写”,如果安装的是英文版的 应该是【Url Rewrite】

2.添加 “ 空白规则”

3.添加规则
名称 : HTTPS
匹配URL 模式: (.*)
添加条件:    条件: {HTTPS}  模式: off

操作类型选择:重定向
重定向URL: https://{HTTP_HOST}/{R:1}

设置301跳转

之后点击保存

当然也可以自己直接编辑网站配置文件web.config,相关代码参考

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                  <match url="(.*)" />
                        <conditions>
                          <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                        </conditions>
                  <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
                </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

windows服务器IIS6 设置301跳转到HTTPS

windows服务器IIS6 设置301跳转到HTTPS
1. 新建一个空站点,网站的主机头填写 要跳转的域名. 端口使用80

描述随便写

2.网站根目录随便新建一个空目录指定  d:/wwwroot/301

3.站点新建完成,右键 属性设置。
重定向到:   https://www.domain.com$S$Q           domain.com修改成要跳转的域名

4.绑定证书的站点删掉 www.domain.com   80端口的域名绑定

windows服务器IIS6 自动跳转到HTTPS配置教程

windows服务器IIS6 自动跳转到HTTPS配置教程

1. IIS6中,站点属性-》目录安全性-》编辑中把“要求安全通道(SSL)”勾选上即可。

2. 打开自己网站根目录, 例如 d:webroot,  在根目录新建一个名为  https.htm 的文件,内容如下:

<html>
 <head><title>Redirecting...</title></head>
 <script language="JavaScript">
 function redirectHttpToHttps()
 {
     var  httpURL= window.location.hostname + window.location.pathname +  window.location.search;
     var httpsURL=  "https://" + httpURL;
     window.location  = httpsURL;
 }
 redirectHttpToHttps();
 </script>
 <body>
 </body>
 </html>


3.IIS6中, 站点属性 -》 自定义错误 -》选择 403.4 -》修改文件路径为  d:/webroot/https.htm

阿里云申请HTTPS证书 申请免费ssl证书 阿里云申请免费ssl证书

这里简单的说一下免费SSL证书的申请,阿里云有提供免费HTTPS的证书。

1.登录阿里云平台
aliyun.com
进入控制台,搜索SSL

也可以在阿里云首页点击该链接直接进入

2.在证书SSL购买频道直接购买

点击购买按钮

3.在购买页面,选择【免费个人版】

之后按照流程申请即可