docker部署nginx配置阿里云证书实现https访问

1、申请阿里云免费证书


image

image

image

2、nginx.conf配置


2.1、nginx宿主机目录结构

image

注意:
conf:配置文件及证书所在位置
conf.d:https配置信息
html:静态文件所造目录
logs:nginx日志信息

2.2、将下载下来的证书文件放入到宿主机conf/目录下

image

2.3、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;

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

2.4、conf.d/default.conf内容

image

server {
        listen  80;
        listen  [::]:80;
        charset 'utf-8';
        server_name  域名;
        rewrite ^(.*) https://$server_name$1 permanent;
        
        # 404页面跳转
        error_page  404 /404.html; 
        location = /404.html{
            root  /usr/share/nginx/html;
        }
}

server {
    listen  443 ssl;
    server_name 域名;

    # 注意证书文件名字和位置,是从/etc/nginx/下开始算起的
    ssl_certificate /etc/nginx/xxx.pem;
    ssl_certificate_key /etc/nginx/xxx.key;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

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

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        proxy_set_header   X-Real-IP         $remote_addr;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    # 404页面跳转
    error_page  404 /404.html;             
    location = /404.html {
        root  /usr/share/nginx/html;
    }
    
    # 500页面跳转
    # 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;
    #}
}

3、启动nginx


docker run -d --restart=always 
-p 80:80 -p 443:443 
--name nginx80 
-v /home/achen/nginx/conf/nginx.conf:/etc/nginx/nginx.conf 
-v /home/achen/nginx/html/dist/:/usr/share/nginx/html 
-v /home/achen/nginx/conf/:/etc/nginx 
-v /home/achen/nginx/conf.d:/etc/nginx/conf.d 
-v /home/achen/nginx/logs:/var/log/nginx 
nginx:1.19.10

4、访问页面即可


https://域名

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值