1、下载镜像
docker pull nginx
2、本机创建目录
1)创建nginx挂载目录
mkdir /usr/local/nginx
2)进入nginx目录
cd /usr/local/nginx
3)创建 www和logs目录
mkdir -p www logs
3、创建nginx容器
此容器用于复制配置文件,复制配置文件后需要删除
docker run -itd -p 80:80 --name mynginx 602
4、复制配置文件到本机
docker cp mynginx:/etc/nginx /usr/local/nginx
5、文件夹更名
把nginx目录下的nginx目录名修改为conf
mv nginx conf
5、删除容器
复制完配置后删除此容器,再重新创建新容器
docker stop mynginx
docker rm mynginx
6、创建容器
docker run -itd -p 80:80 --name nginx-musicbar -v /usr/local/nginx/conf:/etc/nginx -v /usr/local/nginx/www:/usr/share/nginx/html -v /usr/local/nginx/logs:/var/log/nginx --net network-musicbar --ip 172.18.0.5 602
7、关联tomcat容器
1)查看tomcat容器IP
docker inspect 实例ID | grep "IPAddress"
2)修改/usr/local/nginx/conf/config.d/default.conf
以下为我的项目中的代理配置,实际需要根据自己的项目要求配置各种资源的代理,
location /admin {
alias /usr/share/nginx/html;
index index.html index.html;
}
location /musicbar {
proxy_pass http://172.18.0.2:8080/musicbar;
proxy_cookie_path /musicbar /;
proxy_set_header Cookie $http_cookie;
proxy_set_header Host $host;
proxy_set_header Remote_Addr $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_redirect off;
}
location /images {
proxy_pass http://172.18.0.2:8080/musicbar;
proxy_cookie_path /musicbar /;
proxy_set_header Cookie $http_cookie;
proxy_set_header Host $host;
proxy_set_header Remote_Addr $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_redirect off;
}
location /uploads {
proxy_pass http://172.18.0.2:8080/musicbar;
proxy_cookie_path /musicbar /;
proxy_set_header Cookie $http_cookie;
proxy_set_header Host $host;
proxy_set_header Remote_Addr $remote_addr;
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://172.18.0.2:8080/musicbar/api;
proxy_cookie_path /musicbar /;
proxy_set_header Cookie $http_cookie;
proxy_set_header Host $host;
proxy_set_header Remote_Addr $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_redirect off;
}
location /{
proxy_pass http://172.18.0.2:8080/musicbar/client/;
proxy_cookie_path /musicbar /;
proxy_set_header Cookie $http_cookie;
proxy_set_header Host $host;
proxy_set_header Remote_Addr $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_redirect off;
}
3) 重启nginx容器
docker restart nginx-musicbar
文章来源于哔站《8、docker 安装 nginx》
更多学习视频和专栏文章请到哔站个人空间: 布道师学院的个人空间-布道师学院个人主页-哔哩哔哩视频
更多资源和项目下载请到:”开源吧(找实战项目和毕设项目的好网站)“ :开源吧