本文描述如何使用Nginx作TCP转发。
在Amazon Linux 2上,执行如下命令安装Nginx最新版1.20。
yum update -y
amazon-linux-extras install nginx1
yum install nginx-mod-stream
systemctl enable nginx
systemctl start nginx
编辑配置文件
vim /etc/nginx/nginx.conf
内容替换为如下:
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;
}
stream {
upstream backend {
server 1.2.3.4:80 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass backend;
}
}
其中1.2.3.4是要转发的服务器。保存退出。
重启服务:
systemctl restart nginx
配置完成。