Linux下nginx添加stream模块支持Tcp转发

方式1 yum直接安装

  1. 安装nginx
yum install nginx
  1. 安装模块
yum install nginx-mod-stream -y
  1. 添加转发配置 /etc/nginx/nginx.conf底部添加
stream {
    server {
        listen 8666;
        proxy_connect_timeout 360s;
        proxy_timeout 360s;
        proxy_pass xxx.xxx.xxx.xxx:8666;
    }
}
  1. 运行测试
# 运行
nginx
# 重载配置文件
nginx -s reload

方式2 源码编译

一、安装编译工具
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
二、安装PCRE

PCRE (Perl Compatible Regular Expressions) 是一个用于处理正则表达式的库,它是一个C语言的库,可以在多种编程语言中使用。PCRE库提供了一套API来编译和执行正则表达式,并提供了一组函数来匹配、查找和替换文本中符合正则表达式的模式。PCRE库与Perl的正则表达式语法兼容,因此可以直接使用Perl的正则表达式语法。

如果我们在nginx.conf配置文件中使用了正则表达式,那么在编译Nginx时必须将PCRE库编译进Nginx。这是因为Nginx的HTTP模块需要依赖PCRE库来解析正则表达式。

  1. 进入下载目录
cd /usr/local/src/
  1. 下载
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
  1. 解压
tar zxvf pcre-8.35.tar.gz
  1. 安装
./configure
make && make install
三、编译运行nginx
  1. 下载源码
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.25.3.tar.gz
  1. 解压
tar zxvf nginx-1.25.3.tar.gz
  1. 进入源码目录
cd nginx-1.25.3
  1. 配置
./configure --prefix=/opt/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-pcre=/usr/local/src/pcre-8.35 --with-stream
  1. 编译安装
# 编译
make
# 安装
make install
  1. 配置文件 /opt/nginx/etc/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 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

	autoindex on;
	autoindex_exact_size off;
	autoindex_localtime on;

    include       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 /opt/nginx/conf/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /opt/nginx/conf/default.d/*.conf;

        location / {
		root   html;
            index  index.html index.htm;
        }

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

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
	
	include /opt/nginx/conf/vhosts/*.conf;
}

stream {
    server {
        listen 8666;
        proxy_connect_timeout 360s;
        proxy_timeout 360s;
        proxy_pass locktcp.cosinwx.com:8666;
    }
}

  1. 运行
# 运行
./opt/nginx/sbin/nginx
# 重载配置文件
./opt/nginx/sbin/nginx -s reload
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ArslanRobot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值