Nginx配置HTTPS以及HTTPS原理

Posted 礁之

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx配置HTTPS以及HTTPS原理相关的知识,希望对你有一定的参考价值。

文章目录

一、HTTPS概述

(1)HTTPS简介

  • 现在越来越多的公司都从HTTP转到了HTTPS,主要是为了数据安全,防止敏感信息被第三方获取
  • HTTPS是由两个部分组成的分别为HTTP和SSL和TLS,也就是说HTTPS就是在HTTP的基础上加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS和SSL进行加密, 也就是说使用HTTPS传输的数据都是经过加密的。SSL是服务器的加密,TLS是客户端的加密

(2)HTTPS原理

  1. 客户端通过HTTPS协议访问服务端的443端口

  2. 服务端在能够使用HTTPS的情况下,是有公钥和私钥的,公钥就是证书。服务器端向客户端进行回应,并且发送证书,也就是公钥

  3. 客户端在收到证书后,会向CA请求判断证书是否有效,如果无效,客户端就会提示警告信息,提示此证书不安全

  4. 证书有效的话,客户端就会生成一个随即值

  5. 客户端会用服务端发送来的证书向随即值进行加密然后发送给服务端

  6. 服务端收到后,会使用本地的私钥解开,从而获得客户端的随即值。在服务端发送数据时,会使用随即值对发送的数据进行加密也就是再生成一个相当于是公钥,而随即值就是私钥。

    加密使用对称加密算法,就是将信息和私钥通过某种算法混合在一起,只有持有私钥(随即值)的主机才可以获取数据。

  7. 服务端向客户端发送被加密的数据

  8. 客户端收到数据使用随即值进行解密,从而成功成功传送数据

CA: 是一个颁发证书的机构

二、nginx实现HTTPS网站设置

(1)实验环境

系统主机名ip地址nginx版本
Centos7.4rzy192.168.100.202nginx-1.18.0

(2)实验目的

客户端使用浏览器可以访问https

(3)实验步骤

注意:一般生成的目录,应该放在nginx/conf/ssl目录

使用的模块: --with-http_ssl_module

******(1)先进行基础配置
[root@Centos7 ~]# hostnamectl set-hostname rzy
[root@Centos7 ~]# su
[root@rzy ~]# systemctl stop firewalld
[root@rzy ~]# setenforce 0
setenforce: SELinux is disabled
[root@rzy ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
mount: /dev/sr0 已经挂载或 /mnt 忙
       /dev/sr0 已经挂载到 /mnt 上

******(2)上传Nginx源码包进行安装
[root@rzy ~]#  yum -y install pcre-devel zlib-devel popt-devel openssl-devel openssl
。。。。。。
完毕!
[root@rzy ~]# useradd -M -s /sbin/nologin  nginx
[root@rzy ~]# rz
z waiting to receive.**B0100000023be50
[root@rzy ~]# tar xf nginx-1.18.0.tar.gz  -C /usr/src/
[root@rzy ~]# cd /usr/src/nginx-1.18.0/
[root@rzy nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make && make install
[root@rzy nginx-1.18.0]# cd 
[root@rzy ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@rzy ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@rzy ~]# systemctl start nginx 
[root@rzy ~]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3786/nginx: master  

******(3)创建服务器证书密钥文件
[root@rzy ~]# openssl genrsa -des3 -out server.key 1024    #生成密钥
Generating RSA private key, 1024 bit long modulus
................++++++
............................++++++
e is 65537 (0x10001)
Enter pass phrase for server.key: #输入密码
Verifying - Enter pass phrase for server.key:  #确认密码
[root@rzy ~]# openssl req -new -key server.key -out server.csr #生成证书认证文件
Enter pass phrase for 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  #国家
State or Province Name (full name) []:beijing  #省
Locality Name (eg, city) [Default City]:beijing  #市
Organization Name (eg, company) [Default Company Ltd]:baidu #公司
Organizational Unit Name (eg, section) []:  #邮箱,可以不输入
Common Name (eg, your name or your server's hostname) []:www.aaa.com  #域名,这个域名必须和nginx使用的域名相同
Email Address []:  #不用写

Please enter the following 'extra' attributes 
to be sent with your certificate request
A challenge password []:  #不用写
An optional company name []:  #不用写
[root@rzy ~]# ll
总用量 1028
-rw-------. 1 root root    1264 1月  12 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root 1039530 4月  19 10:03 nginx-1.18.0.tar.gz
-rw-r--r--  1 root root     627 4月  26 23:21 server.csr
-rw-r--r--  1 root root     951 4月  26 23:21 server.key
[root@rzy ~]# cp server.key server.key.org  #复制一份密码
[root@rzy ~]# openssl rsa -in server.key.org -out server.key  #删除密码,更安全
Enter pass phrase for server.key.org:  #之前的密码
writing RSA key
[root@rzy ~]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt #生成公钥也就是证书
Signature ok
subject=/C=CN/ST=beijing/L=beijing/O=baidu/CN=www.aaa.com
Getting Private key

******(4)修改配置文件
[root@rzy ~]# cd /usr/local/nginx/conf/
[root@rzy conf]# cp nginx.conf nginx.conf.bak
[root@rzy conf]# sed -i '/#/d' nginx.conf
[root@rzy conf]# sed -i '/^$/d' nginx.conf
[root@rzy conf]# vim nginx.conf
  1 worker_processes  1;
  2 events 
  3     worker_connections  1024;
  4 
  5 http 
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server 
 11         listen       443 default ssl;
 12         ssl On;
 13         ssl_certificate ssl/server.crt;  #指定证书路径
 14         ssl_certificate_key ssl/server.key; #指定私钥路径
 15         server_name  www.aaa.com; #指定域名,要和证书认证文件的域名相同
 16         location / 
 17             root   html;
 18             index  index.html index.htm;
 19         
 20         error_page   500 502 503 504  /50x.html;
 21         location = /50x.html 
 22             root   html;
 23         
 24     
 25 
[root@rzy conf]# cd 
[root@rzy ~]# mkdir -p /usr/local/nginx/conf/ssl
[root@rzy ~]# cp server.crt server.key /usr/local/nginx/conf/ssl/
[root@rzy ~]# systemctl restart nginx #重启服务器
[root@rzy ~]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      3844/nginx: master  

进行测试,可以成功访问

三、扩展

有些公司要求把http转到https上面,只需要修改一下配置文件即可

******(1)使用状态码进行跳转
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf
  1 worker_processes  1;
  2 events 
  3     worker_connections  1024;
  4 
  5 http 
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server 
 11         listen 80;
 12         listen       443 default ssl;
 13         ssl On;
 14         ssl_certificate ssl/server.crt;
 15         ssl_certificate_key ssl/server.key;
 16         server_name  www.aaa.com;
 17         error_page 497 https://$server_name$1;
 18         location / 
 19             root   html;
 20             index  index.html index.htm;
 21         
 22         error_page   500 502 503 504  /50x.html;
 23         location = /50x.html 
 24             root   html;
 25         
 26     
 27 
[root@rzy ~]# systemctl restart nginx

访问测试

******(2)使用虚拟主机进行重定向
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf
  1 worker_processes  1;
  2 events 
  3     worker_connections  1024;
  4 
  5 http 
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server 
 11         listen       443 default ssl;
 12         ssl On;
 13         ssl_certificate ssl/server.crt;
 14         ssl_certificate_key ssl/server.key;
 15         server_name  www.aaa.com;
 16         location / 
 17             root   html;
 18             index  index.html index.htm;
 19         
 20     
 21     server 
 22         listen 80;
 23         server_name www.aaa.com;
 24         location / 
 25             rewrite ^(.*) https://$server_name$1 redirect;
 26         
 27     
 28 
[root@rzy ~]# systemctl restart nginx

以上是关于Nginx配置HTTPS以及HTTPS原理的主要内容,如果未能解决你的问题,请参考以下文章

nginx配置https访问

HTTPS_SSL配置的步骤以及原理说明

HTTPS_SSL apache认证配置的步骤以及原理说明

购买https证书以及nginx配置https

Nginx Ingress 控制器工作原理

nginx基本用法和HTTPS配置