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端口)

相关推荐
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
还是大剑师兰特1 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
分布式存储与RustFS1 天前
MinIO 官方 Docker 镜像被移除:依赖它的项目该怎么办
docker·云原生·devops·对象存储·minio·分布式存储
AI职业加油站1 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
此冬歌咏1 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
玉&心1 天前
通过Arthas在线诊断K8S中的内存及JVM等使用情况
docker·k8s·arthas
xing-xing1 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
-梅1 天前
linux(8) 软硬链接
linux·运维·服务器
guo_wen_qiang1 天前
上传本地镜像到harbor中
docker·容器·持续部署
张洛闻Eren1 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github