記一次全站升級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 / { roothtml; #站點目錄 indexindex.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 { listen80; location / { rewrite (.*) https://www.laoyeye.net; } }
直接將80埠的訪問,全部轉發到網站的https域名,其實我這個寫的還不是很規範,一般不會將域名寫死,而是根據請求的域名做重定向的。
3.3 一級域名跳轉二級域名的問題
一般我們的網站很多都想讓www作為網站的首頁,這是一個二級域名,而我們預設的一級域名,如laoyeye.net是不提供服務的,這個時候就需要將https訪問這個域名的請求重定向到www域名,實現的過程也很簡單。
server { listen443; 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_redirecthttp:// https://;
這句話的作用是對傳送給客戶端的URL進行修改, 將http協議強制轉為https,完成這個配置後就不會出現400的問題了。
當然我還有一些個人的配置問題,比如QQ互聯的回撥地址啦,也是需要修改http為https才能正常使用。
最後附個人nginx的配置供參考:
userroot; worker_processes1; #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; events { worker_connections1024; } http { includemime.types; default_typeapplication/octet-stream; upstream tomcat_server { server xxxx:xxxx; } #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' #'$status $body_bytes_sent "$http_referer" ' #'"$http_user_agent" "$http_x_forwarded_for"'; #access_loglogs/access.logmain; sendfileon; #tcp_nopushon; #keepalive_timeout0; keepalive_timeout65; #gzipon; server { listen 80; location / { rewrite (.*) https://www.laoyeye.net; } } server { listen443; server_namelaoyeye.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_loglogs/host.access.logmain; location / { rewrite (.*) https://www.laoyeye.net; } #error_page404/404.html; # redirect server error pages to the static page /50x.html # error_page500 502 503 504/50x.html; location = /50x.html { roothtml; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { #proxy_passhttp://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { #roothtml; #fastcgi_pass127.0.0.1:9000; #fastcgi_indexindex.php; #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name; #includefastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { #denyall; #} } 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_redirecthttp:// https://; index index.jsp index.html index.htm; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { #listen8000; #listensomename:8080; #server_namesomenamealiasanother.alias; #location / { #roothtml; #indexindex.html index.htm; #} #} # HTTPS server # #server { #listen443 ssl; #server_namelocalhost; #ssl_certificatecert.pem; #ssl_certificate_keycert.key; #ssl_session_cacheshared:SSL:1m; #ssl_session_timeout5m; #ssl_ciphersHIGH:!aNULL:!MD5; #ssl_prefer_server_cipherson; #location / { #roothtml; #indexindex.html index.htm; #} #} }