nginx 添加stream模塊支持tcp、sftp代理

0 前言

nginx從1.9.0開始,新增加了一個stream模塊,用來實現四層協議的轉發、代理或者負載均衡等。

1 備份原有配置

先到原nginx目錄下備份nginx.conf 相關文件,如果是引用的外部配置文件,則忽略這一步

2 複製nginx正在使用的模塊

進入nginx下sbin目錄,執行 ./nginx -V 查看當前nginx安裝信息,configure arguments後是當前nginx正在使用的模塊,複製這個值

cd /usr/local/nginx/sbin/
./nginx -V

在這裏插入圖片描述

3 執行./configure,追加 --with-stream

重新下載nginx安裝包,放到local下,解壓,進入到解壓後的nginx-1.17.1目錄

進入nginx-1.17.1根目錄,檢查安裝環境是否有問題,有問題的話會報錯。./configure命令後黏貼上面複製的內容,後面追加 --with-stream

tar -zxvf nginx-1.17.1.tar.gz #解壓nginx安裝包
cd /nginx-1.17.1
./configure --prefix=/home/deployer/soft/nginx_backward_proxy --with-stream --with-http_ssl_module

常用configure命令設定安裝參數

# 常用模塊配置
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-stream  \
--with-http_ssl_module

4 編譯make & make install,驗證是否安裝成功

make  #編譯需要一兩分鐘的樣子
make install #安裝很快

重新查看nginx版本信息,可以看到stream模塊已經安裝成功

在這裏插入圖片描述

5 配置steam反向代理tcp

重新在nginx.conf文件上關聯相關的配置文件,以及配置tcp socket sftp相關的配置,注意,stream和http同級,在最外層。

stream {
    upstream sftp{
        hash $remote_addr consistent;
        server 172.25.32.16:22;
    }
    
    server {
        listen 10022;
        proxy_connect_timeout 300s;
        proxy_timeout 300s;
        proxy_pass sftp;
   }
}
worker_processes auto;
error_log logs/error.stream.log info;
events {
    worker_connections  1024;
}
stream {
    upstream backend {
        hash $remote_addr consistent;
        server 127.0.0.1:12346 weight=5;
        server 127.0.0.1:12347            max_fails=3 fail_timeout=30s;
        server 127.0.0.1:12348            max_fails=3 fail_timeout=30s;
    }
    upstream dns {
       server 17.61.29.79:53;
       server 17.61.29.80:53;
       server 17.61.29.81:53;
       server 17.61.29.82:53;
    }
    server {
        listen 12345;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
    }
    server {
        listen 127.0.0.1:53 udp;
        proxy_responses 1;
        proxy_timeout 20s;
        proxy_pass dns;
    }
}

stream core 一些變量
注意:變量支持是從 nginx 1.11.2版本開始的

$binary_remote_addr
二進制格式的客戶端地址
$bytes_received
從客戶端接收到的字節數
$bytes_sent
發往客戶端的字節數
$hostname
連接域名
$msec
毫秒精度的當前時間
$nginx_version
nginx 版本
$pid
worker進程號
$protocol
通信協議(UDP or TCP)
$remote_addr
客戶端ip
$remote_port
客戶端端口
$server_addr
接受連接的服務器ip,計算此變量需要一次系統調用。所以避免系統調用,在listen指令裏必須指定具體的服務器地址並且使用參數bind。
$server_port
接受連接的服務器端口
$session_time
毫秒精度的會話時間(版本1.11.4開始)
$status
會話狀態(版本1.11.4開始), 可以是一下幾個值:
200
成功
400
不能正常解析客戶端數據
403
禁止訪問
500
服務器內部錯誤
502
網關錯誤,比如上游服務器無法連接
503
服務不可用,比如由於限制連接等措施導致
$time_iso8601
ISO 8601時間格式
$time_local
普通日誌格式的時間戳

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章