12.20 Nginx配置ssl
时间:2022-04-27
本文章向大家介绍12.20 Nginx配置ssl,主要内容包括Nginx配置ssl目录概要、Nginx配置ssl、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
Nginx配置ssl目录概要
- vim /usr/local/nginx/conf/vhost/ssl.conf//加入如下内容
server
{
listen 443;
server_name aming.com;
index index.html index.php;
root /data/wwwroot/aming.com;
ssl on;
ssl_certificate aminglinux.crt;
ssl_certificate_key aminglinux.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}
- -t && -s reload //若报错unknown directive “ssl” ,需要重新编译nginx,加上--with-http_ssl_module
- mkdir /data/wwwroot/aming.com
- echo “ssl test page.”>/data/wwwroot/aming.com/index.html
- 编辑hosts,增加127.0.0.1 aming.com
- curl https://aming.com/
Nginx配置ssl
- 在有了公钥和私钥之后,配置nginx
- 生成新的配置文件 vim /usr/local/nginx/conf/vhost/ssl.conf
[root@hf-01 conf]# vim /usr/local/nginx/conf/vhost/ssl.conf
添加以下内容
server
{
listen 443; //监听端口为443
server_name aming.com; //主机名
index index.html index.php;
root /data/wwwroot/aming.com; //root 目录
ssl on; //开启ssl
ssl_certificate gurui.crt; //指定公钥
ssl_certificate_key gurui.key; //指定私钥
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; //ssl 的协议
}
保存退出
- ssl 的协议,一般情况下,三种协议都配置上
- 创建/data/wwwroot/aming.com目录
[root@hf-01 conf]# mkdir /data/wwwroot/aming.com
[root@hf-01 conf]#
- 检查配置文件语法
[root@hf-01 conf]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@hf-01 conf]#
- 报错:
- 因为不知道这个 ssl 配置,在编译nginx的时候,并没有指定支持ssl
[root@hf-01 conf]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments: --prefix=/usr/local/nginx
[root@hf-01 conf]#
- 解决办法
- 重新编译nginx
- 重新编译nginx
[root@hf-01 conf]# cd /usr/local/src/nginx-1.12.1/
[root@hf-01 nginx-1.12.1]# ./configure --help |grep -i ssl
--with-http_ssl_module enable ngx_http_ssl_module
--with-mail_ssl_module enable ngx_mail_ssl_module
--with-stream_ssl_module enable ngx_stream_ssl_module
--with-stream_ssl_preread_module enable ngx_stream_ssl_preread_module
--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenSSL
[root@hf-01 nginx-1.12.1]#
- 编译的时候需要加上--with-http_ssl_module
- 初始化./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@hf-01 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
- 编译make
[root@hf-01 nginx-1.12.1]# make
- 然后make install
[root@hf-01 nginx-1.12.1]# make install
- 查看nginx的编译参数,会看到增加了--with-http_ssl_module
[root@hf-01 nginx-1.12.1]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.1
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
[root@hf-01 nginx-1.12.1]#
- 检查配置文件语法错误
[root@hf-01 nginx-1.12.1]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@hf-01 nginx-1.12.1]#
- 重启nginx
[root@hf-01 nginx-1.12.1]# /etc/init.d/nginx restart
Restarting nginx (via systemctl): [ 确定 ]
[root@hf-01 nginx-1.12.1]#
- 查看监听端口,会看到多出一个443端口
[root@hf-01 nginx-1.12.1]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1533/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 5716/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5716/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1205/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1533/master
tcp6 0 0 :::3306 :::* LISTEN 1576/mysqld
tcp6 0 0 :::22 :::* LISTEN 1205/sshd
[root@hf-01 nginx-1.12.1]#
- 切换目录路径,并创建一个测试文件
[root@hf-01 nginx-1.12.1]# cd /data/wwwroot/aming.com/
[root@hf-01 aming.com]# ls
[root@hf-01 aming.com]# vim index.html
This is ssl.
保存退出
- 测试,若是直接访问会报400
[root@hf-01 aming.com]# curl -x127.0.0.1:443 https://aming.com/
curl: (56) Received HTTP code 400 from proxy after CONNECT
[root@hf-01 aming.com]#
- 在虚拟机中 /etc/写hosts
[root@hf-01 aming.com]# vim /etc/hosts
加入以下内容
127.0.0.1 aming.com
- 测试,不指定-x访问
[root@hf-01 aming.com]# curl https://aming.com/
curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
[root@hf-01 aming.com]#
- 就是说你这个证书被标记为不可信任了,因为这个证书是自己颁发的,实际上是已经配置成功了
- 在windows中的host文件添加,并保存
192.168.74.129 aming.com
- 浏览器访问aming.com,会看到加载超时
- 这时查看虚拟机防火墙iptables -nvL,若是防火墙存在,可以直接ipbables -F清空所有规则,若不想清空所有规则可以增加443端口的规则
[root@hf-01 aming.com]# iptables -nvL
[root@hf-01 aming.com]# iptables -F
[root@hf-01 aming.com]#
- 这时再来访问aming.com,会提示是否信任证书,选择 是 ,会访问成功
- 这个就是自己颁发证书,浏览器不被信任的时候,会显示红色 不安全 ,而不是绿色
- 以后若想正常的访问https,可以去沃通买证书
- Oracle 数据库之最:你见过最高的 SQL Version 是多少?
- Android 子activity关闭 向父activity传值
- 《Redis设计与实现》读书笔记(三十一) ——Redis集群消息类型
- 统计学中的相关性分析
- 《Redis设计与实现》读书笔记(三十二) ——Redis事务设计与实现
- 收藏一个简洁的PHP可逆加密函数
- 《Redis设计与实现》读书笔记(三十二) ——Redis集发布订阅设计与实现
- Android点击EditText文本框之外任何地方隐藏键盘的解决办法
- Spark 1.4为DataFrame新增的统计与数学函数
- 《Redis设计与实现》读书笔记(三十三) ——Redis排序命令sort的实现
- 高通HAL层之Sensor HAL
- Android Data Binding(数据绑定)用户指南
- Android 禁止Viewpager左右滑动功能
- 高通Audio中ASOC的machine驱动
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Hive-1.2.1_04_DML操作 5.1. Join8.1. 使用案例8.2. Transform实现
- Hive-1.2.1_05_案例操作
- Mysql字符编码利用技巧
- Hive-1.2.1_06_累计报表查询
- Flume-1.8.0_部署与常用案例
- GlassFish 目录穿越漏洞测试过程
- 安装CDH7.1.1
- GIT-SHELL 沙盒绕过(CVE-2017-8386)
- Azkaban-2.5.0-部署与常见案例
- 无字母数字webshell之提高篇
- Sqoop-1.4.7-部署与常见案例
- Hbase-2.0.0_01_安装部署
- 前端黑魔法之远程控制地址栏
- 攻击LNMP架构Web应用的几个小Tricks
- Hbase-2.0.0_02_常用操作