本地域与本地主机的性能 [英] Performance of local domain vs localhost

查看:77
本文介绍了本地域与本地主机的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与以下对象的TCP连接之间是否存在性能差异?

Is there a performance difference between TCP connections to:

  • 本地主机/127.0.0.1
  • 解析为本地计算机的域

或更具体地说,后者的连接是通过回送设备还是通过实际网络进行?

Or more specifically, do the latter connections go through the loopback device, or over the actual network?

我问的原因是我正在考虑更改许多PHP应用程序中的数据库设置,以便它们使用完整域而不是localhost.这样,如果需要,我们可以更轻松地将数据库移至其他服务器.

The reason I'm asking is I'm thinking about changing database settings in many PHP apps so they use a full domain instead of localhost. That way we could more easily move the database to a different server, if the need arises.

推荐答案

这取决于实现和操作系统.在Windows上,连接到本地IP地址的任何内容(即使它是面向外部的IP)都将经过环回.对于诸如数据包嗅探器之类的应用程序,这是一个已记录的问题,因为您无法嗅探回送. (Windows不会将环回视为设备",它是在网络级别处理的.)但是,在这种情况下,它将对您有利.

This is implementation and operating system dependent. On Windows, anything connecting to a local IP address, even if it is an outside-facing IP, will go over loopback. This is a documented problem for applications such as packet sniffers, because you can't sniff the loopback. (Windows doesn't treat loopback as a "device" -- it is handled at the network level.) However, in this case it would work in your favor.

Linux将遵循路由表中的所有内容,因此,如果路由表不是,则发往本地计算机 的数据包将通过网络发送到本地计算机正确配置.但是,在99%的情况下,将正确配置路由.您的数据包不会通过环回设备,但是TCP/IP堆栈会知道您正在与本地IP联系,并且实际上它会在适当的以太网设备中传出并返回.

Linux, in contrast, will follow whatever you have in your routing table, so packets that are destined to your local machine will go to your local machine over the network if the routing table isn't properly configured. However, in 99% of the cases the routing will be configured properly. Your packets won't go over the loopback device, but the TCP/IP stack will know that you are contacting a local IP and it will virtually go out and back in the proper ethernet device.

在正确配置的环境中,使用域名的唯一瓶颈将是DNS解析时间.联系外部DNS可能会在您的配置中增加额外的延迟.但是,如果将域名添加到/etc/hosts文件中(在Windows中为C:\Windows\System32\drivers\etc\hosts),则系统将跳过DNS解析阶段并直接获取IP,这将使时间成本降低.

In a properly configured environment, the only bottleneck for using a domain name would be DNS resolution time. Contacting an outside DNS can add additional latency into your configuration. However, if you add in the domain name into your /etc/hosts file (C:\Windows\System32\drivers\etc\hosts on Windows), your system will skip the DNS resolution phase and obtain an IP directly, making this time cost moot.

这篇关于本地域与本地主机的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆