8.4nginx配置HTTPS
centos yum 安装nginx 后增加模块,配置HTTPS
centos yum 安装nginx 后增加模块,
yum 和 源码安装的区别
yum 安装是在线安装,优点:安装方式简单,快捷;源码安装是将源码进行编译,生成可执行文件,优点:方便的添加模块等
yum安装nginx
系统版本:CentOS Linux release 7.4.1708 (Core)
1.增加对应的源
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.查看nginx信息
yum info nginx (查看nginx 版本方便后面下载对应版本)
3.yum安装nginx
yum -y install nginx
nginx 相关的命令
1.查看安装路径
rpm -ql nginx
2.查看编译参数
nginx -V #v大写
3.nginx 启动、停止、重启
systemctl start nginx #启动 nginx 服务
systemctl stop nginx #停止 nginx 服务
systemctl restart nginx #重启 nginx 服务
or
service nginx start
启动检查是否启动成功
curl -i localhost
如下显示说明正常启动:
···
Welcome to nginx!
···安装第三方模块
其实yum安装nginx 后想要添加第三方模块,只需对yum安装的nginx相同版本的源码进行编译后替换
1.安装源码安装需要的第三方包
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
2.下载对应的源码
通过nginx -V 可以知道yum 安装nginx 的版本为1.12.1,下载对应的源码
cd /opt
wget http://nginx.org/download/nginx-1.12.1.tar.gz
3.查看对应configure
tar xf nginx-1.12.1.tar.gz
cd nginx-1.12.1
nginx -V
./configure --help # 查看对应的模块是否能够安装
--with-http_ssl_module enable ngx_http_ssl_module enable 表示需要编译安装
4.增加对应的模块
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
...
--add-module=../headers-more-nginx-module
5.编译
make && make install
6.对可执行文件进行备份替换
cp /usr/sbin/nginx /usr/sbin/nginx.20181018 #备份
cp -r /etc/nginx /etc/nginx.20181018 # 备份
cp /opt/nginx-1.12.1/objs/nginx /usr/sbin/nginx #替换
systemctl restart nginx #重启 nginx 服务
HTTPS 配置
客户端通过 HTTPS 可与服务器进行安全的交互,不用担心消息会被拦截和读取。HTTPS 证书用来帮助客户端核实它们连接的服务器的身份。
服务器证书是一个公共实体,它被发送到连接到服务器的每个客户端。私钥则是一个安全实体,应该被存储到一个受访问限制的文件中,但是它必须对 Nginx 的主进程可读。
要使 Nginx 提供 HTTPS 功能,第一步是要创建 SSL 证书。首先在 nginx 的安装目录下创建一个 ssl 目录。
mkdir /etc/nginx/ssl
cd /etc/nginx/ssl
然后在 ssl 目录中创建 SSL 证书,如下是创建一个有效期 10 年,加密强度为 RSA2048 SSL 密钥 nginx.key 和 X509 证书文件 nginx.crt。
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout nginx.key -out nginx.crt
- -x509 指定使用 X.509 证书签名请求(Certificate Signing Request,CSR)管理。
- -node 告诉 openssl 在生成证书时忽略密码环节(此处需要 Nginx 自动读取此文件,而非是以用户交互的形式)。
- -day 指定证书的有效期。
- -newkey rsa:2048 表示生成一个新证书和一个新的 SSL key(加密强度为 RSA 2048)。
- -keyout 指定 SSL 输出文件名。
- -out 指定生成的证书文件名。
执行上述命令后,会要求填入下述信息。
Generating a 2048 bit RSA private key
.....................................+++
...........+++
writing new private key to '/etc/nginx/ssl/nginx.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]:hao
Organizational Unit Name (eg, section) []:hao
Common Name (eg, your name or your server's hostname) []:www.hao.com
Email Address []:admin@hao.com
SSL 创建完成后,第二部就是配置 Nginx 使用 SSL。首先是配置将 HTTP 请求的重定向到 HTTPS。
server {
listen 80;
server_name localhost 192.168.0.99 www.hao.com;
# HTTPS 配置
rewrite ^ https://$http_host$request_uri? permanent;
}
- rewrite,对 URL 进行重写。 语法:rewrite 规则 定向路径 重写类型; 规则,匹配目标 URL 的字符串或正则表达式。 定向路径,匹配到规则后要定向的路径。 $http_host 表示主机地址。 $request_uri 表示 URI。 重写类型, last,完成 rewrite 后,浏览器地址栏的 URL 不变。 break,本条规则匹配完成后,终止匹配,不再匹配后面的规则,浏览器地址栏的 URL 不变。 redirect,返回 302 临时重定向,浏览器地址会显示跳转后的 URL。 permanent,返回 301 永久重定向,浏览器地址栏会显示跳转后的 URL。
然后配置对 HTTPS 请求的处理。
server {
listen 443 ssl;
server_name www.hao.com;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
root /www/nginx/https;
index index.html index.jsp;
keepalive_timeout 70;
server_tokens off;
access_log /var/log/nginx/www.hao.com.access.log;
error_log /var/log/nginx/www.hao.com.error.log;
}
- listen,监听 443 端口并使用 ssl 参数。
- ssl_certificate,指定服务器证书的路径。
- ssl_certificate_key,指定私钥的路径。
- ssl_protocols,指定 SSL 协议(Nginx 默认使用)。
- ssl_ciphers,以 OpenSSL 库理解的格式指定密码(Nginx 默认使用)。
- server_tokens,关闭(显示)Nginx 版本号。
最后重启 Nginx 以使用新的配置文件。
service nginx restart
也可配置一个同时处理 HTTP/HTTPS 的服务器,server 上下文中的配置如下:
server {
listen 80;
listen 443 ssl;
server_name 192.168.0.99;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
root /www/nginx;
index index.html index.jsp;
keepalive_timeout 70;
server_tokens off;
access_log /var/log/nginx/www.hao.com.access.log;
error_log /var/log/nginx/www.hao.com.error.log;
}
HTTPS 服务器优化
SSL 连接会占用更多的更多的 CPU 资源(例如 SSL 握手),因此在多处理器系统上,应多运行几个工作进程(worker process)。有两种方法可以减少每个客户端执行 SSL 握手的操作:
- 首先使连接 keep-alive,然后通过一个连接发送多个请求。
- 然后是重用 SSL session 参数,以避免并行和后续连接的 SSL 握手。
Session 存储在 worker 共享的 SSL session 高速缓存中,并可通过 ssl_session_cache 指令配置。在 http 上下文中对 session 的配置如下所示。
http {
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
}
- ssl_session_cache,指定 SSL 共享缓存的大小为 10M。
- ssl_session_timeout,指定 SSL 共享缓存的超时为 10 mins
参考:https://www.jianshu.com/p/b9e02251e483
https://blog.csdn.net/zzy5066/article/details/81136273
https://blog.csdn.net/smartdt/article/details/80027579
https://www.cnblogs.com/jingxiaoniu/p/6745254.html