Nginx配置HTTPS以及HTTPS原理

本文涉及的产品
密钥管理服务KMS,1000个密钥,100个凭据,1个月
.cn 域名,1个 12个月
简介: Nginx配置HTTPS以及HTTPS原理

一、HTTPS概述


(1)HTTPS简介


现在越来越多的公司都从HTTP转到了HTTPS,主要是为了数据安全,防止敏感信息被第三方获取


HTTPS是由两个部分组成的分别为HTTP和SSL和TLS,也就是说HTTPS就是在HTTP的基础上加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS和SSL进行加密, 也就是说使用HTTPS传输的数据都是经过加密的。SSL是服务器的加密,TLS是客户端的加密


(2)HTTPS原理


2021053109391488.png


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


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


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


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


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


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


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


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


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


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


二、Nginx实现HTTPS网站设置


(1)实验环境


系统 主机名 ip地址 nginx版本
Centos7.4 rzy 192.168.100.202 nginx-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  

进行测试,可以成功访问


20210531094000437.png

三、扩展


有些公司要求把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

访问测试

20210531094023894.png


******(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

20210531094037112.png

目录
相关文章
|
5天前
|
存储 应用服务中间件 Linux
nginx配置证书和私钥进行SSL通信验证
nginx配置证书和私钥进行SSL通信验证
21 4
|
3月前
|
安全 算法 网络安全
HTTPS原理
HTTPS 通过加密、数字证书、握手过程等多种手段,确保了网络通信的安全和可靠。它为用户提供了更高级别的隐私保护和数据安全,是现代互联网中重要的安全保障机制。随着网络安全威胁的不断增加,HTTPS 的应用也越来越广泛,成为保障网络安全的重要基石。
138 70
|
3月前
|
负载均衡 监控 应用服务中间件
配置Nginx反向代理时如何指定后端服务器的权重?
配置Nginx反向代理时如何指定后端服务器的权重?
195 61
|
2月前
|
应用服务中间件 Linux 网络安全
nginx安装部署ssl证书,同时支持http与https方式访问
为了使HTTP服务支持HTTPS访问,需生成并安装SSL证书,并确保Nginx支持SSL模块。首先,在`/usr/local/nginx`目录下生成RSA密钥、证书申请文件及自签名证书。接着,确认Nginx已安装SSL模块,若未安装则重新编译Nginx加入该模块。最后,编辑`nginx.conf`配置文件,启用并配置HTTPS服务器部分,指定证书路径和监听端口(如20000),保存后重启Nginx完成部署。
420 7
|
2月前
|
安全 算法 网络协议
【网络原理】——图解HTTPS如何加密(通俗简单易懂)
HTTPS加密过程,明文,密文,密钥,对称加密,非对称加密,公钥和私钥,证书加密
|
2月前
|
网络协议 安全 网络安全
探索网络模型与协议:从OSI到HTTPs的原理解析
OSI七层网络模型和TCP/IP四层模型是理解和设计计算机网络的框架。OSI模型包括物理层、数据链路层、网络层、传输层、会话层、表示层和应用层,而TCP/IP模型则简化为链路层、网络层、传输层和 HTTPS协议基于HTTP并通过TLS/SSL加密数据,确保安全传输。其连接过程涉及TCP三次握手、SSL证书验证、对称密钥交换等步骤,以保障通信的安全性和完整性。数字信封技术使用非对称加密和数字证书确保数据的机密性和身份认证。 浏览器通过Https访问网站的过程包括输入网址、DNS解析、建立TCP连接、发送HTTPS请求、接收响应、验证证书和解析网页内容等步骤,确保用户与服务器之间的安全通信。
125 3
|
2月前
|
存储 应用服务中间件 nginx
nginx反向代理bucket目录配置
该配置实现通过Nginx代理访问阿里云OSS存储桶中的图片资源。当用户访问代理域名下的图片URL(如 `http://代理域名/123.png`)时,Nginx会将请求转发到指定的OSS存储桶地址,并重写路径为 `/prod/files/2024/12/12/123.png`。
83 5
|
3月前
|
缓存 负载均衡 算法
如何配置Nginx反向代理以实现负载均衡?
如何配置Nginx反向代理以实现负载均衡?
|
2月前
|
负载均衡 前端开发 应用服务中间件
负载均衡指南:Nginx与HAProxy的配置与优化
负载均衡指南:Nginx与HAProxy的配置与优化
123 3
|
3月前
|
安全 应用服务中间件 网络安全
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
141 3