nginx配置端口转发(docker-compose方式、包括TCP转发和http转发)

http端口转发

docker-compose.yml文件(示例转发geoserver服务)

复制代码
services:
  nginx:
    image: nginx:latest
    container_name: nginx-proxy-geoserver
    ports:
      - "18080:18080"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    restart: unless-stopped

nginx.conf文件

复制代码
events {
    worker_connections 1024;
}

http {
    server {
        listen 18080;

        location /geoserver/ {
            proxy_pass http://目的ip:目的端口/geoserver/;

            proxy_set_header Host 目的ip:目的端口;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_redirect http://目的ip:目的端口/ http://$host:目的端口/;
        }
    }

以上配置达到的目的是将目的ip的目的端口映射到本机的18080端口,达到本机18080端口转发到目的ip的目的端口。(监控的是本机的18080端口)

TCP端口转发(例如mysql 、postgres)

docker-compose.yml文件

复制代码
services:
  nginx:
    image: nginx:latest
    container_name: nginx-proxy-pg
    ports:
      - "15432:15432"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    restart: unless-stopped

nginx.conf文件

复制代码
events {
    worker_connections 1024;
}

stream {
    server {
        listen 15432;
        proxy_pass 目的ip:目的端口;
        proxy_timeout 30s;
        proxy_connect_timeout 10s;
    }
}

以上配置达到的目的是将目的ip的目的端口映射到本机的15432端口,达到本机15432端口转发到目的ip的目的端口。(监控的是本机的15432端口)

相关推荐
小p1 天前
docker学习: 1. docker基本使用
docker
蝎子莱莱爱打怪2 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
崔小汤呀2 天前
Docker部署Nacos
docker·容器
缓解AI焦虑2 天前
Docker + K8s 部署大模型推理服务:资源划分与多实例调度
docker·容器
何中应2 天前
Nginx转发请求错误
前端·后端·nginx
1candobetter2 天前
Docker Compose Build 与 Up 的区别:什么时候必须重建镜像
docker·容器·eureka
DianSan_ERP2 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
シ風箏2 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
呉師傅2 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑