1.yum 安装方式
vim /etc/nginx/conf.d/proxy.conf
stream {
upstream proxy_mysql {
server 192.168.1.10:3306;
}
server {
listen 9999;
proxy_pass proxy_mysql;
}
}
[root@localhost ~]# nginx -t
nginx: [emerg] unknown directive "stream" in /etc/nginx/conf.d/proxy.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
#若是出现上面的错误,执行下面的操作后再nginx -t
[root@localhost ~]# yum -y install nginx-all-modules.noarch
[root@localhost ~]# nginx -t
nginx: [emerg] "stream" directive is not allowed here in /etc/nginx/conf.d/proxy.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
#安装完模块之后继续问题,这个问题表示stream指令不允许防止在这里,那当前的位置是在http指令中。所以把他移出来。
vim /etc/nginx.conf
events {
......
}
http {
......
}
#配置方法1
stream {
server {
listen 9999;
proxy_pass 192.168.1.10:3306;
}
}
#配置方法2
stream {
upstream proxy_mysql {
server 192.168.1.10:3306
}
server {
listen 9999;
proxy_ass proxy_mysql;
}
}
# 然后再nginx -t 即可通过。再nginx -s reload即可,不行就systemctl restart nginx
2.编译安装方式的端口转发
nginx -V #获取原来的编译参数
./configure --with-stream (添加上即可)
原文:https://www.cnblogs.com/sharkchili/p/15252650.html