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

相关推荐
小豆子范德萨20 小时前
cursor连接远程window服务器的WSL-ubuntu
运维·服务器·ubuntu
夜月yeyue1 天前
Linux 调度类(sched_class)
linux·运维·c语言·单片机·性能优化
林义满1 天前
运维转型让产线 “少掉链”:上海义满汽车零部件借智能运维降本增效,年减损失超 200 万
运维·汽车
VekiSon1 天前
Linux系统编程——IPC进程间通信:信号通信与共享内存
linux·运维·服务器
南山nash1 天前
CentOs7 安装 Docker 详细步骤
linux·运维·docker·容器
ZHHHHHJ661 天前
LL层-PAST
运维·服务器·网络
杨浦老苏1 天前
轻量级私有云音乐服务器QM-Music
docker·群晖·音乐
咩咩大主教1 天前
VSCode远程连接Linux部署的Docker
linux·vscode·docker
fufu03111 天前
Linux环境下的C语言编程(四十六)
linux·运维·服务器
qq_418247881 天前
Linux上部署conda环境
linux·运维·神经网络·机器学习·conda