最近公司一客户要求服务器与客户端之间传输内容是加密的,通过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文件)

进入你想创建证书和私钥的目录

[[email protected] conf]# pwd
/app/nginx/conf
[[email protected] 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:
[[email protected] 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签名后形成服务端自己的证书。 

[[email protected] 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 []:[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:csdp
An optional company name []:51cto
[[email protected] conf]#

在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

[[email protected] conf]# cp server.key server.key.org
[[email protected] conf]# openssl rsa -in server.key.org -out server.key
Enter pass phrase for server.key.org:
writing RSA key
[[email protected] conf]#

标记证书使用上述私钥和CSR:

[[email protected] 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/[email protected]
Getting Private key
[[email protected] 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配置文件,让其包含新标记的证书和私钥:

[[email protected] conf]# cp nginx.conf{,20160919bak}
[[email protected] 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;
        }
 [[email protected] conf]# service nginx restart

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


wKiom1fd-QyzvRBMAAEeJ-z9G1E341.pngwKiom1fd-M_x4vZGAACLRWEGXA8977.png

 如果出现“[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共存。

 [[email protected] 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;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_session_cache    shared:SSL:10m;
        ssl_session_timeout  10m;
        ssl_prefer_server_ciphers  on;
        #fastcgi_param HTTPS $https if_not_empty;  #有https协议时自动使用https,否则忽略这个参数。
        #设定本虚拟主机的访问日志
        access_log /app/nginx/logs/access_fund.log fund;
  [[email protected] conf]# service nginx restart

用http和https访问同一个链接,应该都可以访问了。


SSL/TLS 系列中有五种协议:SSL v2,SSL v3,TLS v1.0,TLS v1.1和TLS v1.2:SSL v2 是不安全的,不能使用。此协议版本非常糟糕,即使它们位于完全不同的服务器(DROWN ***)上也可以用来***具有相同名称的RSA 密钥和站点。当与 HTTP(POODLE ***)一起使用时,SSL v3是不安全的,当与其他协议一起使用时,SSL v3 是弱的。它也是过时的,不应该被使用。

TLS v1.0 也是不应该使用的传统协议,但在实践中通常仍然是必需的。其主要弱点(BEAST)在现代浏览器中得到缓解,但其他问题仍然存在。

TLS v1.1 和 v1.2 都没有已知的安全问题,只有 v1.2 提供了现代的加密算法。

TLS v1.2 应该是您的主要协议,因为它是唯一提供现代认证加密(也称为 AEAD)的版本。如果您今天不支持 TLS v1.2,则缺乏安全性。

为了支持较旧的客户端,您可能需要继续支持 TLS v1.0 和TLS v1.1。但是,您应该计划在不久的将来退出 TLS v1.0。例如,PCI DSS 标准将要求所有接受信用卡付款的网站在 2018 年 6 月之前移除对 TLS v1.0 的支持。目前正在开展设计 TLS v1.3 的工作,其目的是消除所有过时和不安全的功能,并进行改进,以保持我们的通信在未来几十年内的安全。


wKiom1kK6o_TjQ-cAAB4ww0JlNE819.png


http强制跳转到https

  • 环境说明:

nginx之前配置的是https和http共存访问,后来把http取消了,只能通过https访问,但是有的客户已经习惯直接在浏览器里输入域名,这样默认就通过http访问就无法访问,现在要将http的访问强制调转到https。


采用nginx的rewrite方法:

  • 步骤:

server { 
	listen 80; 
	location / { 
		return 301 https://$host$request_uri; 
		#rewrite ^(.*)$  https://$host$1 permanent;        //这是ngixn早前的写法,现在还可以使用。
		#rewrite ^/(.*)$ http://ityunwei2017.51cto.com/$1 permanent;     //目前线上采用的是这种
		#rewrite ^ http://ityunwei2017.51cto.com$request_uri? permanent;
		#return 301 https://$server_name$request_uri;      //这是nginx最新支持的写法
}
server { 
    listen 443 ssl; 
}

多域名的时候,即访问51cto.com的http也会强制跳转到https://ityunwei2017.51cto.com上面,可以通过以下配置

server {
if ($host ~* "^51cto.com$") {
    rewrite ^/(.*)$ https://ityunwei2017.51cto.com/ permanent;
    }
}

或者更简洁点的

server {
if ($host = "ityunwei2017.51cto.com") {
    rewrite ^/(.*)$ http://ityunwei2017.51cto.com permanent;
}
}

如果http非默认端口强制跳转https配置,需要带上端口:return 301 https://$server_name:$server_port$request_uri;


采用nginx的497状态码

当网站只允许https访问时,使用http访问时nginx会报出497错误码。利用error_page命令将497状态码的链接重定向到https://ityunwei2017.51cto.com这个域名上


步骤:

ityunwei2017.51cto.com或者51cto.com的http都会被强制跳转到https

server {
    listen 80;
    error_page 497  https://$host$uri?$args;  
}