HTTP--http之https(九)

本文介绍如何使用OpenSSL生成SSL证书的公钥和私钥,以及如何配置Nginx服务器以实现HTTPS访问,并设置HTTP重定向到HTTPS,确保网站的安全连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

生成公钥和私钥

用git运行以下命令,然后一直按回车

openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout localhost-privkey.pem -out localhost-cert.pem

clipboard.png

目录结构

clipboard.png

文件内容

test.conf
    server {
        listen       443;
        server_name  test.com;

        ssl on;
        ssl_certificate_key ../certs/localhost-privkey.pem;
        ssl_certificate ../certs/localhost-cert.pem;

        location / {
            proxy_cache my_cache;
            proxy_pass http://127.0.0.1:8888;
            proxy_set_header Host $host;
        }
    } 

运行本地服务

test.com

clipboard.png

https://test.com

clipboard.png

clipboard.png

test.com重定向到https://test.com

test.conf
    proxy_cache_path cache levels=1:2 keys_zone=my_cache:10m;

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name test.com;
        return 302 https://$server_name$request_uri;
    }

    server {
        listen       443;
        server_name  test.com;

        ssl on;
        ssl_certificate_key ../certs/localhost-privkey.pem;
        ssl_certificate ../certs/localhost-cert.pem;

        location / {
            proxy_cache my_cache;
            proxy_pass http://127.0.0.1:8888;
            proxy_set_header Host $host;
        }
    } 

clipboard.png

clipboard.png

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值