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

你可能感兴趣的:(软件开发,linux,nginx,tcp/ip)