[文章作者:张宴 本文版本: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) | 阅读(50228)
xiaobao
2007-8-28 22:54
如果是比较大的并发访问,这种方法应该会影响到程序执行时间的吧
秋水晓寒 Email
2007-9-12 14:29
你还不如直接说:A通过B去映射C
redeyes Email Homepage
2008-6-3 14:49
rnatd + iptables 哎,为什么不用 ssh tcp forward 呢?
嘿嘿,我知道sina的 sshd 都禁止了ssh 转发。。
coach factory outlet Email Homepage
2012-5-17 11:01
Today, following half a century, mentor leather-bases coach factory outlet continues to be the delicate craft of leather-based master is accountable for,Would you like to meet more friends, or go with the times? If yes, coach factory online is opening welcome doors to you.in the market you definitely can find various colorways that are designed in as well as the high quality that applied in. For most of you would like to come. So just come to our coach factory outlet online store to choose one.
louis vuitton sale Email Homepage
2012-5-17 11:01
Louis Vuitton belt at louis vuitton sale is one kind of fashion accessory with high cost performance among the Louis Vuitton accessories.Offering quality LV products with favorable prices, louis vuitton outlet store is at your service. Hurry up, or you can not seize the chance.in fact, louis vuitton is one of the most famous fashion design master.he opened the fist suitcase shop called after his name.
coach outlet online Email Homepage
2012-5-17 11:01
I heard of coach outlet online through the advertisement when I was shopping. And now I often brow the webpage and buy Coach bags online.It is a symbol regarding position not to mention nature.Here I would like to launch a excellent bags pertaining to business men.Which may be coach outlet store.coach outlet has always been simple,durable style features to win consumers.The products are more flexible,with easy bleaching,wear characteristics,and simply use a damp cloth.
coach outlet, Email Homepage
2012-5-17 11:01
Remember the coach outlet provide coach bags which won't be deteriorated into its overall styles by any means. It will maintain its looks, colors, and uniqueness for long time.coach outlet store online has been voted by Hour Detroit magazine readers as the Best of Detroit in their 12th annual readers'poll.Lots of women like which usually amount normally include a coach outlet online ,it provides coziness to many girls that don't even think it is a great bushel of great interest directly to them.
xujie777 Email
2012-5-18 16:21
We aim to make all our customers satisfy with our products. You will find a variety of Men's fashion louis vuitton uk, fashion Women's cheap Louis Vuitton bags in our store at affordable price.Thinking of interesting ways to cost a milestone birthday? louis vuitton online shop had one of the most distinctive distinctive celebrations.bakery along with living room operated by means of about three moment louis vuitton online Most effective Pastry Chef’s of the year Rammy Nominee Chef’s.
xujie777 Email
2012-5-18 16:22
The choices are likely to be basically countless seeing that louis vuitton outlet occurs with the help of completely new and also incredible concepts once in a while.Louis vuitton Wholesale Monogram Canvas HandbagsLouis Vuitton Collection Beach Handbags louis vuitton bags outlet Damier Canvas HandbagsLouis vuitton Mahina HandbagsLouis Vuitton Monogram Mini Lin HandbagsLouis Vuitton Monogram Multicolore HandbagsLouis vuitton Monogram Vernis HandbagsLouis Vuitton Wholesale Epi Leather HandbagsLouis Vuitton For Men HandbagsLouis Vuitton Damier Canvas WalletsLouis Vuitton Epi Leather WalletsLouis Vuitton Monogram Canvas WalletsLouis Vuitton Monogram Vernis WalleLouis Vuitton ShoesLouis Vuitton Men wallets.As the Authentic Louis Vuitton are so high-priced, so came the louis vuitton handbags outlet.
xujie777 Email
2012-5-18 16:22
coach outlet has become a popular shopping experience for consumers around the world, and a desirable distribution channel for manufacturer's and retailers.Getting your hands on coach outlet store online can be a hefty investment of hundreds of dollars.But do not despair,the Coach Outlet store could be the answer to your prayers.coach outlet online is your smart choice when you want to get the discount Coach accessory. You can find the exact Coach Bags and other accessory you want at a low price that's right for you. My dear friends, let the coach outlet online pave your way into the world of high fashion with their ultimate fashion factory.
xujie123
2012-5-21 11:06
Our online store offers you discounted Designer louis vuitton replica wallet at present. You could find them in desirable quality and price. If you don't mind high class louis vuitton uk, have a good time here.louis vuitton Store Online Handbags can also bring great accuracy as well as practical applicability and fashionable.Have you ever dreamed of being as charming as Madonna? Have you ever thought of becoming an envy of all your friends? If so, come to louis vuitton outlet.<br/>
xujie555
2012-5-21 13:30
coach factory outlet uses graceful accessories to match the classical logo of coach, which is the best combination of coach. The handmade coach products?can make you more charming and graceful.coach factory online provides people many coach goods. If you wish to snatch the coach handbag, then this best method is made for that you like for coach discount.coach factory outlet online is a fashion brand to ensure its quality. With designer coach shoulder bags, you will always attract people's attention. The bags will emphasize your personal style and taste.<br/>
xujie333
2012-5-21 14:51
coach outlet online Store guarantee that all the coach handbags offered are own high quality. In addition , all of them are sold at an unexpected low price.If you want to purchase, just visit their website.coach factory outlet is really sizzling kinds of shopping way for you. With the usage of the replica designer coach bags, you can surely be able to change your individual looks in a stunning manner.Coach bags on sale from the coach outlet are cheap or discount prices that you certainly will stand out from the crowd on your next camping trip!<br/>
coach88888888 Email
2012-8-13 17:37
If you desire to go to Coach Factory Outlet, but have no idea in which to go, you can research online. It is no doubt that there is drastically information and details about it for the reference.Coach Factory Online are loved by many people, when you walk in the street, you could see many people take coach styles.hat experts claim Coach Outlet Online shopping is in the changes they are available in.Coach Outlet will send you a coupon in the post to use in their upon only after you type a achieve. The Coach bags are customarily somewhat many than the ones in the stores.
SteveBrad Email
2012-10-10 17:18
This website has very good content. so I am sure this website will form the well-known in the future. | Buy Viagra Online |
sk Email
2021-6-24 02:14
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. กาแฟอาราบิก้า
sk
2021-7-3 02:25
As a very satisfied customer not only with their products, but also their customer services, I’d definitely recommend this printing https://www.digitekprinting.com/canvas-prints-2 to my fellow colleagues.
sk Email
2021-7-14 20:23
I would really like to try out this door company's services. I heard a lot of great things about Caldwells. Here's the https://caldwells.com/interior-doors/shaker-doors to their site and I would gladly appreciate your thoughts about them.
Phyllis A. Turner
2021-9-30 22:19
I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy For More Information.
OttoOsborn Email Homepage
2022-5-10 17:19
On a path from the end system to any other distant end system, an access network physically connects an end system to the immediate router (also known as the "edge router"). ISPs, residential networks, workplace networks, ADSL, mobile networks, and FITH are all examples of access networks. If you want free pc games, check out ben the talking dog. It is best for kids.
分页: 1/2 第一页 1 2 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]