阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

配置Nginx网站https访问、http共存访问

178次阅读
没有评论

共计 4586 个字符,预计需要花费 12 分钟才能阅读完成。

最近公司一客户要求服务器与客户端之间传输内容是加密的,通过 https 协议访问,于是使用 OpenSSL 生成证书,默认情况下 ssl 模块并未被安装,如果要使用该模块则需要在编译 nginx 时指定–with-http_ssl_module 参数,需要确保机器上安装了 openssl 和 openssl-devel。

确认以上两点后就可以生成证书了
x509 证书一般会用到三类文,key,csr,crt。
Key:私用密钥 openssl 格,通常是 rsa 算法。
Csr:证书请求文件,用于申请证书。在制作 csr 文件的时,必须使用自己的私钥来签署申,还可以设定一个密钥。
crt:CA 认证后的证书文,(windows 下面的,其实是 crt),签署人用自己的 key 给你签署的凭证。
1. 创建服务器私钥(key 文件)
进入你想创建证书和私钥的目录
[root@localhost conf]# pwd
/app/nginx/conf
[root@localhost conf]# 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@localhost conf]#

运行时会提示输入至少四位的密码, 此密码用于加密 key 文件 (参数 des3 是指加密算法, 当然也可以选用其他你认为安全的算法,openssl 格式,1024 位强度,有的证书是要 2048。), 以后每当需读取此文件(通过 openssl 提供的命令或 API) 都需输入口令. 如果觉得不方便, 也可以去除这个口令, 但一定要采取其他的保护措施!
在加载 SSL 支持的 Nginx 并使用上述私钥时去除 key 文件口令的命令:openssl rsa -in server.key -out server.key
生成没有密码的 key:openssl rsa -in server.key -out server.key

2. 创建签名请求的证书(CSR 文件)
需要依次输入国家,地区,组织,email。最重要的是有一个 common name,可以写你的名字或者域名。如果为了 https 申请,这个必须和域名吻合,否则会引发浏览器警报。生成的 csr 文件交给 CA 签名后形成服务端自己的证书。
[root@localhost conf]# 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]:sh
State or Province Name (full name) []:shanghai
Locality Name (eg, city) [Default City]:shanghai
Organization Name (eg, company) [Default Company Ltd]:51cto
Organizational Unit Name (eg, section) []:51cto
Common Name (eg, your name or your server’s hostname) []:pvbutler.blog.51cto.com
Email Address []:justin@blog.51cto.com
 
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:csdp
An optional company name []:51cto
[root@localhost conf]#

在加载 SSL 支持的 Nginx 并使用上述私钥时除去必须的口令:
[root@localhost conf]# cp server.key server.key.org
[root@localhost conf]# openssl rsa -in server.key.org -out server.key
Enter pass phrase for server.key.org:
writing RSA key
[root@localhost conf]#

标记证书使用上述私钥和 CSR:
[root@localhost conf]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=sh/ST=shanghai/L=shanghai/O=51cto/OU=51cto/CN=pvbutler.blog.51cto.com/emailAddress=justin@blog.51cto.com
Getting Private key
[root@localhost conf]#

这样就生成了私用密钥:server.key 和自己认证的 SSL 证书:server.crt。
也可以使用下面命令:
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey server.key -CAcreateserial -out server.crt
-CA 选项指明用于被签名的 csr 证书,-CAkey 选项指明用于签名的密钥,-CAserial 指明序列号文件,而 -CAcreateserial 指明文件不存在时自动生成。
证书合并:cat server.key server.crt > server.pema
修改 Nginx 配置文件,让其包含新标记的证书和私钥:
[root@localhost conf]# cp nginx.conf{,20160919bak}
[root@localhost conf]# vim nginx.conf
 server {
        #指定虚拟主机的服务端口
        listen 443;
 
        #指定 IP 地址或者域名
        server_name pvbutler.blog.51cto.com;
        ssl on;
        ssl_certificate /app/nginx/conf/server.crt;
        ssl_certificate_key /app/nginx/conf/server.key;
        #设定本虚拟主机的访问日志
        access_log /app/nginx/logs/access_fund.log fund;
        }
 [root@localhost conf]# service nginx restart

这样就可以通过 https://10.10.2.83/ 方式访问:

配置 Nginx 网站 https 访问、http 共存访问

配置 Nginx 网站 https 访问、http 共存访问

如果出现“[emerg] 10464#0: unknown directive “ssl” in /app/nginx/conf/nginx.conf:74”则说明没有将 ssl 模块编译进 nginx,在 configure 的时候加上“–with-http_ssl_module”即可 ^^

这样生成的证书,访问都会提示安全认证,如何让浏览器信任自己颁发的证书呢?
控制面板 -> Internet 选项 -> 内容 -> 发行者 -> 受信任的根证书颁发机构 -> 导入 -》选择 server.crt

如果要不出现这样的提示,需要到第三方 ssl 证书提供商处购买。具体申请过程
可以询问证书商。
——————————————————————————–
实现 https 和 http 共存访问
上面的配置后只能通过 https 访问,下面的配置实现 https 和 http 共存。
[root@localhost conf]# vim nginx.conf
 server {
        #指定虚拟主机的服务端口
        listen 80;
        listen 443 ssl;
 
        #指定 IP 地址或者域名
        server_name pvbutler.blog.51cto.com;
        #ssl on;  #这行一定要注释掉
        ssl_certificate /app/nginx/conf/server.crt;
        ssl_certificate_key /app/nginx/conf/server.key;
        #设定本虚拟主机的访问日志
        access_log /app/nginx/logs/access_fund.log fund;
  [root@localhost conf]# service nginx restart

更多 Nginx 相关教程见以下内容

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

Ubuntu 16.04 LTS 上安装 Nginx、MariaDB 和 HHVM 运行 WordPress http://www.linuxidc.com/Linux/2016-10/136435.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Linux(RHEL7.0)下安装 Nginx-1.10.2 http://www.linuxidc.com/Linux/2016-10/136484.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里 

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-01/139085.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计4586字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中