使用docker容器部署nginx

1. nginx相关路径

配置目录:/etc/nginx
静态页面目录:/usr/share/nginx/html


2. 容器化运行nginx,搭建HTTP服务

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

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

部署成功后,访问IP:8080 ,即可看到nginx的默然欢迎页面。


3. 映射网页目录

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

mkdir html

cd html

在这个子目录里面,放置一个index.html文件

vim index.html

<h1>Hello World</h1>

把这个子目录html,映射到容器的网页文件目录/usr/share/nginx/html。打开浏览器,访问 IP:8080,应该就能看到Hello World了

docker run -d \
-p 8080:80 \
-v $pwd/html:/usr/share/nginx/html \
--name mynginx \
nginx

4. 拷贝配置目录

把容器里面的Nginx配置文件拷贝到本地。

docker cp mynginx:/etc/nginx .

上面命令的含义是,把mynginx容器的/etc/nginx拷贝到当前目录。不要漏掉最后那个点。执行完成后,当前目录应该多出一个nginx子目录。然后,把这个子目录改名为conf。

mv nginx conf

现在可以把mynginx容器终止了

docker stop mynginx

5. 映射配置目录

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

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

注:“$PWD/html”,"$PWD/conf"要加上双引号。$PWD要大写,不能小写。

-v "$pwd/conf":/etc/nginx表示把宿主机的conf子目录映射到容器的配置目录/etc/nginx中。浏览器访问IP:8090,如果能够看到网页,就说明本地配置生效了。这时,可以把这个容器终止。

docker stop mynginx

6. 自签名证书

现在要为容器加入HTTPS支持,第一件事就是生成私钥和证书。正式的证书需要证书当局(CA)的签名,这里是为了测试,搞一张自签名(self-signed)证书就可以了。首先,确定你的机器安装了 OpenSSL,然后执行下面的命令。

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等等。其中最重要的一个问题是 Common Name,正常情况下应该填入一个域名,这里可以填你的IP。回答完问题,当前目录应该会多出两个文件:example.key和example.crt。在conf目录下新建一个子目录certs,把这两个文件放入这个子目录。

mkdir -p conf/certs

mv example.crt example.key conf/certs

7. HTTPS配置

有了私钥和证书,就可以打开Nginx的HTTPS了。首先,打开conf/conf.d/default.conf文件

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

在以上的server模块{}中,添加下面的配置。

server {
    # http 配置
    ...
    
    # https 配置
    listen 443 ssl http2;
    server_name  localhost;

    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;
}

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

cd /path/to/nginx

docker run -d \
-v "$PWD/html":/usr/share/nginx/html \
-v "$PWD/conf":/etc/nginx \
-p 8080:80 \
-p 8443:443 \
--name mynginx \
nginx

上面命令中,不仅映射了容器的80端口,还映射了443端口,这是HTTPS的专用端口。打开浏览器,访问https://IP:8443/。因为使用了自签名证书,浏览器会提示不安全。不要去管它,选择继续访问,应该就可以看到Hello World了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值