[文章作者:张宴 本文版本:v1.0 最后修改:2007.08.21 转载请注明出处:http://blog.zyan.cc]

  我这两天在工作中遇到了一些问题,在今天下午全部解决,于是决定写一篇文章,将实现方法记录下来:

  一、背景环境:
  1、都是Linux服务器;
  2、“服务器A”与“服务器C”不在同一网络,两者之间是不通的;
  3、“服务器A”、“服务器C”分别与“服务器B”相通。

  二、要实现的需求:
  1、让“服务器A”上的PHP程序能够连接“服务器C”上的MySQL数据库(IP:10.10.1.4,端口:3306);
  2、不允许在“服务器A”上的PHP程序中更改MySQL地址(10.10.1.4)和MySQL端口(3306)。

  三、实现原理:
  理论上从“服务器A”是无法直接连接“服务器C”的IP地址(10.10.1.4)及其3306端口的【图中的虚线】,但通过“IP别名+TCP转发+端口映射”,我在“服务器A”上的PHP程序无须作任何修改的情况下实现了这项功能【图中的实线】。
  访问路线:“服务器A”上的PHP程序─→虚拟10.10.1.4:3306─→192.168.1.3:8520─→真实10.10.1.4:3306

  点击在新窗口中浏览此图片

  四、实现方法:
  1、修改“服务器A”上的Apache配置文件httpd.conf(以下仅列出要修改的部分,其余部分用......表示):
引用
................
Listen 192.168.1.2:80
................
NameVirtualHost 192.168.1.2:80
<VirtualHost 192.168.1.2:80>
................

  然后重启Apache:
  /usr/local/apache/bin/httpd -k restart

  2、在“服务器A”上创建本地回环设备lo(即127.0.0.1)的IP别名10.10.1.4,即虚拟IP:
  『图中的①』
  /sbin/ifconfig lo:0 10.10.1.4 broadcast 10.10.1.4 netmask 255.255.255.255 up
  /sbin/route add -host 10.10.1.4 dev lo:0


  3、在“服务器A”上编译安装TCP转发软件rinetd(官方网站:http://www.boutell.com/rinetd/),将对10.10.1.4:80的TCP请求重定向到192.168.1.3:8520上:
  『图中的②』
  wget http://www.boutell.com/rinetd/http/rinetd.tar.gz
  tar zxvf rinetd.tar.gz
  cd rinetd
  make && make install
  vi /etc/rinetd.conf

  输入以下内容(格式:源地址 源端口 目标地址 目标端口):
引用
10.10.1.4 80 192.168.1.3 8520

  启动rinetd守护进程
  /usr/sbin/rinetd -c /etc/rinetd.conf

  4、在“服务器B”上利用iptables配置端口映射,将自身的8520端口映射到10.10.1.4的3306端口上:
  『图中的③』
  echo "1"> /proc/sys/net/ipv4/ip_forward
  /sbin/iptables -t nat -A PREROUTING -p tcp -s 192.168.1.0/24 -d
192.168.1.3 --dport 8520 -j DNAT --to-destination 10.10.1.4:3306
  /sbin/iptables -t nat -A POSTROUTING -p tcp -s 192.168.1.0/24 -d 10.10.1.4 --dport 3306 -j SNAT --to-source 10.10.1.3

  为了防止服务器重启导致TCP转发失效,请:
  vi /etc/rc.local
  增加一行:
引用
echo "1"> /proc/sys/net/ipv4/ip_forward


  或者:
  vi /etc/sysctl.conf
  增加一行:
引用
net.ipv4.ip_forward = 1

  /sbin/sysctl -p

Tags: , ,



技术大类 » 其他Unix技术 | 评论(30) | 引用(0) | 阅读(49938)
shz Email
2022-7-2 22:33
I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.  Lead generation
shzz Email
2022-7-2 22:34
This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article.  Lead generation
shz Email
2022-7-2 22:37
The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.  Lead generation
shzz Email
2022-7-2 22:38
Wonderful blog post. This is absolute magic from you! I have never seen a more wonderful post than this one. You've really made my day today with this. I hope you keep this up!  Lead generation
shz Email
2022-7-2 22:40
Very informative post ! There is a lot of information here that can help any business get started with a successful social networking campaign !  Lead generation
shzz Email
2022-7-2 22:41
Great survey. I'm sure you're getting a great response.  Lead generation
shzz Email
2022-7-2 22:44
I must say, I thought this was a pretty interesting read when it comes to this topic. Liked the material. . . . .  Lead generation
shz Email
2022-7-2 22:44
I see the greatest contents on your blog and I extremely love reading them.  Lead generation
shz Email
2022-7-2 22:54
Thanks for the nice blog. It was very useful for me. I'm happy I found this blog. Thank you for sharing with us,I too always learn something new from your post.  Lead generation
bekean23
2023-12-29 16:28
Are you ready for a gaming experience like no other? Watermelon game invites you to dive into a delightful universe where physics-based challenges meet mind-bending puzzles.
分页: 2/2 第一页 上页 1 2 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]