docker使用nginx实现ssl(https)反向代理其他容器应用

安装nginx容器

搜索nginx镜像

docker search nginx

拉取最新版nginx

docker pull nginx:latest

运行容器

docker run --name=nginx -p 443:443 -v /nginx/conf.d:/etc/nginx/conf.d -d nginx

--name=nginx: 容器名称。
-p 443:443: 端口进行映射,将本地 443 端口映射到容器内部的 443 端口。
-d nginx: 设置容器在在后台一直运行。

容器内安装sz、rz

apt-get update && apt-get install lrzsz

docker容器中安装vi命令

apt-get update
apt-get install vim

进入容器,上传证书到容器

上传根据域名生成的证书,比如 fullchain1.pem(公钥) privkey1.pem(密钥)

监听443端口,通过nginx代理应用网站


/etc/nginx/nginx.conf中加入如下配置:

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;
#server是新增的配置
server {
listen 443 ssl;
#server_name svr.com.cn
ssl_certificate fullchain1.pem; #公钥,证书
ssl_certificate_key privkey1.pem; #密钥

location / {
proxy_set_header X-FORWARDED-FOR $remote_addr;
proxy_set_header X-FORWARDED-PROTO $scheme;
proxy_set_header Host $http_host;
proxy_pass http://192.168.xxx.xxx:80; #代理的应用 宿主机IP:容器映射到宿主机的端口
}

}

}

重载nginx服务

service nginx reload

将域名svr.com.cn映射到nginx所在的宿主机IP(配置DNS映射,或者改hosts文件)

通过域名访问应用

https://svr.com.cn

查看nginx代理应用(svr.com.cn)访问日志(应用容器id为 09a1c)

docker logs -f 09a1c

posted @ 2020-01-13 21:21  军子~  阅读(3780)  评论(0)    收藏  举报