Docker(4)-nginx

This commit is contained in:
结发受长生 2018-06-26 17:29:42 +08:00
parent 7621b6ca2f
commit 478d0212b9
3 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,135 @@
---
title: 3.4、Docker(4)-nginx
date: 2018-2-10 22:38:32
tags:
- docker
- nginx
categories:
- linux
---
1. 使用docker部署nginx
2. 添加ssl支持
3. 添加http/2服务器推送支持
<!-- more -->
### 拉取nginx镜像
不填写版本号默认拉取最新版本的, 也就是latest
```bash
docker pull nginx
```
![nginx image](/images/linux/nginx_image.jpg)
### 启动容器
现在首先创建一个目录作为页面的根目录, 比如创建名为 **html**的目录
在里面创建一个测试页面index.html
```bash
docker run \
-d \
--rm \
--name testnginx \
--volume "/test/html":/usr/share/nginx/html \
-p 8080:80
```
+ volume是本地目录与容器内目录的映射, 前面是本地目录, 后面是容器内目录
也可以使用`mount`参数, 写作 **--mount source=/test/html,target=/usr/share/nginx/html**
+ 目录映射的信息可以通过`docker inspect [ContainerId]`来查看
```json
"Mounts": [{
"Type": "bind",
"Source": "/test/html",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}],
```
+ name 可以省略
现在我们可以访问 http://localhost:8080/ 来测试服务是否启动正常的
如果一切正常, 应该可以看到刚才编写的index.html页面内容
### 添加ssl支持
#### 生成证书
需要是在生产环境运行, 需要向证书颁发机构申请正式的证书
这里为了测试, 直接使用openssl生成
前提是需要安装openssl
之后执行
```bash
openssl req \
-x509 \
-nodes \
-days 365 \
-newkey rsa:2048 \
-keyout example.key \
-out example.crt
```
把生成的证书文件放到conf/certs里面
#### 修改nginx配置
如果要添加ssl支持, 可以使用https协议访问, 我们就需要修改配置文件
所以还需要把配置文件的目录映射出来
```bash
# 先把配置文件的目录整个拷贝到本地
docker container cp [ContainerId]:/etc/nginx ./
mv nginx conf #重命名
```
这里表示把指定容器中的指定目录, 拷贝到当前目录下
然后修改其中的配置文件
![nginx配置文件](/images/linux/nginx配置文件.jpg)
观察发现其中引入了conf.d里面的所有conf文件
所以我们可以直接去修改conf.d里面的
这样配置文件分离, 比较便于维护
修改`conf.d/default.conf`在最后添加
```
server {
listen 443 ssl http2;
server_name localhost;
ssl on;
ssl_certificate /etc/nginx/certs/example.crt;
ssl_certificate_key /etc/nginx/certs/example.key;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
```
#### 启动容器
先把之前启动的容器停止
`docker container [ContainerId] stop`
这次启动需要把配置文件的目录也添加上映射
```bash
docker run \
-d \
--rm \
--name testnginx \
--volume "/test/html":/usr/share/nginx/html \
--volume "/test/conf":/etc/nginx \
-p 8080:80
-p 8443:443
```
> 这里容器内程序启动占用的端口取决于配置, 如果有修改, 端口映射需要对应
然后就可以尝试访问 https://localhost:8443/ 了
当然自己生成的证书浏览器是不信任的, 添加例外即可
### HTTP/2
先背诵教科书, HTTP/2的新特性
1. 二进制分帧 : HTTP/2 采用二进制格式传输数据,而非 HTTP 1.x 的文本格式,二进制协议解析起来更高效
2. 多路复用 : 所有的请求都是通过一个TCP连接并发完成, HTTP 1.x中如果想并发多个请求必须使用多个TCP链接
3. 服务器推送 : 服务端可以在发送页面HTML时主动推送其它资源而不用等到浏览器解析到相应位置发起请求再响应
4. 头部压缩 : 对消息头采用HPACK专为http/2头部设计的压缩格式进行压缩传输能够节省消息头占用的网络的流量
上面的配置当中, 已经启用了http2( 向下兼容, 不用担心低版本浏览器问题 )
但是服务器推送并没有实现

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB