nginx配置https访问

为什么需要使用HTTPS,因为HTTP不安全,当使用http进行消息传输时,可能会遭到黑客的劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时信息泄露。

实现https:需要使用ssl协议。

一、证书简介


首先需要申请证书,先去登记机构进行身份登记,然后登记机构再通过CSR发给CA,CA中心通过后会生成一堆公钥和私钥,公钥会在CA证书链中保存,公钥和私钥证书被部署在WEB服务器上

  1. 当浏览器访问https站点时,会去请求我们的证书

  1. Nginx的web服务器会将公钥证书发给浏览器

  1. 浏览器再去验证证书是否合法有效

  1. CA机构会将过期的证书放置在CRL服务器,CRL服务的验证效率是非常差的,所以CA又推出了OCSP响应程序,OCSP响应程序可以查询指定的一个证书是否过期,所以浏览器可以直接查询OSCP响应程序,但OSCP响应程序性能还不是很高

  1. Nginx会有一个OCSP的开关,当我们开启后,Nginx会主动上OCSP上查询,这样大量的客户端直接从Nginx获取证书是否有效

1.1 证书的类型

对比

域名型 DV

企业型 OV

增强型 EV

绿色地址栏

小锁标记+https

小锁标记+https

小锁标记+企业名称+https

一般用途

个人站点和应用; 简单的https加密需求

电子商务站点和应用; 中小型企业站点

大型金融平台; 大型企业和政府机构站点

审核内容

域名所有权验证

全面的企业身份验证; 域名所有权验证

最高等级的企业身份验证; 域名所有权验证

颁发时长

10分钟-24小时

3-5个工作日

5-7个工作日

单次申请年限

1年

1-2年

1-2年

1.2 https注意事项

https不支持续费,证书到期需要重新申请并进行替换

https不支持三级域名解析,如 test.aa.bb.com

https显示绿色,说明整个网站的url都是https的

https显示黄色,因为网站代码中包含http的不安全链接

https显示红色,那么证书是假的或者证书过期。

二、nginx配置https


2.1 安装nginx

以安装nginx-1.23.3为例:

#下载nginx
wget http://nginx.org/download/nginx-1.23.3.tar.gz
#解压
tar -zxvf nginx-1.23.3.tar.gz
cd nginx-1.23.3
#安装主要依赖包
yum install -y libxml2 libxslt libxslt-devel libxml2-devel gd gd-devel geoip-devel
#编译
./configure \
--prefix=/usr/local/nginx \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module
#安装
make && make install

2.2 关于证书

可以去阿里云或者腾讯云免费申请,或者用openssl命令自制证书;

  • 阿里云申请地址

https://yundun.console.aliyun.com/?p=cas#/overview

  • openssl自制证书

#创建证书目录
[root@centos7 ~]# mkdir -p /usr/local/nginx/cert
[root@centos7 ~]# cd /usr/local/nginx/cert

##使用openssl命令充当CA权威机构创建证书(生产不使用此方式生成证书,不被互联网认可的黑户证书)
#创建证书
[root@centos7 cert]# openssl genrsa -idea -out server.key 2048
Generating RSA private key, 2048 bit long modulus
............+++
..................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:                            #输入密码1234
Verifying - Enter pass phrase for server.key:                #再次确认密码1234
#生成自签证书,同时去掉私钥的密码
[root@centos7 cert]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
Generating a 2048 bit RSA private key
..+++
........................................................+++
writing new private key to 'server.key'
-----
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            # 国家名,最多2个字符
State or Province Name (full name) []:CQ        # 省全称
Locality Name (eg, city) [Default City]:Cq        # 城市全称
Organization Name (eg, company) [Default Company Ltd]:test    # 组织机构名称
Organizational Unit Name (eg, section) []:test                # 组织单位名称
Common Name (eg, your name or your server's hostname) []:www.test.com    # 证书要保护的域名
Email Address []:137708020@qq.com                # email

2.3 配置ssl

修改nginx配置文件,开启https

server {
    listen 443 ssl;
    server_name    127.0.0.1;
    #填写证书文件名称
    ssl_certificate /usr/local/nginx/cert/server.crt;
    #填写证书私钥文件名称  
    ssl_certificate_key /usr/local/nginx/cert/server.key;

    ssl_session_timeout 5m;
    #表示使用的加密套件的类型
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    #表示使用的TLS协议的类型,您需要自行评估是否配置TLSv1.1协议。
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;

    ssl_prefer_server_ciphers on;

    location / {
        root   html;
        index  index.html index.htm;
    }
    ......
    ......
}

2.4 将 http 重定向 https

server {
    listen 80;
    server_name 127.0.0.1;
    #将请求转成https
    #早期写法
    rewrite ^(.*)$ https://$host$1;
    #最新写法
    return      301 https://$server_name$request_uri; 
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Winter Liu

别说话,打赏就行了!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值