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

相关推荐
Shanxun Liao17 小时前
CentOS 7.9 根分区 / 已经 100% 用满隐藏占用解决办法
linux·运维·centos
FOREVER-Q17 小时前
Windows 下通过 SSH 替代 Gitee OAuth Token 推送配置指南
运维·服务器
盛夏52018 小时前
Docker容器化部署SpringBoot+Vue项目:从零到一在阿里云宝塔面板的实践指南
阿里云·docker·云计算
Cyber4K18 小时前
【Kubernetes专项】DockerFile、数据持计划、网络模式及资源配额
运维·网络·云原生·容器·kubernetes
星光不问赶路人18 小时前
Nginx 的 location 路径匹配语法详解
nginx·api
GDAL18 小时前
深入理解 NJS 全局对象:掌控运行时的核心工具
nginx·njs
ba_pi18 小时前
每天写点什么2026-01-09-linux基础
linux·运维·服务器
少云清18 小时前
【性能测试】3_性能测试基础 _指标
运维·服务器·数据库·性能测试·性能测试指标
广州服务器托管18 小时前
比较优秀的视频音频播放器PotPlayer64-v1.7.22764绿色版
运维·windows·计算机网络·电脑·音视频·可信计算技术
行走的bug...19 小时前
cmake总结
linux·运维·服务器