Nginx HTTPS 实现、自定义 Server 名称及升级 OpenSSL(二)|学习笔记

简介: 快速学习 Nginx HTTPS 实现、自定义 Server 名称及升级 OpenSSL

开发者学堂课程【Nginx 企业级 Web 服务实战:Nginx HTTPS 实现、自定义 Server 名称及升级 OpenSSL】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址https://developer.aliyun.com/learning/course/649/detail/10761


Nginx HTTPS 实现、自定义 Server 名称及升级 OpenSSL(二)

五、实现多域名 HTTPS

Nginx 支持基于单个 IP 实现多域名的功能,并且还支持单 IP 多域名的基础之上实现HTTPS,其实是基于 Nginx 的 SNI (Server Name Indication) 功能实现,SNI 是为了解决一个 Nginx 服务 器内使用一个 IP 绑定多个域名和证书的功能,其具体功能是客户端在连接到服务器建立 SS L 链接之前先发送要访问站点的域名(Hostname),这样服务器再根据这个域名返回给客户端一个合适的证书。

#制作 key 和 csr 文件

[root0s2 certs]# openssl req -newkey rsa:4096 -nodes -sha256 - keyout

mobile . magedu .net .key -out mobile . magedu .net .csr

Generating a 4096 bit RSA private key

…………

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] :magedu

Organizational Unit Name (eg, section) [] :magedu

Comon Name (eg, your name or your server's hostname) []:mobile . magedu .net

Email Address [] : 2973707860@qq . com

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:

An optional company name [] :

#签名证书

[root0s2 certs]# openssl x509 -req -days 3650 -in mobile.magedu.net.csr -CA ca.crt

Cakey ca.ke -CAcreateserial-out mobile.magedu.net.crt

Signature oksubject=/C=CN/ST=BeiJing/L=BeiJing/0=magedu/0U=magedu/CN=obile.magedu.net/emailAddress=29737078600qq。com

Getting CA Private Key

#验证证书内容

[root@s2 certs]# openss1 x509 -in mobile . magedu.net.crt -noout -text

Certificate:

Data:

Version: 1 (0x0)

Serial Number :  

bb:76:ea:fe: f4:04:8C:07

Signature Algorithm: sha256W1 thRSAEncryption

Issuer: C=CN,ST=BeiJing, L=Beijing, 0=magedu.Ltd, ou=magedu,

CN=magedu. ca/ emailAddress-2973707860@qq. com

Validity

Not Before: Feb 22 13:50:43 2019 GMT

Not After : Feb 19 13:50:43 2029 GNT

Subject: C=CN, ST=BeiJing, L=BeiJing, 0=magedu. net, oU=magedu.net,

CN=www. magedu. net/emailAddress-2973707860@qq. com

Subject Public Key Info:

Public Key Algorithm: rsaEncryption

Public-Key: (4096 bit )

#Nginx 配置

[root@s2 certs]# cat /apps/nginx/conf/conf . d/mobile . conf

server {

listen   80;

server_ name mobile .magedu.net;

location 1 {

root   html;

index   index.html index. htm;

}

location /inux38 {

root   /data/nginx/mobile/html;

index   index.html index. htm;

}

location /python {

root   /data/nginx/mobile/html;

index   index.html index.htm;

}

}

Server {

listen 443 ssl;

server_name mobile .fagedu.net;  

ssl_certificate /apps/nginx/certs/mobile.magedu.net.crt;

ssl_certificate.key /apps/nginx/certs/mobile.magedu.net.key;

ssl_session cache shared:sslcache :20m;

ssl_ session_ tineout 100;  

location / {

root   html;

index   index.html index .htm;

}

location /linux38 {

root   /data/nginx/mobile/html;

index   index. html index.htm;

}

location /python {

root   /data/nginx/mobile/html;

index   index. html index htm;

}

六、关于 favicon.ico

favicon.ico 文件是浏览器收藏网址时显示的图标,当客户端使用浏览器问页面时,浏览器会自己主动发起请求获取页面的 favicon.co 文件,但是当浏览器请求的 favicon.ico 文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错。

解决办法:

#一:服务器不记录访问日志:

#location = /favicon.ico {

#1og.not_ound off;

#access_1og off;

#}

#二:将图标保存到指定目录访问:

#location - /favicon\.icos {

location = /favicon.ico {

root   /data/nginx/html/pc/images;

expires 90d;  #设置文件过期时间

}

七、安全选项

1、隐藏 Nginx 版本号:

更改 nginx 源码信息并重新编译 Nginx

# vim src/http/ngx. http. header. filter. module.c

49 static u char ngx. http. server. string[] = "Server: 1inux38" CRLF;#定义响应报文中的 server 字段信息

▼Response Headers   view source

Connection: keep- alive

Date: Mon, 28 oct 2019 16:32:10 GMT

ETag:"5db41959-19"

Keep-Alive: timeout=65

Last-Modified: sat, 26 oct 2019 10:00:57 GMT

Server:magedu/1.2

2、升级 OpenSSL 版本:

心脏出血(英语: Heartbleed) ,也简称为心血漏洞,是一个出现在加密程序库 OpenSSL 的安全漏洞,该程序库广泛用于实现互联网的传输层安全(TLS) 协议。

它于2012年被引入了软件中,2014年4月首次向公众披露。只要使用的是存在缺陷的 OpenSSL 实例,无论是服务器还是客户端,都可能因此而受到攻击。

此问题的原因是在实现 TLS 的心跳扩展时没有对输入进行适当验证(缺少边界检查) ,因此漏洞的名称来源于“心跳”(heartbeat)。该程序错误属于缓冲区过读,即可以读取的数据比应该允许读取的还多。

准备 Openssl 源码包:

# pwd

/usr/1ocal/src

# tar xvf openss1-1.1.1d

编译安装 Nginx 并制定新版本 OpenSSL 路径:

# cd /usr/1ocal/src/nginx-1.16.1/

#./configure - -prefix=/apps/nginx --user=nginx --group=nginx --with-http. ssl module --

with-http_v2_module --with-http. realip_ module --with-http. stub_ status_ module --with-

http_gzip_static_module --with-pcre --with-stream --with-stream_ ssl. module --with-

stream_realip_module --with-select. module - -with-file-aio -- add-

module=/usr/1ocal/src/echo-nginx- module . with- openssl=/usr/1ocal/src/openss1-1.1.1d

# make && make install

验证并启动 Nginx:

# /apps/nginx/sbin/nginx -t

nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /apps/nginx/conf/nginx.conf test is successful

# /apps/nginx/sbin/nginx

相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
3月前
|
应用服务中间件 nginx
百度搜索:蓝易云【HTTP请求是如何关联Nginx server{}块的?】
总结来说,Nginx中的 `server{}`块用于关联HTTP请求和虚拟主机,通过配置不同的 `server{}`块,可以实现多个域名或IP地址的请求分发和处理。这样,Nginx可以根据不同的请求来提供不同的服务和内容。
37 0
|
4月前
|
应用服务中间件 网络安全 nginx
nginx 常用命令 |升级到1.20.1版本 | 如何更换 Nginx SSL 证书
nginx 常用命令 |升级到1.20.1版本 | 如何更换 Nginx SSL 证书
157 0
|
16天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
26 0
|
26天前
|
应用服务中间件 nginx
nginx配置https和直接访问静态文件的方式
nginx配置https和直接访问静态文件的方式
28 3
|
1月前
|
弹性计算 算法 应用服务中间件
倚天使用|Nginx性能高27%,性价比1.5倍,基于阿里云倚天ECS的Web server实践
倚天710构建的ECS产品,基于云原生独立物理核、大cache,结合CIPU新架构,倚天ECS在Nginx场景下,具备强大的性能优势。相对典型x86,Http长连接场景性能收益27%,开启gzip压缩时性能收益达到74%。 同时阿里云G8y实例售价比G7实例低23%,是Web Server最佳选择。
|
2月前
|
tengine Rust 负载均衡
反向代理学习笔记(一) Nginx与反向代理绪论
反向代理学习笔记(一) Nginx与反向代理绪论
|
2月前
|
网络协议 Unix 应用服务中间件
如何进行 Nginx HTTPS服务器搭建
【2月更文挑战第6天】
63 0
|
2月前
|
缓存 应用服务中间件 网络安全
nginx服务升级配置
nginx服务升级配置
|
3月前
|
Web App开发 网络协议 Linux
Linux C/C++ 开发(学习笔记十 ):实现http请求器(TCP客户端)
Linux C/C++ 开发(学习笔记十 ):实现http请求器(TCP客户端)
49 0
|
4月前
|
应用服务中间件 Linux 网络安全
Linux【脚本 06】HTTPS转发HTTP安装OpenSSL、Nginx(with-http_ssl_module)及自签名的X.509数字证书生成(一键部署生成脚本分享)
Linux【脚本 06】HTTPS转发HTTP安装OpenSSL、Nginx(with-http_ssl_module)及自签名的X.509数字证书生成(一键部署生成脚本分享)
66 1