Docker Nginx SSL证书认证启动教程[通俗易懂]

前言:linux环境下,ssl证书认证https,docker快速部署nginx1下载nginxdocker镜像准备:服务器首先要安装docker,docker安装教程网上很多域名、ssl证书dockerpullnginx:latest2新建文件夹mkdir-p/home/docker-nginx/conf.d/mkdir-p/home/docker-nginx/logmkdir-p/home/docker-nginx/3下载nginx.

大家好,又见面了,我是你们的朋友全栈君。

前言:linux环境下,ssl证书认证https,docker快速部署nginx

1 下载nginx docker镜像

准备:

服务器首先要安装docker,docker安装教程网上很多

域名、ssl证书

docker pull nginx:latest

2 新建文件夹

mkdir -p /home/docker-nginx/conf.d/ 
mkdir -p /home/docker-nginx/log
mkdir -p /home/docker-nginx/

3 下载nginx docker镜像

生成配置nginx.conf文件,文件路径/home/docker-nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    upstream appointment {
        # 请将xxx.xxx.xxx.xxx:8080
        server xxx.xxx.xxx.xxx:8080 weight=1 max_fails=2 fail_timeout=20; 
    }

    server {
        listen 80;
        server_name dayuxiaozhi.top;
        location / {
            root html;
            index index.html index.htm;
            proxy_pass http://appointment;
        }
    }

    include /etc/nginx/conf.d/*.conf;
}

注意:请将31行的xxx.xxx.xxx.xxx:8080,改成你的ip与端口

生成配置default.conf文件,文件路径为/home/docker-nginx/conf.d/default.conf

server {

    listen       80;
    server_name  localhost;

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

    location / {
        #root   /data/nginx/html;
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        #autoindex  on;
    #try_files $uri /index/index/page.html;
        #try_files $uri /index/map/page.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;
    }

    location ~ /images {
        default_type                            application/json;
        return 200  '{"code": "A000000", "message": "ok", "timestamp": "20180307184426", "data": {"isvip": "1", "monthProList": []}}';
    }

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

4 启动nginx docker容器

docker run --name nginx -p 80:80 -p 443:443 -v /home/docker-nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/docker-nginx/log:/var/log/nginx -v /home/docker-nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -d nginx
-p 80:80:将容器的 80 端口映射到主机的 80 端口。
-p 443:443:将容器的 80 端口映射到主机的 443 端口。
--name nginx:将容器命名为 nginx。
-v /home/docker-nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf:将服务器上的default.conf 挂载到容器的 /etc/nginx/default.conf
-v /home/docker-nginx/nginx.conf:/etc/nginx/nginx.conf:将服务器上的nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。
-v /home/docker-nginx/log:/var/log/nginx:将服务器上的 logs 挂载到容器的 /var/log/nginx。

5 进入nginx docker容器,新建文件夹/ssl

进入nginx docker前,请检查下容器是否启动,若没有启动,请检查上述步骤是否有缺漏。

docker ps | grep nginx

Docker Nginx SSL证书认证启动教程[通俗易懂]

docker exec -it nginx bash
mkdir /ssl
chmod 775 /ssl

6 证书复制到docker容器中

1_dayuxiaozhi.top_bundle.crt  2_dayuxiaozhi.top.key证书放到/home/docker-nginx,将crt、key证书文件复制到docker容器中

cd /home/docker-nginx
docker cp 2_dayuxiaozhi.top.key  nginx:/ssl/
docker cp 1_dayuxiaozhi.top_bundle.crt nginx:/ssl/

7 修改nginx.conf文件

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    upstream appointment {
        # 请将xxx.xxx.xxx.xxx:8080
        server xxx.xxx.xxx.xxx:8080 weight=1 max_fails=2 fail_timeout=20; 
    }

    server {
        listen 80;
        server_name dayuxiaozhi.top;
        location / {
            root html;
            index index.html index.htm;
            proxy_pass http://appointment;
        }
    }
   

    server {
        listen 443 ssl;
        #证书文件名称
        ssl_certificate /ssl/1_dayuxiaozhi.top_bundle.crt;
        #私钥文件名称
        ssl_certificate_key /ssl/2_dayuxiaozhi.top.key;
        ssl_session_timeout 5m;
        #请按照以下协议配置
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        server_name dayuxiaozhi.top;
        location / {
            root html;
            index index.html index.htm;
            proxy_pass http://appointment;
        }
    }

    include /etc/nginx/conf.d/*.conf;
}

注意:请将31行的xxx.xxx.xxx.xxx:8080,改成你的ip与端口

配置两个server,一个是80端口,另外一个是443端口

8 重启docker nginx

docker restart nginx

9 查看重启是否工程

docker ps | grep nginx

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/127970.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号