Docker 安装并配置nginx

一、HTTP 服务

Nginx 的最大作用,就是搭建一个 Web Server。有了容器,只要一行命令,服务器就架设好了,完全不用配置。

docker container run \
  -d \
  -p 8080:80 \
  --rm \
  --name mynginx \
  nginx:1.20.2

上面命令下载并运行官方的 Nginx image 版本是 1.20.2。
上面命令的各个参数含义如下。

-d:在后台运行
-p :容器的80端口映射到8080
--rm:容器停止运行后,自动删除容器文件
--name:容器的名字为mynginx

如果没有报错,就可以打开浏览器访问 192.168.1.11:8080 了。正常情况下,显示 Nginx 的欢迎页。

image

然后,把这个容器终止,由于--rm参数的作用,容器文件会自动删除。

$ docker container stop mynginx

二、映射网页目录

网页文件都在容器里,没法直接修改,显然很不方便。下一步就是让网页文件所在的目录/usr/share/nginx/html映射到本地。

新建html目录。

mkdir -p /docker/nginx/html

在这个子目录里面,放置一个index.html文件,内容如下。

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

接着,就可以把这个子目录html,映射到容器的网页文件目录/usr/share/nginx/html

docker container run \
  -d \
  -p 8080:80 \
  --rm \
  --name mynginx \
  --volume "$PWD/html":/usr/share/nginx/html \
  nginx:1.20.2

打开浏览器,访问 192.168.1.111:8080,应该就能看到   Welcome to nginx!  了。

三、拷贝配置

修改网页文件还不够,还要修改 Nginx 的配置文件,否则后面没法加 SSL 支持。
首先,把容器里面的 Nginx 配置文件拷贝到本地。

docker container cp mynginx:/etc/nginx .

上面命令的含义是,把mynginx容器的/etc/nginx拷贝到当前目录。不要漏掉最后那个点。

执行完成后,当前目录应该多出一个nginx子目录。然后,把这个子目录改名为conf

mv nginx conf

现在可以把容器终止了。

docker container stop mynginx

四、映射配置目录

重新启动一个新的容器,这次不仅映射网页目录,还要映射配置目录。

docker container run \
  --rm \
  --name mynginx \
  --volume "$PWD/html":/usr/share/nginx/html \
  --volume "$PWD/conf":/etc/nginx \
  -p 8080:80 \
  -d \
  nginx:1.20.2

上面代码中,--volume "$PWD/conf":/etc/nginx表示把容器的配置目录/etc/nginx,映射到本地的conf子目录。

浏览器访问 192.168.1.111:8080,如果能够看到网页,就说明本地的配置生效了。这时,可以把这个容器终止。

docker container stop mynginx

五、自签名证书

现在要为容器加入 HTTPS 支持,第一件事就是生成私钥和证书。正式的证书需要证书当局(CA)的签名,这里是为了测试,搞一张自签名(self-signed)证书就可以了。

下面,我参考的是 DigitalOcean 的教程。首先,确定你的机器安装了 OpenSSL,然后执行下面的命令。

sudo openssl req \
  -x509 \
  -nodes \
  -days 365 \
  -newkey rsa:2048 \
  -keyout example.key \
  -out example.crt

上面命令的各个参数含义如下。

req:处理证书签署请求。
-x509:生成自签名证书。
-nodes:跳过为证书设置密码的阶段,这样 Nginx 才可以直接打开证书。
-days 365:证书有效期为一年。
-newkey rsa:2048:生成一个新的私钥,采用的算法是2048位的 RSA。
-keyout:新生成的私钥文件为当前目录下的example.key。
-out:新生成的证书文件为当前目录下的example.crt。

执行后,命令行会跳出一堆问题要你回答,比如你在哪个国家、你的 Email 等等。

[root@localhost nginx]# sudo openssl req \
>   -x509 \
>   -nodes \
>   -days 365 \
>   -newkey rsa:2048 \
>   -keyout example.key \
>   -out example.crt
Generating a 2048 bit RSA private key
..+++
.....................................................+++
writing new private key to 'example.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]:CN
# 州或省名称(全称)
State or Province Name (full name) []:beijing
# 城市
Locality Name (eg, city) [Default City]:beijing
# 组织名称
Organization Name (eg, company) [Default Company Ltd]:oyz
# 公司名称
Organizational Unit Name (eg, section) []:oyz
# 服务器的域名
Common Name (eg, your name or your server's hostname) []:192.168.1.111
# 邮箱
Email Address []:haolb@qq.com
[root@localhost nginx]# ls
conf  example.crt  example.key  html
[root@localhost nginx]#

其中最重要的一个问题是 Common Name,正常情况下应该填入一个域名,这里可以填 192.168.1.111。

Common Name (eg, your name or your server's hostname) []:192.168.1.111

回答完问题,当前目录应该会多出两个文件:example.key example.crt

conf目录下新建一个子目录certs,把这两个文件放入这个子目录。

mkdir ./conf/certs
mv example.crt example.key ./conf/certs/

六、HTTPS 配置

有了私钥和证书,就可以打开 Nginx 的 HTTPS 了。

首先,打开conf/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;
    }
}

然后,启动一个新的 Nginx 容器。

[root@localhost nginx]# pwd
/docker/nginx
[root@localhost nginx]# ll
总用量 0
drwxr-xr-x. 4 root root 145 8月  29 16:14 conf
drwxr-xr-x. 2 root root  24 8月  29 15:45 html
[root@localhost nginx]# docker container run \
   --name nginx \
   --restart always \
   --volume "$PWD/html":/usr/share/nginx/html \
   --volume "$PWD/conf":/etc/nginx \
   -p 8080:80 \
   -p 8081:443 \
   -d \
   nginx:1.20.2
52eed0fd76afccf5c3724997b946c5f6fbd437df32f0ff131975ba00931c3983
[root@localhost nginx]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS             PORTS                                                                            NAMES
52eed0fd76af   nginx:1.20.2             "/docker-entrypoint.…"   4 seconds ago   Up 3 seconds       0.0.0.0:8080->80/tcp, :::8080->80/tcp, 0.0.0.0:8081->443/tcp, :::8081->443/tcp   nginx
ce90018683a8   mongo:4.2.2              "docker-entrypoint.s…"   5 hours ago     Up About an hour   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp                                    mongodb
7e1e3ec55bc3   redis:6.0                "docker-entrypoint.s…"   6 hours ago     Up About an hour   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp                                        redis
0a664907556c   postgis/postgis:12-3.2   "docker-entrypoint.s…"   2 days ago      Up About an hour   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp                                        postgis
[root@localhost nginx]#

上面命令中,不仅映射了容器的80端口,还映射了443端口,这是 HTTPS 的专用端口。

打开浏览器,访问 https://192.168.1.111:8081/ 。因为使用了自签名证书,浏览器会提示不安全。不要去管它,选择继续访问,应该就可以看到 Hello World 了。

posted @ 2022-08-29 14:28  我命由我不由天—hao  阅读(2286)  评论(0编辑  收藏  举报