通过docker 直接挂载证书的生成目录,启动nginx,失败,报错:无法找到挂载目录下的证书文件。

解决:新建挂载目录cert,把相关证书复制到该文件夹下,重新挂载,读取并启动成功。

docker 镜像log 位置:/var/lib/docker/containers/对应镜像启动后返回ID

1、通过certbot 申请免费的证书,例如域名为:www.goodstudy.com

certbot certonly --standalone -d www.goodstudy.com

如果申请成功,证书和私钥路径如下:

/etc/letsencrypt/live/www.goodstudy.com/fullchain.pem
/etc/letsencrypt/live/www.goodstudy.com/privkey.pem

 

2、nginx.conf 文件配置


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
         listen 80;
         listen 443 ssl http2;
##如果在linux中直接安装nginx,是可以直接写全路径的
#         ssl_certificate /etc/letsencrypt/live/www.goodstudy.top/fullchain.pem;
#         ssl_certificate_key /etc/letsencrypt/live/www.goodstudy.top/privkey.pem;
## 证书挂载到docker中的路径 
         ssl_certificate /etc/nginx/cert/fullchain.pem; 
         ssl_certificate_key /etc/nginx/cert/privkey.pem;

         ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
         ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
         ssl_prefer_server_ciphers on;
          ssl_session_timeout 10m;
          ssl_session_cache builtin:1000 shared:SSL:10m;
  ssl_buffer_size 1400;
  add_header Strict-Transport-Security max-age=15768000;
  ssl_stapling on;
  ssl_stapling_verify on;
  server_name www.goodstudy.top;
  root   /www;
  index index.html index.htm index.php;

  if ($ssl_protocol = "") { return 301 https://$host$request_uri; }

 #  include /usr/local/nginx/conf/rewrite/other.conf;


  #error_page 404 /404.html;
  #error_page 502 /502.html;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /v2ray {
             proxy_pass       http://127.0.0.1:6666;
             proxy_redirect             off;
             proxy_http_version         1.1;
             proxy_set_header Upgrade   $http_upgrade;
             proxy_set_header Connection "upgrade";
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

3、创建挂载目录,cert为证书挂载目录

mkdir -p /root/dev/docker/nginx/{conf,conf.d,www,logs,cert}

复制证书到cert文件夹,如:

 

4、启动 挂载目录

docker run -d \
-p 80:80 -p 443:443 \
--name nginx2 \
-v /root/dev/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /root/dev/docker/nginx/conf.d:/etc/nginx/conf.d \
-v /root/dev/docker/nginx/logs:/var/log/nginx \
-v /root/dev/docker/nginx/www:/www \
-v /root/dev/docker/nginx/cert:/etc/nginx/cert \
nginx

挂载启动成功

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐