ubuntu20.04安装Nginx端口转发

作者: seamus 分类: 常用 发布时间: 2022-09-22 13:35

1:Nginx安装

apt-get update && apt-get install -y nginx

2:运行Nginx

systemctl start nginx.service

2.1:设置Nginx转发配置文件

mkdir /etc/nginx/tcp.d/
nano /etc/nginx/tcp.d/zf.conf

然后输入转发配置:

stream{
    upstream tcpssh {
               server  IP(域名):端口;  
    }
    server{
        listen NAT/VPS外部端口;
        listen NAT/VPS外部端口 udp;
        proxy_pass tcpssh;
    }
}

2.2 在Nginx添加strem目录

cd /etc/nginx/
nano nginx.conf

在配置文件中添加以下内容:

# tcp/ip proxy
include /etc/nginx/tcp.d/*.conf;

或采用echo命令

echo "include /etc/nginx/tcp.d/*.conf;" >> /etc/nginx.conf

3:用 nginx -t 检查配置
输出
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
3.1 关闭防火墙并禁止开机启动启动防火墙

sudo ufw enable | disable

4:设置开机启动nginx

systemctl enable nginx && systemctl start nginx

5:查看Nginx运行状态

ss -ntlp| grep -i nginx

6重启Nginx

systemctl restart nginx
service nginx restart

6.1:禁止selinux

sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config
setenforce 0

Oracle关闭Iptable规则

Oracle自带的Ubuntu镜像默认设置了Iptable规则,关闭它。

apt-get purge netfilter-persistent

开启为:

apt-get install netfilter-persistent

然后重启:

reboot

强制删除

rm -rf /etc/iptables && reboot

查看规则是否生效,命令:

iptables -L