首先去雲服務器申請證書
這里購買免費的ssl證書,申請完成之后,直接在服務平台下載即可,放在如下的目錄,這里參考阿里雲的推薦配置,在nginx安裝目錄下的conf目錄創建cert文件夾,將解壓的證書放在cert文件夾下,因為我的nginx不僅僅配置了一個應用,都配置在conf的 vhost
目錄下
一定要注意位置,不然啟動nginx會出現文件沒有找到的錯誤
對wordpress文章內的圖片及后台強制https設置。
wordpress上傳的圖片都寫入了絕對地址,也就是啟用https前所有上傳到文章的圖片都是以http://
開頭的地址存儲,如果不轉換成https訪問網站時瀏覽器頂部的“小鎖”會在某些瀏覽器顯示安全警告,讓圖片變成https地址很簡單,只需要在當前主題的functions.php
文件中添加以下代碼:
/* 替換圖片鏈接為 https */
function my_content_manipulator($content){
if( is_ssl() ){
$content = str_replace('http://www.imisty.cn/wp-content/uploads', 'https://www.imisty.cn/wp-content/uploads', $content);
}
return $content;
}
add_filter('the_content', 'my_content_manipulator');
可自動將全站所有http地址圖片自動轉換成https地址圖片。
開啟登錄和后台https訪問。
- 打開網站根目錄下的
wp-config.php
文件,在文件底部添加:
/* 強制后台和登錄使用 SSL */
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);
-
然后在后台管理界面
設置-常規-wordpress地址和站點地址
修改為https -
要確保網站的每個元素(如插件、JS、CSS文件、圖片、內容分發網站等)都采用https協議:(可以在chromeF12看看有沒有警報)
-
修改“菜單”當中的所有“自定義鏈接”為 https;
-
修改其他自己手賤寫入的絕對鏈接的地址……
-
替換文章所有的Http到https
直接修改數據庫(注意先備份!注意先備份!!注意先備份!!!重要的事情說三遍!)
UPDATE wp_posts
SET post_content = ( Replace (post_content, 'http://www.imisty.cn/', 'https://www.imisty.cn/') );
但是實際操作的過程中我發現不需要使用這一步,畢竟有點危險,所以我將后台站點https設置完成之后,發現文章訪問404,再修改為朴素鏈接之后直接順帶修改了數據庫還是使用WordPress自帶的修改的對我這樣不懂PHP的小白還是很不錯的
開始配置站點的配置文件 ,我的配置如下
網站這時候雖然支持了 HTTPS 訪問,但是也可以使用 HTTP 來訪問,考慮到搜索引擎目前收錄的都是 HTTP 鏈接,需要將http重定向到https。
server {
listen 80;
server_name www.imisty.cn imisty.cn;
return 301 https://www.imisty.cn$request_uri;
root /var/www/html/wordpress;
index index.html index.htm index.php;
client_max_body_size 100M;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php$1 last;
}
location ~ .*\.php(\/.*)*$ {
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
error_log logs/error_wordpress.log;
access_log logs/misty.log combined;
}
server {
listen 443 ssl;
server_name www.imisty.cn imisty.cn;
root /var/www/html/wordpress;
index index.html index.htm index.php;
client_max_body_size 100M;
ssl_certificate cert/misty.pem;
ssl_certificate_key cert/misty.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location ~ .*\.php(\/.*)*$ {
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
}
檢驗配置是否正確
/usr/local/nginx/sbin/nginx -t
Nginx安裝http_ssl_module模塊
Nginx如果未開啟SSL模塊,配置Https時提示錯誤
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:xxx
因為在配置的時候已經安裝過nginx,但是沒有安裝ssl模塊
采用如下步驟安裝nginx的ssl模塊
-
進入nginx的ssl源碼包
cd ~/MyFille/nginx
-
用
configure
安裝指定的模塊
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
-
make編譯
make
4.一定不要執行make install,否則就覆蓋安裝了。 -
備份原有的nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak
6.將剛剛編譯好的nginx替換原來的,一定要先stop,不然會顯示文件忙
cp ./objs/nginx /usr/local/nginx/sbin/
7.查看安裝情況,如下信息說明正常安裝
[root@izwz993bv9azta8nrfwuhqz sbin]# ./nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
常見問題
啟動nginx的時候出現如下錯誤
解決Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid" failed(2:No such file or directory)
解決辦法
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
使用nginx -c
的參數指定nginx.conf文件的位置,注意這里的配置文件位置最好使用絕對路徑
阿里雲推薦nginx配置的警告信息
[warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
nginx 1.15 及以后的版本,不需要再寫 ssl on; 了。
改為
應該把 ssl on 改成 listen 443 ssl 這樣才對
主頁和后台管理https訪問正常,其他頁面訪問出現404錯誤
這個查閱資料發現時偽靜態的問題,最后還是使用朴素鏈接解決這個問題,原因我也不想深究了
WP Editor.md 在修改為https訪問之后卡加載界面
這個問題我在前端調試發現發現是js加載的時候使用了https需要將地址改為https的,最后我還是選擇逃避式解決問題改用WP Githuber MD,另一個原因就是WP Editor.md作者不再維護了
小確幸
每一絲靈感都值得被記錄,每一筆記錄都是成長,每一點成長都值得歡呼
博主個人站: www.imisty.cn
CSDN博客: https://blog.csdn.net/lookinthefog
博客園 :https://imist.cnblogs.com/
希望能夠認識一些熱愛技術的小伙伴,歡迎友鏈接喲