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

这篇具有很好参考价值的文章主要介绍了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库来解析正则表达式。文章来源地址https://www.toymoban.com/news/detail-860951.html

  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

到了这里,关于Linux下nginx添加stream模块支持Tcp转发的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • nginx 配置支持websocket转发

    编辑nginx.conf,在http区域内一定要添加下面配置: http {     #自定义变量 $connection_upgrade     map $http_upgrade $connection_upgrade {         default          keep-alive;  #默认为keep-alive 可以支持 一般http请求         \\\'websocket\\\'      upgrade;     #如果为websocket 则为 upgra

    2024年02月06日
    浏览(33)
  • nginx的TCP/UDP转发

    一、nginx的TCP/UDP转发   一)stream模块 1.9.0版之后nginx开始具有TCP/UDP的代理转发功能, 但需要手动安装stream模块 用nginx -V命令(注意V是大写, 可查询nginx已安装模块)查了一下, 我的nginx是1.16.0版本, 并且已经安装了stream模块(命令输出包含--with-stream)   二)配置 1、概述 TCP转发的配

    2024年02月10日
    浏览(28)
  • nginx模块stream配置

    一、stream模块概要。 stream模块一般用于tcp/UDP数据流的代理和负载均衡,可以通过stream模块代理转发TCP消息。 ngx_stream_core_module模块由1.9.0版提供。 默认情况下,没有构建此模块。 -必须使用-with stream配置参数启用。 也就是说,必须在使用./configure --with-stream编译时添加流模块

    2024年02月16日
    浏览(21)
  • nginx配置stream模块

    使用背景:使用stream模块转发应用服务器sftp连接请求。 1.解压nginx压缩包 tar -zxvf nginx-1.16.1.tar.gz 2.指定配置项,--prefix表示安装路径,--with-stream表示添加流模块 ./configure --prefix=/usr/local/nginx-my-stream/nginx1.16 --with-stream --with-http_stub_status_module --with-http_ssl_module 3.编译 make 4.安装

    2024年02月16日
    浏览(33)
  • Nginx使用stream模块分流实现端口复用

    使用Nginx复用端口有很多方法,最普遍的方法是在不同的server块中监听同一端口,根据不同的主机名完成分流。本文介绍了一种较新的端口复用方法,它可以方便地对TLS加密的TCP数据进行分流。 1 Nginx stream分流 Nginx一般都工作在应用层,可以通过多个虚拟主机对端口的监听实

    2024年02月12日
    浏览(27)
  • nginx 把所有请求转发到另一个端口, 并添加header头,怎么配置

    要将nginx中的所有请求转发到另一个端口,可以使用nginx的 proxy_pass 指令来实现。以下是配置文件示例: 在上述配置中,我们创建了一个服务器块,并监听80端口,这是常见的HTTP请求端口。 server_name 指令用于指定该服务器块适用的域名,你需要将其替换为你的域名。 location

    2024年02月12日
    浏览(24)
  • nginx中根据请求参数的不同将请求转发到不同的服务(map模块的使用)

    需求 :有一个文本翻译的接口,需要根据原语、目标语、以及apikey的不同转发到不同的服务; 实现 :可以使用Nginx的map模块来实现基于请求参数的转发。具体实现步骤如下: 在Nginx配置文件中定义一个map块,用于根基请求参数判断对应的转发地址, $arg_apikey 是获取请求参数

    2024年02月16日
    浏览(38)
  • Nginx重新编译并添加模块

            作用:一是检查所需模块是否已安装,二是将configure arguments: 后面的参数复制出来并保存,因为等会重新编译时还需将这些模块一同添加进去。         进入Nginx源码包目录下,执行make clean指令,清除历史编译。         使用./configure --help指令查询需要配置的参

    2024年02月05日
    浏览(26)
  • nginx添加nginx-sticky-module模块步骤

    ip_hash 根据客户端ip将请求分配到不同的服务器上. sticky 根据服务器个客户端的cookie,客户端再次请求是会带上此cookie,nginx会把有次cookie的请求转发到颁发cookie的服务器上. 1. 下载sticky 2. 编译nginx 3. 查看模块是否被载入 如下图表表示添加成功 4. 使用 name: cookie的名称 expire: 有效

    2024年02月13日
    浏览(23)
  • linux配置nginx websocket ws转发,绝对好用

    1:http下面加入 2:http下面 server 加入 我这里配置的 拦截websocket 转发到本地的ws地址8066端口,根据自己的服务器配置

    2024年02月12日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包