最近對自己的博客網(wǎng)站進行HTTPS化,打造一個安全的博客,博客地址:https://www.toptech.top/,,下面是具體步驟:
1、下載Nginx安裝包
wget -i -c http://nginx.org/download/nginx-1.17.5.tar.gz
2、安裝依賴包,新版本nginx依賴zlib-devel、pcre-devel,這里我們不妨把其他的也一并安裝
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
3、把下載的文件解壓
tar -zxvf nginx-1.17.5.tar.gz
4、安裝nginx:執(zhí)行以下命令
//進入nginx
cd nginx-1.17.5
//執(zhí)行命令
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
//執(zhí)行make命令
make
//執(zhí)行make install命令
make install
5、安裝完畢之后, 我們就可以啟動nginx
/usr/local/nginx/sbin/nginx
7、然后訪問這個地址localhost:80,就可以看到nginx的簡單界面了,nginx默認端口為:80
溫馨提示:
以下是nginx常用命令,首先進入到命令目錄:
cd /usr/local/nginx/sbin
然后想做什么操作,就執(zhí)行什么命令吧
# 啟動
./nginx
# 關(guān)閉
./nginx -s stop
# 重啟
./nginx -s reload
8、HTTPS具體配置
首先需要申請SSL安全證書,這里使用的阿里云DigiCert證書簽發(fā)服務,DigiCert在2017年12月1日并購了Symantec,大家不用擔心證書安全問題,這里選擇免費型DV SSL為例子演示,然后根據(jù)阿里云提示操作一步步申請即可:搜索SSL證書
這里我們選擇右邊的免費版
點擊證書申請
申請審核的時間很快的,兩分鐘成功后如下
下載證書,這里使用的是nginx ,所以
9、證書申請下來以后,下載nginx版本的證書,解壓文件以后,里面有兩個文件:
www.toptech.top.pem #證書文件
www.toptech.top.key #證書的密鑰文件
10、把這兩個文件上傳到nginx服務器,例如:我把文件放在了/usr/local/nginx/cert下
11、現(xiàn)在配置nginx的https,編輯nginx.conf文件:
vim /usr/local/nginx/conf/nginx.conf
內(nèi)容如下
#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 {
# 服務器主機配置:server ip:port weight=權(quán)重;
upstream toptech{
server 120.76.62.79:8080 weight=1;
server 120.76.62.79:8080 weight=1;
server 120.76.62.79:8080 weight=1;
server 120.76.62.79:8080 weight=1;
server 120.76.62.79:8080 weight=1;
server 120.76.62.79:8080 weight=1;
server 120.76.62.79:8080 weight=1;
server 120.76.62.79:8080 weight=1;
}
# 設置nginx允許上傳文件的大小
client_max_body_size 10m;
include mime.types;
default_type Application/octet-stream;
#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壓縮
gzip on;
server {
# 監(jiān)聽80端口
listen 80;
# 配置服務名稱
server_name www.toptech.top;
# 監(jiān)聽到http的80端口請求,進行https跳轉(zhuǎn)
return 301 https://$server_name$request_uri;
#charset koi8-r;
#access_log logs/host.access.log main;
# location / {
# proxy_pass http://toptech;
# }
#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;
#}
}
# 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
# 這里就是HTTPS的配置了
server {
# 監(jiān)聽443端口
listen 443 ssl;
# 服務名稱
server_name www.toptech.top;
# 這里配置SSL證書
ssl_certificate /usr/local/nginx/cert/www.toptech.top.pem;
# 這里配置SSL證書的密鑰
ssl_certificate_key /usr/local/nginx/cert/www.toptech.top.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
# 這里是監(jiān)聽到443端口的請求的跳轉(zhuǎn)鏈接
proxy_pass http://toptech.top:8080;
}
}
}
12、配置完畢重啟nginx
./nginx -s reload
以上端口為8080,是因為我的項目運行端口為8080,這個看情況而定,如果網(wǎng)站顯示還是不安全,說明網(wǎng)頁內(nèi)存在不是https的圖片