前景:
趁着双十一,廉价入手了一台服务器,一个域名,现在为域名所解析的服务器配置SSL证书,使其能使用https安全加密的方式访问。
首先需要申请一本证书,当然也可以使用电脑生成一本,但是相对而言不算安全,可以前往阿里云申请一本免费的SSL证书。
申请完,下载下来发送到服务器上,配合Nginx 服务器配置安装证书:
#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 {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
client_max_body_size 3m;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
#gzip on;
# 80 转发至443
server {
listen 80;
server_name localhost;
rewrite ^(.*)$ https://$host$1 permanent;
location / {
index index.html index.htm;
}
}
# 以下属性中以ssl开头的属性代表与证书配置有关,其他属性请根据自己的需要进行配置。
server {
listen 443;
server_name yuming.com; # localhost修改为您证书绑定的域名。
ssl on; #设置为on启用SSL功能。
root html;
index index.html index.htm;
ssl_certificate domain/1691770_yuming.com.pem; #将domain name.pem替换成您证书的文件名。
ssl_certificate_key domain/1691770_yuming.com.key; #将domain name.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 / {
# 配置请求转发到内网ip的4000端口,静态资源所在
proxy_pass http://172.17.0.8:4000;
}
}
}
使用docker-compose.yml 在服务器上启动nignx,配置如下:
version: '2.0'
services:
nginx:
restart: always
image: nginx:stable-alpine
ports:
- 8080:80
- 80:80
- 443:443
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./conf.d:/etc/nginx/conf.d
- /var/log/nginx:/var/log/nginx
- /etc/letsencrypt:/etc/letsencrypt
- ./domain:/etc/nginx/domain
上面映射了三个端口,分别将宿主机的8080、80指向了nginx容器的80,443指向容器内的443。
运行nginx,使用docker命令启动,-d表示在后台运行
docker-compose up -d
接着请求域名:
https://host.com
访问成功,并且链接左边会出现一把锁,说明配置已成功,后面就能够使用https协议访问你的网站了,当然你还需要去备案。
参考链接:
https://help.aliyun.com/document_detail/98728.html?spm=5176.2020520163.cas.36.4baa56a7iBW9Ys