Nginx配置TCP请求转发

背景

有时候内网的服务器需要把服务提供给外网访问,但是这个内网的服务器没有公网ip,所以可以在一台有公网ip的nginx服务器配置TCP请求转发,把内网服务的端口映射出来到公网

Nginx配置TCP请求转发

Nginx配置TCP转发

1.编译安装 stream 组件

如果你的nginx为源码编译,需要增加一下编译参数

./configure --with-stream

如果你的nginx为yum直接安装的,需要检查相关编译参数是否含有–with-stream

如下的 –with-stream=dynamic

/usr/sbin/nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1g FIPS  21 Apr 2020 (running with OpenSSL 1.1.1k  FIPS 25 Mar 2021)
TLS SNI support enabled
configure arguments:
--prefix=/usr/share/nginx
--sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib64/nginx/modules
--conf-path=/etc/nginx/nginx.conf
...

--with-stream=dynamic
...

2.配置TCP转发

TCP转发主配置文件

添加与http同级配置
如下的 TCP请求转发
include /etc/nginx/tcp.d/*.conf;

cat /etc/nginx/nginx.conf
For more information on configuration, see:
  * Official English Documentation: http://nginx.org/en/docs/
  * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 51024;
}

#TCP请求转发
include /etc/nginx/tcp.d/*.conf;

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    #log_format gitlab_access '$remote_addr - $remote_user [$time_local] "$request_method $filtered_request_uri $server_protocol" $status $body_bytes_sent "$filtered_http_referer" "$http_user_agent" $gzip_ratio';
    #log_format gitlab_mattermost_access '$remote_addr - $remote_user [$time_local] "$request_method $filtered_request_uri $server_protocol" $status $body_bytes_sent "$filtered_http_referer" "$http_user_agent" $gzip_ratio';
    access_log  /var/log/nginx/access.log  main;
    map $http_upgrade $connection_upgrade {
      default upgrade;
      ''      close;
    }

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    server_tokens   off;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.

    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/upstreams/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
    return 404; #不存在的域名返回值
    #rewrite ^.*$ http://www.baidu.com/ last;

        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.

        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

创建存放配置目录

mkdir -p /etc/nginc/tcp.d/

TCP转发子配置文件

cat /etc/nginx/tcp.d/stream.conf
#麦穗
stream {
    # 添加socket转发的代理
    upstream socket_proxy {
        hash $remote_addr consistent;
        # 转发的目的地址和端口
        server 10.40.0.103:5050 weight=5 max_fails=3 fail_timeout=30s;
    }

    # 提供转发的服务,即访问localhost:5050,会跳转至代理socket_proxy指定的转发地址
    server {
       listen 5050;
       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_pass socket_proxy;
    }
}

Original: https://www.cnblogs.com/linuxshare/p/16590807.html
Author: 爱折腾的大臭臭
Title: Nginx配置TCP请求转发

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/573852/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • ansible-复制模块

    简介:临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。 1、复制模块 可在终端执行ansible-doc copy…

    Linux 2023年6月6日
    095
  • 【Jmeter】jmeter提取response中的返回值,并保存到本地文件–BeanShell后置处理器

    有个需求,需要在压测环境中,创建几十万的账号数据,然后再根据创建结果,查询到某些账号信息。 按照之前我的做法,直接Python调用API,然后再数据库查询; 但是近期所有开发人员的…

    Linux 2023年5月28日
    071
  • Redis info参数总结(转)

    可以看到,info的输出结果是分几块的,有Servers、Clients、Memory等等,通过info后面接这些参数,可以指定输出某一块数据。 我刚开始在Gentoo上装的默认版…

    Linux 2023年5月28日
    0112
  • burp通过条件竞争上传文件

    一·何为条件竞争现代框架更能抵御此类攻击。他们通常不会将文件直接上传到文件系统上的预期目的地。相反,他们采取了预防措施,例如首先上传到临时的沙盒目录并随机命名以避免覆盖现有文件。然…

    Linux 2023年6月7日
    079
  • docker redis启动将配置文件挂载在数据卷(volume)中

    关键词 [     "linux",     "docker",     "volume",     "red…

    Linux 2023年5月28日
    074
  • PowerShell Automation : Introduction To Kasini3000

    tags: Ansible , pipeline , winrm , SSH , psremoting , automation , devops site mirror: htt…

    Linux 2023年6月14日
    095
  • c++模板类的使用,编译的问题

    前两天在写代码时,把模板类的声明和分开放在两个文件中了,类似于下面这样: stack.hpp: #ifndef _STACK_HPP #define _STACK_HPP temp…

    Linux 2023年6月14日
    089
  • 什么?Android上面跑Linux?

    前言 众所周知,现在程序员因为工作、个人兴趣等对各种系统的需求越来越大,部分人电脑做的还是双系统。其中,比较常见的有各种模拟器、虚拟机在windows上面跑Android、Linu…

    Linux 2023年5月27日
    081
  • QML 信号与响应方法的总结

    如果面试过程中,面试官想了解你对 Qt 的理解有多少,少不了会涉及到信号槽这一块,毕竟这是 Qt 最经典的一项技术。 刚开笔,我可能有点狂妄了。 信号槽,分为两部分,信号和对信号响…

    Linux 2023年6月6日
    095
  • 博客园排名预测

    前言 之前写过一篇绘制博客园积分与排名趋势图的文章——《查看博客园积分与排名趋势图的工具》,使用那篇文章介绍的工具,可以通过趋势图直观的看出排名前进的走势。但是如果想看看自己积分达…

    Linux 2023年6月6日
    088
  • RHCSA阶段笔记

    命令终端字段含义介绍 [root@localhost ~]# 解释: root:当前登录系统用户名(root超级管理员) localhost :当前主机名 :当前用户所在目录( 为…

    Linux 2023年5月27日
    078
  • 本篇还玩“障眼法”,一文解读HTML内联框架Iframes。

    写在开篇 假设有一个需求,想要在网页内显示其它网页,怎么搞?很简单,可以用iframe来解决,那啥是iframe?本篇的主题就是它,接下来我们一一解剖它的用法。 嵌入第三方url页…

    Linux 2023年6月7日
    0101
  • Java — 枚举

    枚举是 JDK5 中引入的特性,由 enum 关键字来定义一个枚举类。 格式: enum 枚举类名 { 枚举项1, 枚举项2, …; 成员变量; 构造方法 成员方法 } 说明:…

    Linux 2023年6月8日
    075
  • Ceph 块存储 创建的image 映射成块设备

    将创建的volume1映射成块设备 [root@mysql-server ceph]# rbd map rbd_pool/volume1 rbd: sysfs write fail…

    Linux 2023年6月13日
    090
  • jmeter压测 报connect reset和unexpected end of file from server的解决方案

    今天做负载压测,500并发没有报错,500并发以上就报错,报错率很高,主要有两种错误: 报错1: 报错2: 解决方案: 1.压测机的注册表中添加 DefaultReceiveWin…

    Linux 2023年6月8日
    078
  • ThinkPHP5权限管理

    自己写的权限管理,大致思路:用户登陆成功之后,查出该用户的权限列表,并把权限列表存到session中,进入系统后,再判断该模块是否在session中,如果存在就说明有该权限,就显示…

    Linux 2023年6月7日
    083
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球