記一次全站升級https引發的一系列問題


中秋假期,閑來無事。花了一下午折騰了下https,說實話這年頭還有網站不上https顯然是折騰精神不夠啊~

1、SSL證書評估

看了市面上各種類型的證書,有收費的也有免費的,但是最終還是選擇了騰訊雲提供的TrustAsia一年免費期的證書,沒有次數限制,可以過期后再次申請。最主要的原因還是我懶,哈哈~~

2、SSL證書安裝

從騰訊雲將申請的證書下載到本地,解壓獲得3個文件夾,分別是Apache、IIS、Nginx 服務器的證書文件,可以根據自己的實際情況,安裝在三種服務器上。

我這里使用Nginx做前端轉發。更多的介紹可以查看文檔:https://cloud.tencent.com/document/product/400/4143

2.1 獲取證書

Nginx文件夾內獲得SSL證書文件 1_www.domain.com_bundle.crt 和私鑰文件 2_www.domain.com.key,
1_www.domain.com_bundle.crt 文件包括兩段證書代碼 “-----BEGIN CERTIFICATE-----”和“-----END CERTIFICATE-----”,
2_www.domain.com.key 文件包括一段私鑰代碼“-----BEGIN RSA PRIVATE KEY-----”和“-----END RSA PRIVATE KEY-----”。

2.2 證書安裝

將域名 www.domain.com 的證書文件1_www.domain.com_bundle.crt 、私鑰文件2_www.domain.com.key保存到同一個目錄,例如/usr/local/nginx/conf目錄下。
更新Nginx根目錄下 conf/nginx.conf 文件如下:

server {
        listen 443;
        server_name www.domain.com; #填寫綁定證書的域名
        ssl on;
        ssl_certificate 1_www.domain.com_bundle.crt;
        ssl_certificate_key 2_www.domain.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 / {
            root   html; #站點目錄
            index  index.html index.htm;
        }
    }

配置完成后,先用bin/nginx –t來測試下配置是否有誤,正確無誤的話,重啟nginx。就可以使用 https://www.domain.com 來訪問了。

注:

配置文件參數 說明
listen 443 SSL訪問端口號為443
ssl on 啟用SSL功能
ssl_certificate 證書文件
ssl_certificate_key 私鑰文件
ssl_protocols 使用的協議
ssl_ciphers 配置加密套件,寫法遵循openssl標准

2.3 使用全站加密,http自動跳轉https(可選)

對於用戶不知道網站可以進行https訪問的情況下,讓服務器自動把http的請求重定向到https。
在服務器這邊的話配置的話,可以在頁面里加js腳本,也可以在后端程序里寫重定向,當然也可以在web服務器來實現跳轉。Nginx是支持rewrite的(只要在編譯的時候沒有去掉pcre)
在http的server里增加rewrite ^(.*) https://$host$1 permanent;
這樣就可以實現80進來的請求,重定向為https了。

 3、證書安裝的一系列問題。

3.1 端口號問題

需要注意的是,我們通常使用的80端口作為http默認訪問的端口,而在https中使用的443端口,這個是需要特別注意的,我在安裝過程中一開始就沒有注意端口號的問題,導致https一直沒有生效,后來問了公司的運維同學才知道,耽誤了很長的時間。

3.2 http跳轉https

一般我們的網站支持https之后,就不在支持http的訪問了。這時候就需要把http的訪問請求,重定向到https。實現的過程也很簡單,新增一個server,然后加入以下配置。

server {
        listen       80;

        location / {
            rewrite (.*) https://www.laoyeye.net;
        }
    }

直接將80端口的訪問,全部轉發到網站的https域名,其實我這個寫的還不是很規范,一般不會將域名寫死,而是根據請求的域名做重定向的。

3.3 一級域名跳轉二級域名的問題

一般我們的網站很多都想讓www作為網站的首頁,這是一個二級域名,而我們默認的一級域名,如laoyeye.net是不提供服務的,這個時候就需要將https訪問這個域名的請求重定向到www域名,實現的過程也很簡單。

server {
        listen       443;
        server_name laoyeye.net; #填寫綁定證書的域名
        ssl on;
        ssl_certificate 1_www.laoyeye.net_bundle.crt;
        ssl_certificate_key 2_www.laoyeye.net.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 / {
            rewrite (.*) http://www.laoyeye.net;
        }
    }
需要注意的是這里處理的是https://laoyeye.net鏈接,而http://laoyeye.net在上面端口80的監聽中是已經做了處理的。

3.4 400 Bad Request | The plain HTTP request was sent to HTTPS port

完成上述的配置,基本上訪問http://www.laoyeye.net/,還是http://laoyeye.net/,亦或是https://laoyeye.net/,均會跳轉到https://www.laoyeye.net/。但是我發現個別的鏈接訪問會出現400錯誤,搞了一下午才弄清問題所在。

原因主要是我在后端邏輯處理的時候使用了重定向,后端重定向后返回的是http鏈接,就會出現這個問題。

解決辦法也很多,很多人會改tomcat的配置,讓重定向后的鏈接直接是https形式的。其實我不怎么喜歡這種方式,因為這樣做的侵入性太大。當換一個tomcat服務器后,可能你並不會想起這個配置。

既然我們是在nginx上配置的ssl,這里我們還是從nginx上想辦法。

location種加入如下配置

    proxy_redirect   http:// https://;

這句話的作用是對發送給客戶端的URL進行修改, 將http協議強制轉為https,完成這個配置后就不會出現400的問題了。

當然我還有一些個人的配置問題,比如QQ互聯的回調地址啦,也是需要修改http為https才能正常使用。

附Nginx入門以及我的配置參考:

一、Nginx安裝手冊

三、nginx實現反向代理負載均衡

我的nginx配置供參考:

user  root;
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;

upstream tomcat_server {
            server xxxx:xxxx;
        }

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {

        listen 80;

        location / {

                rewrite (.*) https://www.laoyeye.net;

        }

    }

    server {
        listen       443;
        server_name  laoyeye.net;
   
        ssl on;
        ssl_certificate 1_www.laoyeye.net_bundle.crt;
        ssl_certificate_key 2_www.laoyeye.net.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;

    #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            rewrite (.*) https://www.laoyeye.net;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    server { 
        listen 443;
        server_name www.laoyeye.net;
        ssl on;
        ssl_certificate 1_www.laoyeye.net_bundle.crt;
        ssl_certificate_key 2_www.laoyeye.net.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 / {

         #域名www.laoyeye.net的請求全部轉發到tomcat_server即tomcat服務上
         proxy_pass http://tomcat_server;
         proxy_set_header Host $host:$server_port;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Real-PORT $remote_port;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                
         # 必須配置:
         proxy_set_header X-Forwarded-Proto  $scheme;

         # 作用是對發送給客戶端的URL進行修改, 將http協議強制轉為https
         proxy_redirect   http:// https://; 
        index index.jsp index.html index.htm;

        }

    }
                 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM