编译安装 Nginx 1.20.2,并增加 Sticky 和 nginx_upstream_check_module 模块

nginx_upstream_check_module 模块的作用主要是为 Nginx 的 upstream(上游服务器) 提供主动健康检查 和一些负载均衡增强功能。

1、具体来说,它的功能和作用如下:
主动健康检查(Active Health Check)
默认 Nginx 的 upstream 只能被动发现后端宕机(通过连接失败或响应超时)。
安装了 nginx_upstream_check_module 后,可以:
定期向后端服务器发送 HTTP 请求或 TCP 探针。
判断后端服务器是否可用。
动将不可用的服务器标记为 down,从负载均衡池中剔除。

2、支持多种类型:
type=http → 用于 HTTP 服务。
type=https → 用于 HTTPS 服务。
type=tcp → 用于 TCP 服务(非 HTTP)。

interval:检查间隔,例如 `interval=5000` 表示每 5 秒检查一次。
rise:连续成功多少次后,判定节点恢复正常。
fall:连续失败多少次后,判定节点不可用。
timeout:检查请求超时时间(毫秒)。

3、可自定义 HTTP 请求报文。
判断响应状态码是否正常,例如 2xx 或 3xx。

4、设置可用更新源

wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
wget -4 --no-check-certificate -O /etc/yum.repos.d/CentOS-Base.repo https://www.zhangfangzhou.cn/third/Centos-7.repo

5、下载源码

### Nginx 源码
wget https://nginx.org/download/nginx-1.20.2.tar.gz
tar -zxvf nginx-1.20.2.tar.gz

### zlib(可选,用于数据压缩)
wget http://www.zlib.net/zlib-1.2.13.tar.gz
tar -zxf zlib-1.2.13.tar.gz

### PCRE(支持正则表达式匹配)
wget --no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-8.45.tar.gz
tar -zxf pcre-8.45.tar.gz

### OpenSSL(支持 HTTPS)
wget https://www.openssl.org/source/openssl-1.1.1t.tar.gz
tar -zxf openssl-1.1.1t.tar.gz
mv openssl-1.1.1t openssl

### Sticky 模块
wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip -O sticky.zip
unzip sticky.zip
mv nginx-goodies-nginx-sticky* nginx-sticky

### nginx_upstream_check_module
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip -O nginx_upstream_check_module-master.zip
unzip nginx_upstream_check_module-master.zip

6、编译安装

yum -y install libtool  
#zlib 是提供数据压缩之用的库 (非必要编译安装)
cd ~
cd zlib-1.2.13
./configure --prefix=/usr/local/zlib
make && make install
echo "/usr/local/zlib/lib" > /etc/ld.so.conf.d/zlib.conf
ldconfig

#pcre PCRE库是一组函数,它们使用与Perl 5相同的语法和语义实现正则表达式模式匹配.(非必要编译安装)
cd ~
sudo yum install gcc-c++ -y
tar -zxf pcre-8.45.tar.gz
cd pcre-8.45
./configure --prefix=/usr/local/pcre --enable-utf8
make && make install
~/pcre-8.45/libtool --finish  /usr/local/pcre/lib/
echo "/usr/local/pcre/lib/" > /etc/ld.so.conf.d/pcre.conf
ldconfig

## 安装 Patch 工具(为 nginx_upstream_check_module 打补丁)
yum install -y patch

ngstable=1.20.2
#Install Nginx
cd ~
yum -y install gzip man
tar -zxf nginx-${ngstable}.tar.gz
#
#Custom nginx name
sed -i 's@^#define NGINX_VER          "nginx/" NGINX_VERSION@#define NGINX_VER          "Microsoft-IIS/10.0/" NGINX_VERSION@g'  ~/nginx-${ngstable}/src/core/nginx.h
sed -i 's@^#define NGINX_VAR          "NGINX"@#define NGINX_VAR          "Microsoft-IIS"@g'  ~/nginx-${ngstable}/src/core/nginx.h
sed -i '30,40s@nginx@Microsoft-IIS@g'  ~/nginx-${ngstable}/src/http/ngx_http_special_response.c
sed -i '45,50s@nginx@Microsoft-IIS@g' ~/nginx-${ngstable}/src/http/ngx_http_header_filter_module.c
#
#Nginx shows the file name length of a static directory file
sed -i 's/^#define NGX_HTTP_AUTOINDEX_PREALLOCATE  50/#define NGX_HTTP_AUTOINDEX_PREALLOCATE  150/'  ~/nginx-${ngstable}/src/http/modules/ngx_http_autoindex_module.c
sed -i 's/^#define NGX_HTTP_AUTOINDEX_NAME_LEN     50/#define NGX_HTTP_AUTOINDEX_NAME_LEN     150/'  ~/nginx-${ngstable}/src/http/modules/ngx_http_autoindex_module.c
#
yum install -y patch
cd ~/nginx-${ngstable}
patch -p1 < ../nginx_upstream_check_module-master/check_1.20.1+.patch
# patch -p1 < ../nginx_upstream_check_module-master/check_1.20.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h

#Copy NGINX manual page to /usr/share/man/man8:
cp -f ~/nginx-${ngstable}/man/nginx.8 /usr/share/man/man8
gzip /usr/share/man/man8/nginx.8

cd ~/nginx-${ngstable}
./configure --prefix=/usr/local/nginx --user=www --group=www \
--build=CentOS \
--modules-path=/usr/local/nginx/modules \
--with-openssl=/root/openssl \
--with-pcre=/root/pcre-8.45 \
--with-zlib=/root/zlib-1.2.13 \
--add-module=/root/nginx-sticky \
--add-module=/root/nginx_upstream_check_module-master \
--with-http_stub_status_module \
--with-http_secure_link_module \
--with-threads \
--with-file-aio \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_sub_module \
--with-http_dav_module \
--with-stream \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module
make -j$(nproc)
make install

7、平滑升级nginx

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

#然后拷贝一份新编译的二进制文件:
cp ~/nginx-${ngstable}/objs/nginx /usr/local/nginx/sbin/

#检测配置
nginx -t
kill -USR2 `cat /var/run/nginx.pid`
kill -HUP `cat /var/run/nginx.pid`

8、修改配置

upstream oah {
sticky; # 会话保持
server 10.53.121.51:8080;
server 10.53.121.52:8080;
server 10.53.121.53:8080;
server 10.53.121.66:8080;
server 10.53.121.67:8080;
# TCP 健康检查,只检测端口是否可用
check interval=5000 rise=2 fall=3 timeout=3000 type=tcp;
}

9、添加status页面
location /status {
check_status;
}

nginx.service start-pre operation timed out. Terminating.

问题
服务器重启后Nginx服务不会自动启动,手动启动Nginx服务没有出现任何问题。

错误状态
#systemctl status nginx
nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: timeout) since Fri 2020-02-28 19:13:13 CST; 6min ago
Docs: http://nginx.org/en/docs/
Process: 806 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=killed, signal=TERM)

Feb 28 19:11:43 izj6cdtn3gxvjysis55g0hz systemd[1]: Starting nginx - high performance web server...
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: nginx.service start-pre operation timed out. Terminating.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: Failed to start nginx - high performance web server.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: Unit nginx.service entered failed state.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: nginx.service failed.

#journalctl -u nginx.service
-- Logs begin at Sat 2020-02-29 03:11:38 CST, end at Fri 2020-02-28 19:20:01 CST. --
Feb 28 19:11:43 izj6cdtn3gxvjysis55g0hz systemd[1]: Starting nginx - high performance web server...
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: nginx.service start-pre operation timed out. Terminating.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: Failed to start nginx - high performance web server.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: Unit nginx.service entered failed state.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: nginx.service failed.

原因
没有足够的超时值来启动Nginx服务

解决办法
vim /usr/lib/systemd/system/nginx.service
增加TimeoutStartSec参数值:
[Service]
...
TimeoutStartSec=600
-------------------------------------------
例子
cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
LimitNOFILE=51200
LimitNPROC=51200
LimitCORE=51200
TimeoutStartSec=600

[Install]
WantedBy=multi-user.target
--------------------------------------------

这个情况也可能出现在其他服务上面,没有足够的超时值来启动Nginx或Apache或者其他服务

CentOS7
使用任何文本编辑器(例如vi)打开初始化init脚本:
对于Apache: /usr/lib/systemd/system/httpd.service
对于nginx: /usr/lib/systemd/system/nginx.service

增加TimeoutStartSec参数值
[Service]
...
TimeoutStartSec=600

Debian9或者Ubuntu18
可以使用以下命令找到确切初始化脚本
systemctl status apache2.service | grep loaded
systemctl status nginx.service | grep loaded

使用任何文本编辑器(例如vi)打开初始化init脚本:
对于Apache:/lib/systemd/system/apache2.service
对于nginx:/lib/systemd/system/nginx.service

增加TimeoutStartSec参数值
[Service]
...
TimeoutStartSec=600
https://support.plesk.com/hc/en-us/articles/213387149-Nginx-Apache-are-not-running-after-server-reboot-on-a-Plesk-server-service-start-operation-timed-out-Terminating

Nginx编译加载使用动态模块(Dynamic Shared Object)(DSO)

Nginx编译加载使用动态模块(Dynamic Shared Object)(DSO)
Nginx版本必须>=1.9.11

查看支持的动态模块
[root@localhost nginx-1.12.2]# ./configure --help | grep dynamic
--with-http_xslt_module=dynamic enable dynamic ngx_http_xslt_module
--with-http_image_filter_module=dynamic
enable dynamic ngx_http_image_filter_module
--with-http_geoip_module=dynamic enable dynamic ngx_http_geoip_module
--with-http_perl_module=dynamic enable dynamic ngx_http_perl_module
--with-mail=dynamic enable dynamic POP3/IMAP4/SMTP proxy module
--with-stream=dynamic enable dynamic TCP/UDP proxy module
--with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
--add-dynamic-module=PATH enable dynamic external module
--with-compat dynamic modules compatibility

编译时候的参数
--with-stream \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module

使用stream动态模块
user www www;
worker_processes auto;
worker_cpu_affinity auto;

load_module "modules/ngx_stream_module.so";

error_log /home/wwwlogs/error_nginx.log warn;#debug | info | notice | warn | error | crit ]
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events {
use epoll;
worker_connections 51200;
multi_accept on;
}

stream {
server {
listen 20020;
proxy_pass 192.168.122.214:3389;
}
server {
listen 20021;
proxy_pass 192.168.122.200:3389;
}
}
......
......

CentOS 6、CentOS 7 ,使用crontab和logrotate实现Nginx日志轮询(转存)

CentOS 6、CentOS 7 ,使用crontab和logrotate实现Nginx日志轮询(转存)

1、安装crontab
2、安装logrotate日志文件管理工具
3、分析cron.daily
4、Nginx日志轮询(转存)配置,Apache的类似
5、为什么日志轮询(转存)生成日志的时间是凌晨三四点
6、立即执行logratate
7、logratate常用配置参数

1、安装crontab
if ! which crond >/dev/null 2>&1;then yum install cronie -y; fi
systemctl enable crond

2、 安装logrotate日志文件管理工具
yum -y install logrotate
查看logrotate的软件包的信息
rpm -ql logrotate
/etc/cron.daily/logrotate
/etc/logrotate.conf
/etc/logrotate.d
.....
.....

3、分析cron.daily
cd /etc/cron.daily
[root@www.zhangfangzhou.cn cron.daily]# ls
logrotate makewhatis.cron
[root@www.zhangfangzhou.cn cron.daily]# tail logrotate
#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

4、Nginx日志轮询(转存)配置,Apache日志轮询(转存)类似。
/home/wwwlogs/ 网站日志目录

cat > /etc/logrotate.d/nginx << EOF
/home/wwwlogs/*log {
daily
rotate 5
missingok
dateext
compress
notifempty
sharedscripts
postrotate
    [ -e /usr/local/nginx/logs/nginx.pid ] && kill -USR1 \`cat /usr/local/nginx/logs/nginx.pid\`
endscript
}
EOF

5、为什么日志轮询(转存)生成日志的时间是凌晨三四点?
Logrotate是基于CRON运行的,所以这个时间是由CRON控制的,具体可以查询CRON的配置文件「/etc/crontab」
anacron 正在运行过去 crontab 未进行的各项工作排程

cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45 #最大延迟时间
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22 #只在03到22点之间执行

#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly

每天都执行/etc/cront.daily/目录下的脚本文件,
真实的延迟RANDOM_DELAY+delay。这里的延迟是5分钟,加上上面的RANDOM_DELAY,所以实际的延迟时间是5-50之间,开始时间为03-22点,
如果机器没关,那么一般就是在03:05-03:50之间执行。nice命令将该进程设置为nice=10,默认为0,即低优先级进程。
如果RANDOM_DELAY=0,那么表示准确延迟5min,即03:05执行cron.daily

6、立即执行logratate
ldconfig #执行
或者
/etc/cron.daily
logrotate
或者
执行下面
/usr/sbin/logrotate -f /etc/logrotate.d/nginx

由于logratate已经加到cron.daily(/etc/cron.daily/logrotate),不再需要加到计划任务中

7、logratate常用配置参数
/home/wwwlogs/*log:需要轮询日志路径
daily:每天轮询
rotate 5:保留最多5次滚动的日志
missingok:如果日志丢失,不报错继续滚动下一个日志
dateext:使用日期作为命名格式
compress:通过gzip压缩转储以后的日志
notifempty:当日志为空时不进行滚动
/usr/local/nginx/logs/nginx.pid: nginx pid位置,请查看nginx.conf
postrotate/endscript:在截断转储以后需要执行的命令

CentOS 7 可能出现如下问题
error: skipping "/home/wwwlogs/access_nginx.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
解决方案
vi /etc/logrotate.d/nginx
su $http_user $http_group
su www www

CentOS实现Nginx1.10.3平滑升级Nginx1.11.10(热部署或热加载)

CentOS实现Nginx1.10.3平滑升级Nginx1.11.10(热部署或热加载)
如果要对当前的Nginx服务器进行版本升级,应用新模块,如果用户访问量比较大的时候,如果需要在不影响客户的情况下进行升级的话,这时候就得考虑平滑升级了。
平缓停止Nginx服务
平缓重启Nginx服务

创建用户目录和赋予权限
id -u www >/dev/null 2>&1
[ $? -ne 0 ] && useradd -M -s /sbin/nologin www;
mkdir -p /usr/local/nginx && chown www.www -R /usr/local/nginx;

yum -y install gcc make screen python wget git pcre-devel zlib-devel openssl-devel

CentOS源码编译安装Nginx1.10.3
wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -zxvf nginx-1.10.3.tar.gz;cd nginx-1.10.3
./configure --prefix=/usr/local/nginx --user=www --group=www \
--with-http_stub_status_module \
--with-http_secure_link_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-ipv6 \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_sub_module \
--with-stream \
--with-stream_ssl_module
make
make install

CentOS源码编译Nginx1.11.10
wget http://nginx.org/download/nginx-1.11.10.tar.gz
tar -zxvf nginx-1.11.10.tar.gz;cd nginx-1.11.10
./configure --prefix=/usr/local/nginx --user=www --group=www \
--with-http_stub_status_module \
--with-http_secure_link_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-ipv6 \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_sub_module \
--with-stream \
--with-stream_ssl_module
make

1、重命名低版本的nginx
# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

2、然后拷贝一份新编译的二进制文件:
[root@www.zhangfangzhou.cn nginx-1.11.10]# cp objs/nginx /usr/local/nginx/sbin/

3、在源码目录执行make upgrade开始升级,因为nginx.pid的位置,造成自动升级会报错误
[root@www.zhangfangzhou.cn nginx-1.11.10] # make upgrade

[root@www.zhangfangzhou.cn nginx-1.11.10]# cat Makefile

default: build

clean:
rm -rf Makefile objs

build:
$(MAKE) -f objs/Makefile

install:
$(MAKE) -f objs/Makefile install

modules:
$(MAKE) -f objs/Makefile modules

upgrade:
/usr/local/nginx/sbin/nginx -t

kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin

kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

#下面是手动升级
[root@www.zhangfangzhou.cn nginx-1.11.10]# kill -USR2 `cat /var/run/nginx.pid;`
[root@www.zhangfangzhou.cn nginx-1.11.10]# sleep 1
[root@www.zhangfangzhou.cn nginx-1.11.10]# test -f /usr/local/nginx/logs/nginx.pid.oldbin
[root@www.zhangfangzhou.cn nginx-1.11.10]# kill -QUIT `cat /var/run/nginx.pid.oldbin`

平缓升级Nginx服务:使用新版本的Nginx文件启动服务,之后平缓停止原有的Nginx进程
# kill -USR2 `cat /nginx/logs/nginx.pid`

平缓停止Nginx服务:平缓停止是指允许Nginx服务将当前正在处理的网络请求处理完成,但不再接受新的请求,之后关闭连接,停止工作
# kill -QUIT `cat /nginx/logs/nginx.pid`

平缓重启Nginx服务:Nginx服务进程接受到信号后,首先读取新的Nginx配置文件,如果配置语法正确,则启动新的Nginx服务,然后平缓关闭旧的服务进程,如果新的Nginx配置文件有问题,将显示错误,仍然使用旧的Nginx进程提供服务
# kill -HUP `cat /nginx/logs/nginx.pid`

CentOS 6.x升级系统和PHP的curl,利用curl和cron自动重启https网站的php-fpm

CentOS 6.x升级系统和PHP的curl,利用curl和cron自动重启https网站的php-fpm
curl -I https://www.zhangfangzhou.cn/
curl: (35) SSL connect error #需要升级curl

wget https://curl.haxx.se/download/curl-7.50.0.tar.gz
tar -zxvf curl-7.50.0.tar.gz
cd curl-7.50.0
./configure --prefix=/usr/local/curl
make
make install
cp /usr/local/curl/bin/curl /usr/bin/curl #在源码的文件夹里面也有/root/curl-7.50.0/src/curl

curl --version
curl 7.50.0 (x86_64-pc-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2

php源码编译
--with-curl=/usr/local/curl/

502 Bad Gateway 自动重启脚本
#!/bin/bash
website="https://www.zhangfangzhou.cn/ https://www.zhangfangzhou.com"
if
/usr/bin/curl -I $website|grep "HTTP/1.1 502"
then
/bin/date -R >>/root/502.log
/etc/rc.d/init.d/nginx restart
fi

给予可执行权限:
chmod +x /root/502.sh

通过 crontab 设置周期运行:
crontab -u root -e
* * * * * /root/502.sh >/dev/null 2>&1

修改Nginx源码,使header头信息伪装成其他页面服务器软件

nginx_header
Nginx 修改源码,header 头信息伪装成其他服务器软件

wget http://nginx.org/download/nginx-1.10.1.tar.gz
tar -zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1

1、修改编译nginx核心文件

vim ./src/core/nginx.h

/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/

#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_

#define nginx_version 1006002 #这里最好不要乱改,某些扩展模块编译时,会检测版本,可能会出现错误,如lua_nginx_module
#define NGINX_VERSION "1.6.2" #这里随便改个版本号
#define NGINX_VER "squid/" NGINX_VERSION #这里改成我们想要的名字

#define NGINX_VAR "squid" #这里也一并修改
#define NGX_OLDPID_EXT ".oldbin"

Nginx1.90 nginx_stream 做TCP代理和协议负载均衡

Nginx1.90做TCP代理和协议负载均衡的功能
nginx从1.9.0开始增加了stream模块(ngx_stream_core_module),默认configure不包含该模块,需要在configure的时候加上--with-stream

./configure --prefix=/usr/local/nginx --user=www --group=www \
--add-module=/root/ngx_http_google_filter_module \
--add-module=/root/ngx_http_substitutions_filter_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-ipv6 \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_sub_module \
--with-stream

配置文件很简单,最基本的

.....................
events {
use epoll;
worker_connections 51200;
}
stream {
server {
listen 2002;
proxy_pass 123.123.123.123:3389;
}

server {
listen 2003;
proxy_pass 123.123.123.123:22;
}
}

http {
.....................

需要在防火墙允许相应的端口通过。这样可以反代远程桌面3389端口或者其他固定的TCP端口,比iptables转发或者虚拟专用网络连接来管理国外Windows或者Linux服务器要方便不少。

当然,该模块最重要的功能是支持TCP负载均衡,比如远程多台mysql负载均衡。

stream {
upstream mysql {
server 1.1.1.1:3306;
server 2.2.2.2:3306;
server 3.3.3.3:3306;
}
server {
listen 3306;
proxy_pass mysql;
}
}

官方文档http://nginx.org/en/docs/stream/ngx_stream_core_module.html