使用openssl生成证书,并通过Nginx配置https访问、访问http自动重定向到https访问

 

创建服务器证书密钥文件 server.key

openssl genrsa -des3 -out server.key 2048

这个时候会提示输入密码 这个密码要记住

 

openssl语法

openssl  genrsa [-out filename] [-passout arg] [-f4] [-3] [-rand file(s)] [-engine id] [numbits] [-des] [-des3] [-idea]

 

复制代码
usage: genrsa [args] [numbits]
 -des            encrypt the generated key with DES in cbc mode
 -des3           encrypt the generated key with DES in ede cbc mode (168 bit key)
 -idea           encrypt the generated key with IDEA in cbc mode
 -seed
                 encrypt PEM output with cbc seed
 -aes128, -aes192, -aes256
                 encrypt PEM output with cbc aes
 -camellia128, -camellia192, -camellia256
                 encrypt PEM output with cbc camellia
 -out file       output the key to 'file
 -passout arg    output file pass phrase source
 -f4             use F4 (0x10001) for the E value
 -3              use 3 for the E value
 -engine e       use engine e, possibly a hardware device.
 -rand file:file:...
                 load the file (or the files in the directory) into
                 the random number generator
复制代码

 

 

 

创建服务器证书的申请文件 server.csr

openssl req -new -key server.key -out server.csr

会要求输入下面内容

输出内容为:
Enter pass phrase for root.key: 输入前面创建的密码
Country Name (2 letter code) [AU]:CN   国家代号,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing   省的全名,拼音
Locality Name (eg, city) []:BeiJing  市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Yvioo  公司英文名(可以随便输入)
Organizational Unit Name (eg, section) []:  单位名 可以不输入
Common Name (eg, YOUR name) []: 输入你的名字
Email Address []:admin@mycompany.com  电子邮箱随便填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:  可以不输入
An optional company name []:   可以不输入
 
 
备份一份服务器密钥文件
cp server.key server.key.org

 

去除文件口令

openssl rsa -in server.key.org -out server.key

会要求输入之前的密码 输入一开始的密码

 

生成证书文件server.crt

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

 

然后文件夹下会有四个文件 

 

 

 

 

配置Nginx的证书

 

 这个路径根据自己的来

复制代码
# HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  localhost;
        
        #访问http自动跳转到https上 
        #error_page 497 301 =307 https://$host:$server_port$request_uri;       
 
        ssl_certificate      /usr/share/nginx/html/ssl/server.crt;
        ssl_certificate_key  /usr/share/nginx/html/ssl/server.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
复制代码

 

 

以上方式只允许使用ssl方式访问,上面的端口443是https的默认端口,不一定必须使用默认端口,可以是其他端口,其他端口只需要在访问的时候带上端口访问即可

 

如果需要设置访问http请求自动跳转到https请求上,可以在server_name下增加配置 (多层代理情况下可以出现反复重定向,需要找其他方式)

error_page 497 301 =307 https://$host:$server_port$request_uri;

 

 

 

//到对应目录,检验nginx配置文件是否有错误

./nginx -t

 

重新加载nginx配置文件

./nginx - s reload

 

 

posted @   yvioo  阅读(4250)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-06-30 tomcat启动报错There is insufficient memory for the Java Runtime Environment to continue
2020-06-30 JAVA中价格金额的存储类型
点击右上角即可分享
微信分享提示