Docker安装nginx并配置https

目录

一、生成ssl证书

二、准备nginx配置文件

三、创建文件夹

四、创建nginx

五、测试


一、生成ssl证书

1.openssl生成私钥文件,并输入密码

[root@node187 home]# ll
总用量 0
drwxr-xr-x 4 root root 33 1月   4 15:13 es
drwxr-xr-x 6 root root 53 1月   5 16:57 nginx

[root@node187 home]# openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
..........+++
..........................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:

2.去除口令

[root@node187 home]# mv server.key server.key.back

[root@node187 home]# openssl rsa -in server.key.back -out server.key
Enter pass phrase for server.key.back:
writing RSA key

3.创建请求证书 

[root@node187 home]# openssl req -new -key server.key -out server.csr
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]:haidian
Organizational Unit Name (eg, section) []:zhongguancun
Common Name (eg, your name or your server's hostname) []:dream
Email Address []:123@qq.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:zk

[root@node187 home]# ll
总用量 12
drwxr-xr-x 4 root root   33 1月   4 15:13 es
drwxr-xr-x 6 root root   53 1月   5 16:57 nginx
-rw-r--r-- 1 root root 1102 1月   6 11:09 server.csr
-rw-r--r-- 1 root root 1675 1月   6 11:06 server.key
-rw-r--r-- 1 root root 1743 1月   6 11:06 server.key.back

 4.生成证书文件

[root@node187 home]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJING/O=haidian/OU=zhongguancun/CN=dream/emailAddress=123@qq.com
Getting Private key

5.查看已经生成的文件

[root@node187 home]# ll
总用量 16
drwxr-xr-x 4 root root   33 1月   4 15:13 es
drwxr-xr-x 6 root root   53 1月   5 16:57 nginx
-rw-r--r-- 1 root root 1285 1月   6 11:10 server.crt
-rw-r--r-- 1 root root 1102 1月   6 11:09 server.csr
-rw-r--r-- 1 root root 1675 1月   6 11:06 server.key
-rw-r--r-- 1 root root 1743 1月   6 11:06 server.key.back

二、准备nginx配置文件

 default.conf 内容如下:

server {
    listen       8081;
    #listen  [::]:8081;
    server_name  localhost;

    listen 443 ssl; #侦听443端口,用于SSL
    # 注意文件位置,是从/etc/nginx/下开始算起的
    #ssl  on;
    ssl_certificate ssl/server.crt;
    ssl_certificate_key ssl/server.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

    # gzip config
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 9;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript applic
ation/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";

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

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

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

三、创建文件夹

1.创建文件夹

[root@node187 nginx]# mkdir conf
[root@node187 nginx]# mkdir html
[root@node187 nginx]# mkdir logs
[root@node187 nginx]# mkdir ssl
[root@node187 nginx]# ll
总用量 0
drwxr-xr-x 3 root root 20 1月   5 16:57 conf
drwxr-xr-x 2 root root 24 1月   5 16:24 html
drwxr-xr-x 2 root root 41 1月   5 16:23 logs
drwxr-xr-x 2 root root 42 1月   6 10:25 ssl

2.将 default.conf 放入 /home/nginx/conf/conf.d 文件夹

[root@node187 nginx]# cd conf/conf.d
[root@node187 conf.d]# ll
总用量 4
-rw-r--r-- 1 root root 1840 1月   6 10:45 default.conf

3.将 server.crt 和 server.key 放入/home/nginx/ssl文件夹中

[root@node187 nginx]# cd ssl/
[root@node187 ssl]# ll
总用量 8
-rw-r--r-- 1 root root 1330 1月   5 17:03 server.crt
-rw-r--r-- 1 root root 1679 1月   5 17:02 server.key
[root@node187 ssl]# 

4.在/home/nginx/html放入项目或测试访问文件

四、创建nginx

1.拉取镜像

docker pull nginx

2.创建容器

docker run --d --name newnginx \
-p 443:443 \
-p 8081:8081 \
--privileged=true \
-v /home/nginx/html:/usr/share/nginx/html:rw \
-v /home/nginx/conf/conf.d:/etc/nginx/conf.d:rw \
-v /home/nginx/logs:/var/log/nginx/:rw \
-v /home/nginx/ssl:/etc/nginx/ssl:rw \
-d nginx

五、测试

访问https://192.168.51.187

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值