最近对自己的博客网站进行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:执行以下命令
//进入nginx
cd nginx-1.17.5
//执行命令
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
//执行make命令
make
//执行make install命令
make install
5、安装完毕之后, 我们就可以启动nginx
/usr/local/nginx/sbin/nginx
7、然后访问这个地址localhost:80,就可以看到nginx的简单界面了,nginx默认端口为:80
温馨提示:
以下是nginx常用命令,首先进入到命令目录:
cd /usr/local/nginx/sbin
然后想做什么操作,就执行什么命令吧
# 启动
./nginx
# 关闭
./nginx -s stop
# 重启
./nginx -s reload
8、HTTPS具体配置
首先需要申请SSL安全证书,这里使用的阿里云DigiCert证书签发服务,DigiCert在2017年12月1日并购了Symantec,大家不用担心证书安全问题,这里选择免费型DV SSL为例子演示,然后根据阿里云提示操作一步步申请即可:搜索SSL证书
这里我们选择右边的免费版
点击证书申请
申请审核的时间很快的,两分钟成功后如下
下载证书,这里使用的是nginx ,所以
9、证书申请下来以后,下载nginx版本的证书,解压文件以后,里面有两个文件:
www.toptech.top.pem #证书文件
www.toptech.top.key #证书的密钥文件
10、把这两个文件上传到nginx服务器,例如:我把文件放在了/usr/local/nginx/cert下
11、现在配置nginx的https,编辑nginx.conf文件:
vim /usr/local/nginx/conf/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 {
# 服务器主机配置:server ip:port weight=权重;
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 {
# 监听80端口
listen 80;
# 配置服务名称
server_name www.toptech.top;
# 监听到http的80端口请求,进行https跳转
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 {
# 监听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 / {
# 这里是监听到443端口的请求的跳转链接
proxy_pass http://toptech.top:8080;
}
}
}
12、配置完毕重启nginx
./nginx -s reload
以上端口为8080,是因为我的项目运行端口为8080,这个看情况而定,如果网站显示还是不安全,说明网页内存在不是https的图片