docker安装nginx
镜像地址: https://hub.docker.com/_/nginx
配置文件挂载
# 复制基础配置文件.单个文件挂载注意事项如下:
# 单独挂载文件,通过vim并不会改变inode,挂载目录,或者赋予权限(chmod 777)
mkdir -p $DOCKER_PATH/nginx/conf \
&& docker run --rm nginx:1.21.4 cat /etc/nginx/nginx.conf > $DOCKER_PATH/nginx/conf/nginx.conf \
&& chmod 777 $DOCKER_PATH/nginx/conf/nginx.conf
nginx启动
# 我采用docker-compose.yml方式启动的,其它类似,暴露端口,磁盘挂载.
version: "3.7"
services:
nginx:
image: nginx:1.21.4
container_name: nginx
volumes:
- "$DOCKER_PATH/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
- "$DOCKER_PATH/nginx/conf/conf.d/:/etc/nginx/conf.d/"
ports:
- 80:80
nginx重启
# 和本地一致,更改配置文件后检查重启,因为docker安装所以需要使用docker exec [容器名称] [具体指令]
docker exec nginx nginx -t
docker exec nginx nginx -s reload
常用配置
1. 发起请求的服务器配置域名映射.(或直接为公网域名.)
linux vim /etc/hosts
win notepad C:\Windows\System32\drivers\etc\hosts
2. 添加新的配置文件.
vim $DOCKER_PATH/nginx/conf/conf.d/easy.conf
server
{
listen 80;
server_name easy.com;
charset utf-8;
location /httpbin {
proxy_pass http://httpbin.org/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# 其它热备,轮询,加权轮询,绑定ip等参考.https://www.runoob.com/w3cnote/nginx-proxy-balancing.html
测试: curl http://easy.com/httpbin/get
遇到的问题
1. proxy_pass配置问题:
nginx日志报错: connect() failed (111: Connection refused) while connecting to upstream
因为采用docker安装.nginx服务节点和宿主机不是同一个.
配置为localhost或者127.0.0.1导致服务无法转发
配置为easy.local(宿主机内网域名)能正常生效.
2. proxy_pass配置问题:
能转发,样式丢失或跳转页面错误.
配置了路径服务跳转转发过程中grafana丢失.http://easy.com/grafana --> http://easy.com/login
转发对应服务grafana/mongoExpress/metabase等.
部分服务访问存在 subPath/contextPath问题.
错误示例:
location /grafana {
proxy_pass http://easy.local:3000;
}
正确示例:
location /{
proxy_pass http://easy.local:3000;
}
或启用服务的subPath
示例:grafana配置subPath.
https://blog.csdn.net/weixin_34306676/article/details/91493964