用docker创建nginx反向代理tcp流量

时间:2024-04-02 20:48:30

有这样一个需求,需要反向代理一个tcp连接,我打算用nginx来做,比较简单的实现掉

./conf/nginx.conf 配置文件

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

stream {
	proxy_timeout 9999m;

	server {
		listen 9999;
		proxy_pass black.anarckk.me:9999;
	}
}

docker-compose.yaml

version: "3"

services:
  ng:
    image: nginx:1.25.4 
    container_name: ng
    ports:
      - 9999:9999
    restart: unless-stopped
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf
    networks:
      custom-bridge:
    environment:
      - TZ=Asia/Shanghai
networks:
  custom-bridge:
    external: true

然后运行 docker-compose up -d ,这样就把 black.anarckk.me:9999 反向代理到本地的 localhost:9999