docker使用nginx实现ssl(https)反向代理其他容器应用
				
									
					
					
						|  | 
							freeflydom 2025年8月7日 10:57
								本文热度 941 | 
					
				 
				安装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;
    
    keepalive_timeout  65;
    
    include /etc/nginx/conf.d/*.conf;
	
    server {
	    listen       443 ssl;
	    
	    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;  
	    }
    } 
	
}
重载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
转自https://www.cnblogs.com/jun-zi/p/12189434.html
该文章在 2025/8/7 10:57:22 编辑过