Nginx上部署HTTPS

  Nginx上部署HTTPS依赖OpenSSL库和包含文件,即须先安装好libssl-dev,且ln -s /usr/lib/x86_64-linux-gnu/libssl.so  /usr/lib/,然后在编译配置Nginx时要指定--with-http_ssl_module。另外,要在Shell中运行openssl命令,还要安装openssl包,本人用的OpenSSL-1.0.2g。注:本文采用Ubuntu 16.04上的操作实例

  下图展示了数字证书(HTTPS中使用的由CA签名的公钥证书)的签名和验证原理(流程):

  • Nginx上部署HTTPS
  1. 自签发证书:生成证书可以在其他机器上去执行,然后再将所生成server.crt和server.key复制一份至Nginx的/usr/local/nginx/conf下即可

    $ cd /usr/local/nginx/conf
    $ openssl genrsa -des3 -out server.key 1024 #建议:2048
    $ openssl req -new -key server.key -out server.csr #证书签名请求(CSR)
    $ cp server.key server.key.org
    $ openssl rsa -in server.key.org -out server.key
    $ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  2. 修改配置文件nginx.conf:为减少CPU负载,建议只运行一个工作进程,并且开启keep-alive。另外,version 0.6.7以上的Nginx ssl_certificate和ssl_certificate_key的默认关联目录为nginx.conf所在的目录,默认文件名都为cert.pem

    worker_processes 1;
    server {
    
        server_name YOUR_DOMAINNAME_HERE;
        listen 443 ssl;
        listen 80;
        if ($scheme = http) {
                rewrite ^(.*)$ https://$server_name$1 permanent;
        }
        ssl_certificate server.crt;
        ssl_certificate_key server.key;
        keepalive_timeout    70;
    }
  3. 重启Nginx:HTTPS在Nginx上的部署至此已近完毕,然后就可以通过https://YOUR_DOMAINNAME_HERE来访问了。由于本例中采用自签发证书(不同于CA自签名的root证书),在Chrome下将看到如图警告信息,表明该证书不受信任。浏览器在默认情况下内置了一些CA机构的证书,使得这些机构颁发的证书受到信任。

    

  • 私钥保护:私钥是重要的财产,尽可能限制能接触到私钥的人
  1. 在一台可信的计算机上生成私钥和CSR(Certificate Signing Requests)。有一些CA会为你生成密钥和CSR,但这样做明显不妥
  2. 受密码保护的密钥可以阻止在备份系统中被截获
  3. 在发现被截获后,撤回老的证书,生成新的密钥和证书
  4. 每年更新证书,总是使用最新的私钥
  • 部署证书链:证书链(Certificate Chain)包括信任锚(CA 证书)和签名证书,是由一系列 CA 证书发出的证书序列,最终以根 CA 证书结束;Web 浏览器已预先配置了一组浏览器自动信任的根 CA 证书,来自其他证书授权机构的所有证书都必须附带证书链,以检验这些证书的有效性。在很多部署场景中,单一的服务器证书显得不足,而多个证书则需要建立一个信任链。一个常见的问题是正确的配置了服务器证书但却搞忘了包含其他所需要的证书。此外,虽然其他证书通常有很长的有效期,但她们也会过期,如果她们过期就会影响整个链条。你的CA应该提供所有额外需要的证书。一个无效证书链会导致服务器证书失效和客户端浏览器报警告,这个问题有时候不是那么容易被检测到,因为有些浏览器可以自己重构一个完整的信任链而有些则不行。关于Nginx上部署证书链:

    if you have a chain certificate file (sometimes called an intermediate certificate)
    you don‘t specify it separately like you do in Apache. Instead you need to add the
    information from the chain cert to the end of your main certificate file. This can be done
    by typing "cat chain.crt >> mysite.com.crt" on the command line. Once that is done you
    won‘t use the chain cert file for anything else, you just point Nginx to the main certificate file

   下图展示了证书链的工作原理:

     

  1. ssl:开启HTTPS

    syntax:ssl [on|off]

    default:ssl off

    context:main, server

  2. ssl_certificate:证书文件,默认证书和密钥都位于cert.pem中,该文件还可以包含其他证书。自version 0.6.7起,ssl_certificate的默认关联目录为nginx.conf所在的目录。

    syntax:ssl_certificate file

    default:ssl_certificate cert.pem

    context:main, server 

  3. ssl_certificate_key:证书密钥文件,默认密钥位于cert.pem中。自version 0.6.7起,ssl_certificate_key的默认关联目录为nginx.conf所在的目录。

    syntax:ssl_certificate_key file

    default:ssl_certificate_key cert.pem

    context:main, server

  4. ssl_client_certificate:Indicates file with certificates CA in PEM format, utilized for checking the client certificates.

    syntax:ssl_client_certificate file

    default:none

    context:main, server

  5. ssl_dhparam:Indicates file with Diffie-Hellman parameters in PEM format, utilized for negotiating TLS session keys.

    syntax: ssl_dhparam file

    default: none

    context: main, server

  6. ssl_ciphers:Directive describes the permitted ciphers. Ciphers are assigned in the formats supported by OpenSSL.

    syntax: ssl_ciphers file

    default: ssl_ciphers ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP

    context: main, server

    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

    Complete list can be looked with the following command:

    openssl ciphers
  7. ssl_prefer_server_ciphers:Requires protocols SSLv3 and TLSv1 server ciphers be preferred over the client‘s ciphers.

    syntax: ssl_prefer_server_ciphers [on|off]

    default: ssl_prefer_server_ciphers off

    context: main, server

  8. ssl_protocols:Directive enables the protocols indicated. TLS v1.0以上的版本是比较安全的,最好是弃用SSLv3以下的版本,SSLv2坚决不用

    syntax: ssl_protocols [SSLv2] [SSLv3] [TLSv1]

    default: ssl_protocols SSLv2 SSLv3 TLSv1

    context: main, server

  9. ssl_session_cache:The directive sets the types and sizes of caches to store the SSL sessions.

    syntax:ssl_session_cache off|none|builtin:size and/or shared:name:size

    default:ssl_session_cache off

    context:main, server

    ssl_session_cache builtin:1000 shared:SSL:10m;
  10. ssl_session_timeout:Assigns the time during which the client can repeatedly use the parameters of the session, which is stored in the cache.

    syntax:ssl_session_timeout time

    default:ssl_session_timeout 5m

    context:main, server

  • 参考
  1. SSL/TLS部署最佳实践http://www.techug.com/post/ssl-tls.html
  2. Nginx HttpSSLhttp://www.nginx.cn/doc/optional/ssl.html
时间: 2024-07-30 04:02:21

Nginx上部署HTTPS的相关文章

Nginx上部署HTTPS + HTTP2

Nginx上部署HTTPS依赖OpenSSL库和包含文件,即须先安装好libssl-dev(或者OpenSSL),且ln -s /usr/lib/x86_64-linux-gnu/libssl.so  /usr/lib/,然后在编译配置Nginx时要指定--with-http_ssl_module和--with-http_v2_module.另外,若要在本地运行openssl命令,要安装OpenSSL包,本人用的OpenSSL-1.0.2g.注:本文采用Ubuntu 16.04上的操作实例. 下

SSL/TLS深度解析--在 Nginx 上部署 TLS

利用 openssl 源代码安装 Nginx [[email protected] software]# tar xf nginx-1.15.5.tar.gz [[email protected] software]# cd nginx-1.15.5/ [[email protected] nginx-1.15.5]# groupadd nginx [[email protected] nginx-1.15.5]# useradd nginx -M -s /sbin/nologin -g ngi

mac os x 之通过远程主机在nginx上部署web静态页面

1.mac使用ssh命令登陆远程主机 因为苹果mac os x自带ssh命令,所以我们只需打开终端输入 $ ssh [email protected] 在这之前最好在服务器上上传自己的ssh key,避免每次登陆输入密码 稍作等待就连接上服务器了   2.mac使用scp命令向远处主机上传文件 在终端窗口,按下command+n,打开另一个终端窗口,并输入 $ scp ~/local/file [email protected]:~/file  当然一般我们上传的是文件夹,所以加上-r $ sc

在NGINX上配置HTTPS服务

使用 NGINX 配置 HTTPS服务的步骤如下: 首先应该为你的域名申请一个ssl证书,如果是阿里云的服务器可以直接在阿里云的控制台中申请一个ssl安全证书. 在阿里云平台申请证书完成后,点击下载证书将证书下载到本地.然后用ftp工具传输到你自己的服务器上 listen 443 ssl 监听443号端口 server_name 你服务器的ip或者ip绑定的域名 ssl_certificate 服务器上xxx.pem文件的路径(最好填绝对路径) ssl_certificate_key 服务器上x

Nginx 下部署 HTTPS 与安全调优

什么是 HTTPS?# HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版. 更多基本介绍请查阅: 数字签名是什么?(图文并茂, 清晰易懂, 重点推荐) HTTPS on WIKI 密码学笔记 SSL 与数字证书 另一个图文并茂的笔记, 供参考 --> 泛域名ssl证书搭建全攻略 需要弄清楚的几个问题: HTTPS 和 SSL 的关系与基本技术实现; SSL 证书的

nginx上部署PHP

环境:centos7  nginx1.16.1 (1)先将tp源代码下载到nginx目录下 tp放到/usr/local/nginx/目录下,root为相对路径,如: tp在/usr/local/nginx/wwwroot/ 则 root wwwroot/public; tp不再nginx目录,则使用alias 绝对路径指定,不需要root. 注意:存放tp的目录要有可执行权限,否则无法进入目录,访问报403 (2)servr配置: server { listen 80; server_name

在nginx上部署页面,使用ip访问页面,实现跨设备访问本地静态页面

1.在nginx官网下载并安装nginx,下载链接:http://nginx.org/en/download.html. 2.修改安装包中conf文件夹中的nginx.conf的root路径,使用#将默认的路径注释掉,增加新的root路径,如下: location / { #root html; root F:/weike/dazuoye; index index.html index.htm; } 3.命令行输入ipconfig查看本机ip地址 4.在pc端或移动端浏览器输入http://xx

NET Core Kestrel部署HTTPS

NET Core Kestrel部署HTTPS ASP.NET Core配置 Kestrel部署HTTPS.现在大部分网站已经部署HTTPS,大家对于安全越来越重视. 今天简单介绍一下ASP.NET Core 部署HTTPS,直接通过配置Kestrel.大家也可以通过前置Nginx来部署HTTPS. 下面直接进入正题. 新建项目并添加引用 新建一个ASP.NET Core Web Application  模板选择空. 新建好项目添加引用 Microsoft.AspNetCore.Server.

一次部署HTTPS的相关事件引发的思考

前言: 上周五快要下班的时候,突然收到通知客户希望了解一下部署HTTPS的流程,这种事情谁听了都会有几分诧异的.因为这件事虽然和工作有一定的相关度,但平时不会走这个方向,实际上也较少接触.此外,客户手下应该不缺人,做运维和开发的肯定比我更懂这个,但情况却和我想的不一样. 正文: 客户有需求,就应该尽量满足!因此,尽管之前对Apache.Tomcat的一些配置不熟,也未有过自己部署HTTPS的经验[当然失败的尝试还是有的],便趁着周末了解了一下相关的东西,在本地搭建了环境.实践表明,当你对一个东西