当前位置: 首页 > news >正文

【linux-网络】4层转发方法-iptable以及nginx

1.背景

有时候远程或者某些业务需要做转发就会用到iptables或者nginx,或者ss都可以

根据自己的情况去适配。

2.方法:

1)iptables

-把linux内核转发功能打开

echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf

-出入转发:192.168.1.1:19100 -->10.22.0.5:9100

iptables -t nat -A PREROUTING -d 192.168.1.1 -p tcp -m tcp --dport 19100 -j DNAT --to-destination 10.22.0.5:9100

iptables -t -A POSTROUTING -d 10.22.0.5/32 -p tcp -m tcp --dport 9100 -j SNAT --to-source 192.168.1.1

2) nginx

-安装nginx:--一般现在新装版本nginx都在1.19以上,都含有--with-stream模块,手动编译的自行添加

yum -y install nginx

nginx -V

nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

配置:放到nginx.conf 里面就放到http上面,也可以在http上面加include指定路径。

stream {upstream tcp_proxy {hash $remote_addr consistent;server 10.22.0.5:9100;}server {listen 19100;proxy_connect_timeout 1s;proxy_timeout 10s;proxy_pass tcp_proxy;}}

启动服务器可;

检查端口是否起来

netstat -nltp | grep 19100

如果发现--with-stream=dynamic 存在启动还是报错的话

# yum -y install nginx-all-modules.noarch

http://www.lryc.cn/news/106925.html

相关文章:

  • vue复制文案,复制图片,黏贴图片
  • Web应急思路
  • shell脚本清理redis模糊匹配的多个key,并计算释放内存大小
  • python-MySQL数据库建表语句(需要连接数据库)转存为Excel文档-工作小记
  • iOS Block介绍
  • 小程序安全性加固:如何保护用户数据和防止恶意攻击
  • Ubuntu的tar命令详解
  • 使用elementplus实现文本框的粘贴复制
  • 计算机毕设 深度学习卫星遥感图像检测与识别 -opencv python 目标检测
  • devops(前端)
  • SpringBoot中MongoDB的使用
  • Spring学习之GOF的工厂模式
  • 整数转字符串
  • 【ARM Coresight 系列文章 2.4 - Coresight 寄存器:DEVARCH,DEVID, DEVTYPE】
  • Could not locate supplied template: react+ts搭建
  • fatal error C1128: 节数超过对象文件格式限制: 请使用 /bigobj 进行编译
  • xml文件转成yolo中的txt文件
  • [Linux]手把手教你制作进度条小程序
  • centos 重启 nginx 的三种方式
  • 跨境新手必看,海外推广的7个方式
  • SpringBoot之logback-spring.xml详细配置
  • P2141 [NOIP2014 普及组] 珠心算测验
  • [回馈]ASP.NET Core MVC开发实战之商城系统(四)
  • Vue.js常见错误处理包含代码
  • Go项目实现日志按时间及文件大小切割并压缩
  • 容器化的好处
  • TPlink DDNS 内网穿透?外网访问设置方法
  • 以CS32F031为例浅说国产32位MCU的内核处理器
  • 享元模式(Flyweight)
  • Cilium系列-11-启用带宽管理器