CentOS local host modification configuration IP domain name resolution

When doing web development, it is usually necessary to access websites in the local environment with http://localhost or 127.0.0.1. However, if there are too many websites locally, other operations have to be done, such as port settings. The result is not easy to remember.

I use the ubuntu+nginx environment locally. nginx allows multiple domain names to be set, and only needs to point to different storage locations. Then, how to set up a domain name of your own choice, and this domain name only accesses a local website?

In windows, there is a file: system32/drivers/etc/host, in this file, you can configure the domain name to point to the local IP, so as to achieve the above purpose.

In ubuntu (usually linux), the hosts file is searched under /etc, which is equivalent to the existence of the host file. If not, create a new one yourself.

sudo vi /etc/hosts

Add the items you need in the following format:

<IP> <HOSTNAME>.<DOMAIN> <ALIAS>

Here are some configurations I did:

127.0.0.1 www.v.com #Enter the www.v.com domain name in my computer browser to access my local designated website. Of course only locally. Those who didn't know the inside story thought that I had registered the domain name www.v.com.
127.0.0.1 www.b2b.com
127.0.0.1 www.itdiffer.org
127.0.0.1 www.weixin.com

It should be noted that in Nginx, conf configuration should be done so that these domain names can be accessed.

 

 

After modifying /etc/hosts, it should normally take effect immediately after saving, but sometimes it is not. Use uname -a to see what the hostname is, and then you can know whether the modification takes effect. If not
, then the strategies are:
1) Restart the machine
2) Restart the service
      Ubuntu: $sudo  /etc/init.d/networking restart

      Gentoo: /etc/init.d/net.eth0 restart

3) Use the hostname command

        hostname defined hostname

 

The relationship between hostname and /etc/hosts

Many people first think of modifying the /etc/hosts file when they mention changing the hostname, thinking that the configuration file of the hostname is /etc/hosts. Actually not.

The role of the hosts file is quite like DNS, providing the correspondence between IP addresses and hostnames. The early Internet computers were few, and the hosts file of a single machine was enough to store all networked computers. But with the development of the Internet, this is far from enough. So there is a distributed DNS system. A similar IP address to domain name correspondence is provided by a DNS server. Specifically, you can man hosts.

The Linux system will query the /etc/hosts file before sending a domain name resolution request to the DNS server. If there is a corresponding record in it, the record in the hosts will be used. The /etc/hosts file usually contains this record

127.0.0.1 localhost.localdomain localhost The
hosts file format is one line by one record, which are the IP address hostname  aliases, which are separated by blank characters, and aliases are optional .

It is recommended not to modify 127.0.0.1 to localhost, because many applications will use this, such as sendmail, and these programs may not work properly after modification.

 

Part of this article draws on:

http://blog.51cto.com/jschu/1746411

http://blog.sina.com.cn/s/blog_605f5b4f0101847z.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325017883&siteId=291194637