安裝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