NGINX转发udp、tcp

安装nginx

安装依赖

yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl-devel

centos安装报错

[root@localhost nginx-1.20.2]# yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl-devel
CentOS Linux 8 - AppStream                                                                                                                                                                                                                    59  B/s |  38  B     00:00    
错误:为仓库 'appstream' 下载元数据失败 : Cannot prepare internal mirrorlist: No URLs in mirrorlist

CentOS Linux 8在2022年12月31日来到生命周期终点(End of Life,EoL)。即CentOS Linux 8操作系统版本结束了生命周期(EOL),Linux社区已不再维护该操作系统版本。所以原来的CentOS Linux 8的yum源也都失效了!最终导致此问题的产生。

解决方法

更改源,再试,使用简单操作

sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-*
sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" /etc/yum.repos.d/CentOS-*

编译

# 编译
./configure 
 
# 需要使用https执行指令
./configure  --with-http_ssl_module
# 不需要使用https执行
./configure

nginx 默认路径/usr/local/nginx/conf
直接配置会报错

nginx: [emerg] "upstream" directive is not allowed here in /usr/local/nginx/./conf/nginx.conf:16

检查

[root@localhost nginx]# ./sbin/nginx -t
nginx: [emerg] unknown directive "stream" in /usr/local/nginx/conf/nginx.conf:17
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

安装stream插件

# 先安装
yum -y install epel-release
yum -y install nginx-all-modules.noarch
然后在用nginx -t就好了
[root@k8s-node2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

转发配置,这里贴一个完整的配置,因为很多人就只贴一部分,花了不少时间,给小白参考。


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 nginx_80 {
        	server xx.xx.xx.xx:80 max_fails=1 fail_timeout=10s;
	}


	#udp
	upstream nginx_62014 {
        	server xx.xx.xx.xx:62014 max_fails=1 fail_timeout=10s;
	}

	server {
		listen 80;
		proxy_connect_timeout 5s;
        	proxy_timeout 10m;
	        proxy_pass nginx_80;		
	}

	server {
		listen 62014 udp;
		proxy_connect_timeout 5s;
        	proxy_timeout 10m;
	        proxy_pass nginx_62014;		
	}
}

以上配置就做完了,直接启动就可以转发成功。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆芽脚脚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值