神奇的nginx之https支持

引言

随着技术的方法,http传输协议并不能保证数据传输的安全性,随后https技术应运而生,nginx服务器支持https协议,配置的代码也比较难记,记录下以防遗忘。


HTTPS数据传输过程

  1. 客户端向服务器发送https请求;
  2. 服务器上存储了一套数字证书,其实质为一对公私钥。数字证书可以自己制作,也可以向组织申请。前者在客户端访问时需要验证才能继续访问;后者不会弹出验证提示;
  3. 服务器将公钥传输给客户端;
  4. 客户端验证公钥是否合法:无效(自己制作的)会弹出警告,有效的则生成一串随机数,用此随机数加密公钥;
  5. 客户端将加密后的字符串传输给服务器
  6. 服务器收到字符串后,先使用私钥进行解密,获取加密使用的随机数,并以此随机数加密传输的数据(对称机密);
  7. 服务器将加密后的数据传输给客户端;
  8. 客户端收到数据后,使用自己的私钥(即随机字符串)进行解密。

对称加密:将数据和私钥(随机字符串)通过某种算法混合在一起,除非知道私钥,否则无法解密。


前期准备

nginx支持https所需的ngx-http_ssl_module在编译时默认是不安装的,需要二次编译安装(一开始就安装了的就不用再编译了)。

查看安装的nginx是否已安装ssl模块

[[email protected] conf]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx/ 

安装前需要注意一点:重新编译后可能导致之前做的某些修改重置,例如虚拟主机文件被清除,因此最好对重要配置文件先进行备份。

# 切换到你之前安装所使用的nginx软件包内
[[email protected] conf]# cd /usr/local/src/nginx-1.12.2/
[[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
[[email protected] nginx-1.12.2]# make && make install

[[email protected] conf]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx/ --with-http_ssl_module

创建自定义证书文件

  • 创建私钥key
[[email protected] ~]# cd /usr/local/nginx/conf
# 创建私钥key文件,必须输入密码,否则无法生成key文件
[[email protected] conf]# openssl genrsa -des3 -out tmp.key 2048
Generating RSA private key, 2048 bit long modulus
..............................+++
...............................................................+++
e is 65537 (0x10001)
Enter pass phrase for tmp.key:
Verifying - Enter pass phrase for tmp.key:
  • 转换key,取消密码
[[email protected] conf]# openssl rsa -in tmp.key -out test.key
Enter pass phrase for tmp.key:
writing RSA key

[[email protected] conf]# rm -f tmp.key 
  • 生成证书
[[email protected] conf]# openssl req -new -key test.key -out test.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
State or Province Name (full name) []:ZheJiang
Locality Name (eg, city) [Default City]:QuZhou
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server‘s hostname) []:
Email Address []:
Please enter the following ‘extra‘ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

# 需要使用csr文件与私钥一起生成.crt文件
[[email protected] conf]# openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt
Signature ok
subject=/C=CN/ST=ZheJiang/L=QuZhou/O=Default Company Ltd
Getting Private key

这样一个自定义创建的数字证书文件就创建成功了


SSL配置代码

  • 创建新虚拟主机配置文件
[[email protected] conf]#vim /usr/local/nginx/conf/vhost/ssl.conf
server
{
    listen 443;
    server_name test.com;
    index index.html index.php;
    root /data/www/test.com;
    ssl on;
    # 指定自定义的数字证书
    ssl_certificate test.crt;
    # 指定对应的key文件
    ssl_certificate_key test.key;
    ssl_protocols TLSv1 TLS1.1 TLS1.2;
}
  • 创建对应目录及文件
[[email protected] conf]# mkdir -p /data/www/test.com
[[email protected] conf]# vim /data/www/test.com/index.php
ssl test page.
  • 重启服务
[[email protected] conf]# /usr/local/nginx/sbin/nginx -t
[[email protected] conf]# /usr/local/nginx/sbin/nginx -s reload

# 查看443端口是否开放
[[email protected] conf]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
...
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      4953/nginx: master
...


神奇的nginx之https支持

原文地址:http://blog.51cto.com/castiel/2060000

时间: 2024-07-30 02:23:03

神奇的nginx之https支持的相关文章

Let's Encrypt: 为CentOS/RHEL 7下的nginx安装https支持-具体案例

环境说明: centos 7 nginx 1.10.2 前期准备 软件安装 yum install -y epel-release yum install -y certbot 创建目录及链接 方法1:在网站根目录下创建一个.well-known的目录 方法2: mkdir -p /usr/local/nginx/cert/.well-known ln -s /usr/local/nginx/cert/.well-known /data/www/example.com/.well-known l

nginx 添加https支持

自行颁发不受浏览器信任的SSL证书为晒晒IQ网颁发证书.ssh登陆到服务器上,终端输入以下命令,使用openssl生成RSA密钥及证书. # 生成一个RSA密钥 $ openssl genrsa -des3 -out ssl.key 1024 # 生成一个证书请求 $ openssl req -new -key ssl.key -out ssl.csr # 自己签发证书 $ openssl x509 -req -days 365 -in ssl.csr -signkey ssl.key -out

nginx安装https支持

安装软件yum install openssll nginx 在特定目录下生成证书mkdir /usr/share/nginx/confcd /usr/share/nginx/confopenssl genrsa -des3 -out server.key 1024生成服务器端的私钥server.keyopenssl req -new -key server.key -out server.csr生成的csr文件交给CA签名后形成服务端自己的证书,输入证书信息,国家.地区.公司.邮箱等opens

nginx添加模块与https支持

实例1:为已安装nginx动态添加模块 以安装rtmp媒流模块为例: 1)下载第三方模块到 [[email protected] nginx-1.8.1]# git clone https://github.com/arut/nginx-rtmp-module.git 2)查看nginx编译安装时安装的模块 [[email protected] nginx-1.8.1]# nginx -V nginx version: nginx/1.8.1 built by gcc 4.4.7 2012031

Nginx配置IPv6端口监听及务器设置IPV6及Https支持并通过AppStore审核

一.监听端口 从Nginx 1.3的某个版本起,默认ipv6only是打开的,所以,我们只需要在监听中加入ipv6监听即可,不过推荐都手动加上比较好,代码如下: listen [::]:80 ipv6only=on; server { listen 80; listen [::]:80 ipv6only=on; server_name index index.php index.html index.htm; root } 编辑完毕后保存,然后使用命令检测配置是否正确: nginx -t 重启n

nginx配置HTTPS

使用ssl模块配置同时支持http和https并存 一,生成证书 # 1.首先,进入你想创建证书和私钥的目录,例如: cd /etc/nginx/ # 2.创建服务器私钥,命令会让你输入一个口令: openssl genrsa -des3 -out server.key 1024 # 3.创建签名请求的证书(CSR): openssl req -new -key server.key -out server.csr # 4.在加载SSL支持的Nginx并使用上述私钥时除去必须的口令: cp se

nginx搭建https单向证书

一)默认情况下ssl模块并未被安装,如果使用该模块则需要在编译nginx的时指定--with-http_ssl_module参数. wget http://nginx.org/download/nginx-1.3.16.tar.gz tar -xf nginx-1.3.16.tar.gz -C /usr/local/ cd /usr/local/nginx-1.3.16/ ./configure --prefix=/usr/local/nginx --user=nginx --group=ngi

nginx 配置https并自签名证书

2016-10-28 转载请注明出处:http://daodaoliang.com/ 作者: daodaoliang 版本: V1.0.1 邮箱: [email protected] 参考链接: 这里 和 这里 和 官方文档 1. 制作服务器证书 服务器CA私钥: openssl genrsa -des3 -out ca.key 2048 制作解密后的CA私钥(一般无此必要): openssl rsa -in ca.key -out ca_decrypted.key ca.crt CA根证书(公

Nginx之Https最佳实践(跳转)

nginx的https协议需要ssl模块的支持,我们在编译nginx时使用--with-http_ssl_module参数加入SSL模块.还需要服务器私钥,服务器证书,如果是公司对外环境,这个证书需要购买第三方的权威证书,否则用户体验得不到保障. 1.1检查Nginx的SSL模块是否安装 [[email protected]~]# /application/nginx/sbin/nginx -V nginx version:nginx/1.9.13 built by gcc4.4.7 20120