nginx通过四层代理实现端口转发
需要两台虚拟机,一台用作nginx代理(安装 --with-stream模块 192.168.200.113),一台用作测试访问(安装nginx,写测试文本 192.168.200.112),目的是通过访问代理机的300端口可以访问到测试机的80端口。
测试机就是Web服务器,可以是nginx的80端口,apache的80端口,tomcat的8080端口。
一、nginx代理机(192.168.200.113)
[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar -xf nginx-1.15.9.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.15.9/
编译安装,安装所需模块
[root@localhost ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-stream && make && make install
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 1024;
}
stream {
server {
listen 300; //通过本机的300端口访问
proxy_pass 192.168.200.112:80; //可以访问到112主机上的80端口
}
}
http {
include mime.types;
default_type application/octet-stream;
[root@localhost ~]# nginx
二、安装nginx
省略安装过程,编写测试文件如下:
[root@localhost ~]# cat /usr/local/nginx/html/index.html bbbbbb
三、测试
