把網站從 http 轉換成 https

基礎準備: 一臺服務器,一個主域名或多級域名,本次申請的免費html

本次環境使用 centos6.5 + nginx1.8 + jdk1.8 + tomcat8nginx

若是須要收費的請參考:web

雲盾證書服務(包年)-購買-阿里雲apache

SSL證書 - 騰訊雲後端

 

一. 申請免費 HTTPS 證書

1. 輸入網址 https://freessl.org/
2. 輸入你的準備好的域名, 選擇亞洲誠信, 點擊建立

3. 輸入你的郵箱, 選擇證書品牌, 證書類型 (RSA), 驗證類型 (DNS),CSR 生成 (後端服務器生成), 點擊建立

4. 在網頁下出現以下圖所示效果

6. 在你的域名管理平臺下, 填寫如上所述信息, 點擊保存,而後點擊 DNS 驗證, 驗證經過後下載證書

7. 下載證書文件, 並改變文件名稱

full_chain.pem --> 1_blog.zousiliang.com_bundle.crt

private.key   -->  2_blog.zousiliang.com.key

二. 服務器環境配置

下載 jdk1.8 和 tomcat8 配置流程同樣centos

1.jdk 安裝教程

jdk環境配置 - 益達MIM味tomcat

2.tomcat 安裝教程

tomcat環境 - 益達MIM味服務器

3.nginx 安裝教程

Nginx環境搭建 - 益達MIM味session

4. 配置上面環境後進行 https 配置
上傳證書文件到 nginx/conf/ 即在 /usr/local/nginx/conf/
nginx 的 nginx.conf 文件配置以下
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    upstream tomcats{
         server localhost:8080;
    }


    server{
      listen    80;
      server_name  blog.zousiliang.com;
      return 301   https://$server_name$request_uri;
    }

    server {
        listen 443 ssl;
        server_name blog.zousiliang.com;
        ssl on;
        ssl_certificate 1_blog.zousiliang.com_bundle.crt;
        ssl_certificate_key 2_blog.zousiliang.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        location / {
            proxy_pass   http://tomcats$request_uri;
            proxy_set_header  Host $host:$server_port;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-Proto https; 
            client_max_body_size  10m;
        }
    }

}

tomcat service.xml 文件中配置以下app

<Connector port="8080" protocol="HTTP/1.1"
                connectionTimeout="20000"
                redirectPort="443"
                proxyPort="443"
                URIEncoding="UTF-8" />




<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

    <Valve className="org.apache.catalina.valves.RemoteIpValve"
            remoteIpHeader="x-forwarded-for"
            remoteIpProxiesHeader="x-forwarded-by"
            protocolHeader="x-forwarded-proto" directory="logs"
            prefix="localhost_access_log." suffix=".txt"
            pattern="%h %l %u %t &quot;%r&quot; %s %b"
          />

</Host>

三. 驗證配置

訪問 http://blog.zousiliang.com 或者 https://blog.zousiliang.com 出現以下效果表示配置成功

 

轉自:https://hacpai.com/article/1525254970376

相關文章
相關標籤/搜索