Nginx代理https功能

本文介绍如何在Linux环境下使用openssl工具生成SSL证书,包括安装openssl、生成私钥与证书签名请求、制作自签名证书,并配置Nginx支持HTTPS访问。

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

1.  安装openssl:

[root@node2 ~]# yum install -y openssl

2.  生成ssl证书:

[root@node2 ~]# cd /etc/pki

[root@node2 pki]# mkdir nginx

[root@node2 pki]# cd nginx/

## 生成2048位的加密私钥
[root@node2 nginx]# openssl genrsa -out server.key 2048

## 生成证书签名请求(CSR),这里需要填写许多信息
[root@node2 nginx]# openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN   ## 国家代号,中国输入CN
State or Province Name (full name) []:SN  ## 省的全名,拼音,或者缩写
Locality Name (eg, city) [Default City]:XiAn  ## 市的全名,拼音
Organization Name (eg, company) [Default Company Ltd]:openlab  ##  公司英文名
Organizational Unit Name (eg, section) []:  ## 组织单位名,可以不输入
Common Name (eg, your name or your server's hostname) []:www.openlab.com  
## 服务器主机名,若填写不正确,浏览器会报告证书无效
Email Address []:admin@123.com  ## 电子邮箱,可随意填满足格式即可

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:  ## 是否要做密码保护,可以不输入
An optional company name []:  ## 可以不输入

## 查看当前目录是否生成证书
[root@node2 nginx]# ls
server.csr  server.key

3.  生成类型为X509的自签名证书,有效期设置3650天。

## 生成类型为X509的自签名证书。有效期设置3650天
[root@node2 nginx]# openssl x509 -req -days 3650 -in server.csr -signkey 
server.key -out server.crt
Signature ok
subject=/C=CN/ST=SN/L=XiAn/O=openlab/CN=www.openlab.com/emailAddress=admin@123.com
Getting Private key

 4.  配置Nginx配置文件:

[root@node2 nginx]# vim /etc/nginx/nginx.conf

.......
server {
        listen       443 ssl http2;
        listen       [::]:443 ssl http2;
        server_name  _;
        root         /usr/share/nginx/html;

        ssl_certificate "/etc/pki/nginx/server.crt";  ## 自签名证书文件,绝对路径
        ssl_certificate_key "/etc/pki/nginx/server.key";  ## 加密私钥文件
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
}
.......

5.  配置访问页面:

[root@node2 nginx]# cd /usr/share/nginx/html/

[root@node2 nginx]# mkdir bbs

[root@node2 nginx]# vim bbs/index.html
this bbs pages

6.  访问" https://192.168.188.12/bbs ",查看https是否配置成功:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值