---
title: "Nginx+HTTP2+HTTPS 环境搭建"
author: "xiaonanforever"
date: "2016-03-18T13:39:59Z"
tags: []
categories: ["互联网"]
slug: "nginx-http2-https"
draft: false
lastmod: "2016-03-18T13:39:59Z"
id: 2284556
url: "https://xiaonanforever.iteye.com/blog/2284556"
---

## 准备

下载以下依赖

- libressl   
  &gt; 在滴血事件之后，作为OpenSSL的替代
- zlib
- pcre

以上内容不需要自行make，通过nginx一并manke。   
为后续升级方便，相关依赖解压缩到/opt，建立没有版本号的软连接

### Nginx安装

./configure –prefix=/opt/nginx –user=forever –group=forever –with-http\_stub\_status\_module –with-http\_v2\_module –with-http\_ssl\_module –with-openssl=/opt/libressl –with-ipv6 –with-http\_gzip\_static\_module –with-http\_realip\_module –with-http\_flv\_module –with-pcre=/opt/pcre –with-zlib=/opt/zlib   
make && make install

安装完成之后会在/opt/nginx安装内容。

### 生成HTTPS证书

- 生成私钥Key   
  openssl genrsa -aes256 -out private/test.chehejia.com.key.pem 2048
- 生成证书请求（向第三方认证机构申请）   
  openssl req -new -key private/test.chehejia.com.key.pem -out private/test.chehejia.com.key.csr -subj “/C=CN//ST=BJ/L=BJ/O=CHJ/OU=NET/CN=test.chehejia.com”   
  把csr文件提交证书机构进行申请。
- 生成自签发证书   
  openssl req -new -x509 -key openssl.key -out openssl.crt -days 3650

### Nginx部署

在conf/nginx.conf中编辑https

01. `# HTTPS server`
02. `server {`
03. `listen 443 ssl http2 default_server;`
04. `server_name localhost;`
05. `ssl_certificate private/test.chehejia.com.crt;`
06. `ssl_certificate_key private/test.chehejia.com.key;`
07. `ssl_session_cache shared:SSL:1m;`
08. `ssl_session_timeout 5m;`
09. `ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:AES256+EECDH:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4';`
10. `ssl_prefer_server_ciphers on;`
11. `location / {`
12. `root html;`
13. `index index.html index.htm;`
14. `}`
15. `}`

### 启动验证

cd $NGINX\_HOME   
sbin/nginx