一、使用openssl生成证书
openssl是目前最流行的SSL密码库工具,其提供了一个通用、健壮、功能完备的工具套件,用以支持SSL/TLS协议的实现
#进入到生成目录
cd /usr/local/ssl
#生成key和crt,crt与pem通用
openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout yourname.key -out yourname.crt
#接下来填写国家啥的,一路回车也可以
二、Nginx配置
配置443端口
server {
listen 443 ssl;
server_name www.test.com;
ssl_certificate 你存放证书的目录/yourname.crt;
ssl_certificate_key 你存放证书的目录/yourname.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
# 禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击
server_tokens off;
# 如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站>全站加密,并且强制用 HTTPS 访问
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
location / {
root /usr/local/nginx/html/;
index index.html index.htm;
}
}
配置80端口跳转 http跳https
server {
listen 80;
server_name www.test.com;
#charset koi8-r;
#access_log logs/host.access.log main;
# 跳转
rewrite ^/(.*)$ https://www.test.com/$1 permanent;
location / {
root /usr/local/nginx/html/;
index index.html index.htm;
}
三、重启Nginx服务,并进行https访问即可
© 版权声明
文章版权归作者所有,未经允许请勿转载。