使用 Nginx 快速搭建静态资源服务器,并配置 ssl 证书
2021年6月3日大约 1 分钟
环境:
腾讯云 CentOS7.8
Nginx1.19.10
申请 ssl 证书:https://console.cloud.tencent.com/ssl3
下载证书,将 nginx 目录中的两个文件上传至服务器,位置自定

编写 nginx 配置文件
# 这是静态资源服务器的配置文件
server {
listen 443 ssl;
server_name 域名;
root 映射目录的路径;
# ssl配置
ssl_certificate .crt文件路径;
ssl_certificate_key .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 ~ .*\.(jpg|png|ico)(.*){
expires 30d;
}
# 缓存配置
location ~ .*\.(js|css)(.*){
expires 7d;
}
location / {
add_header Access-Control-Allow-Origin *;
# 输入密码时的提示语
#auth_basic "口令";
# 显示认证时的用户密码文件存放路径
#auth_basic_user_file /etc/ssl/password;
# 显示索引
autoindex on;
# 【字节显示】还是【单位显示】
autoindex_exact_size on;
# 时间是否用本地时间
autoindex_localtime on;
# 指定返回格式,语法:autoindex_format html | xml | json | jsonp; 默认:autoindex_format html;
autoindex_format html;
}
}
#把http的域名请求转成https
server {
listen 80;
server_name 域名;
return 301 https://$host$request_uri;
}- 重新加载配置文件
nginx -t
nginx -s reload