1、运行容器
docker run --name nginx -d nginx
2、在宿主机的当前用户文件夹下创建nginx的目录结构
mkdir -p ~/nginx/{html,logs,conf,conf.d,cert}
3、把容器中的Nginx目录拷贝到宿主机的当前用户文件夹下
docker cp nginx:/etc/nginx/conf.d/default.conf ~/nginx/conf.d/default.conf
docker cp nginx:/etc/nginx/nginx.conf ~/nginx/conf/nginx.conf
docker cp nginx:/usr/share/nginx/html ~/nginx/html
4、此时,宿主机中有了nginx容器的配置,可以停止容器,并删除
docker stop nginx
docker rm nginx
5、现在,我们可以重新创建一个新的nginx容器,并且把宿主机刚刚复制出的文件挂载到容器上
docker run --name=nginx \
-p 80:80 \
-p 443:443 \
-v ~/nginx/conf.d:/etc/nginx/conf.d \
-v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v ~/nginx/html:/usr/share/nginx/html \
-v ~/nginx/logs:/var/log/nginx \
-v ~/nginx/cert:/etc/nginx/cert \
--privileged=true \
--restart=always \
-d \
nginx
6、平滑重启Nginx
docker exec nginx nginx -s reload
以下为Nginx的一个简单反向代理配置
server {
listen 80;
server_name www.域名A.com bbs.域名B.com;
access_log /var/log/nginx/www.域名A.com.access.log;
error_log /var/log/nginx/www.域名A.com.error.log;
location / {
proxy_pass http://IP地址:端口;
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_set_header X-Forwarded-Proto $scheme;
}
}