使用docker运行nginx,添加ssl证书

下载nginx镜像

docker pull nginx:alpine

申请ssl证书

可以在阿里云申请免费的ssl证书,进入阿里云控制台首页,点击ssl证书,进入ssl证书管理页面,点击购买证书,按照指引操作即可,申请完成后,将证书上传到/etc/nginx/cert目录下

nginx配置

在/etc/nginx目录下创建nginx配置文件
vim /etc/nginx/nginx.conf

user root;
worker_processes  1;
error_log  /var/log/nginx/error.log  info;
worker_rlimit_nofile 1024;
events {
    worker_connections  1024;
}
http {
    proxy_http_version 1.1;
    proxy_connect_timeout 600;
    proxy_read_timeout 600;
    proxy_send_timeout 600;
    client_max_body_size 50m;
    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  /var/log/nginx/access.log  main;
    keepalive_timeout  65;
    gzip on;
    sendfile on;
    gzip_buffers 16 8k;
    gzip_comp_level 2;
    gzip_http_version 1.1;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;
    gzip_types
        text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
        text/javascript application/javascript application/x-javascript
        text/x-json application/json application/x-web-app-manifest+json
        text/css text/plain text/x-component
        font/opentype font/ttf application/x-font-ttf application/vnd.ms-fontobject
        image/x-icon image/jpeg image/gif image/png;
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
    upstream demo {
        server server01:8080 weight=5 max_fails=5 fail_timeout=60s;
    }
    server {
        listen       80;
        server_name  new.dxnbr.com;
        return       301 https://$server_name$request_uri;
    }
    server {
        listen                    443 ssl;
        server_name               new.dxnbr.com;
        ssl_certificate           /etc/nginx/cert/new.dxnbr.com.pem;
        ssl_certificate_key       /etc/nginx/cert/new.dxnbr.com.key;
        ssl_ciphers               ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols             TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_session_timeout       5m;
        location ^~/demofile/ {
            proxy_pass http://server02/demofile/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
        }
        location ^~/api/{
            proxy_pass http://demo;
            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
            proxy_connect_timeout 1;
            proxy_buffer_size 64k;
            proxy_buffers   4 64k;
            proxy_busy_buffers_size 128k;
        }
        location / {
            root /root/react-demo/;
            index index.html;
        }
    }
}

启动nginx

docker run -d --name nginx \
# 挂载配置文件
-v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf \
# 挂载证书目录
-v /etc/nginx/cert:/etc/nginx/cert \
# 挂载日志目录
-v /var/log/nginx:/var/log/nginx \
-v /root/react-demo:/root/react-demo \
# 时间同步
-v /etc/localtime:/etc/localtime \
-p 80:80 \
-p 443:443 \
# 增加host映射
--add-host server01:172.26.245.47 \
--add-host server02:172.26.245.48 \
nginx:alpine

-v用来挂载目录:-v 宿主机目录:容器目录

Logo

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

更多推荐