Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nginx virtual host based on ip

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Article source

Operation and maintenance guild: nginx ip-based virtual host

1. What is a virtual host

Virtual hosts use special technology to logically divide a running server into multiple hosts. The main reason for this is to allow multiple website programs to run on a physical server, so that you can take advantage of the remaining space on the server. Give full play to the role of the server. Virtual hosts are completely independent. In this way, when establishing a website platform, you only need to use one nginx software to run multiple ip-based or domain name-based websites.

2. Virtual host based on ip

Generally, domain names are used to configure virtual hosts for public web sites. When multiple applications are deployed on the same server in the intranet, ip can be used to configure virtual hosts. Of course, it is also OK to configure the domain name in the intranet, in fact, it is more appropriate to call the host name, so we will not discuss it here.

(1) configure virtual ip on a server first.

This step is very important because it is only with this virtual ip that the request can be applied correctly.

The following is the method for linux to configure virtual ip. There are two ways to build virtual ip. The following will not be explained in detail. Later, we will write articles on these two methods of establishing virtual ip.

Alias ip establishment method:

Ifconfig eth0:1 192.168.8.5 netmask 255.255.255.0 up

This is to create a virtual network card named eth0:1,ip 192.168.8.5 with a subnet mask of 255.255.255.0.

Then use ifconfig-a to check

Auxiliary ip establishment method:

Ip addr add 192.168.40.20/24 dev eth0

This established ip can not be viewed with ifconfig-a, but only with ip a. But if it's an alias ip, it's okay to use this method to check it out.

If you want the virtual ip to work permanently, write it to the configuration file.

(2) configuration in nginx.conf

Just add the following fields to the nginx.conf.

Include vhosts/*.conf

The above fields can be added to the http module.

Then create the vhosts directory in the conf directory of nginx, but do not create it if you have one.

(3) add virtual host configuration file

Add a clear .conf configuration file to the vhosts directory with any name, preferably related to the deployed application, for later maintenance.

Server1.conf

Server {

Listen 80

# configure the listening port, as long as you configure 80, no matter how many virtual hosts you write, use port 80.

Server_name 192.168.8.4

# this is the most important, configure the required domain name.

Root / usr/local/nginx/html/wp1/

# it is also very important to write down the project path clearly. Do not misconfigure it.

Access_log / usr/local/nginx/logs/wp1/access.log main

# the access log can be written clearly or not. In the production environment, the log must be configured clearly to separate the log.

Location / {

Index index.html index.htm

}

}

Server2.conf

Server {

Listen 80

# configure the listening port, as long as you configure 80, no matter how many virtual hosts you write, use port 80.

Server_name 192.168.8.5

# this is the most important, configure the required domain name.

Root / usr/local/nginx/html/wp2/

# it is also very important to write down the project path clearly. Do not misconfigure it.

Access_log / usr/local/nginx/logs/wp2/access.log main

# the access log can be written clearly or not. In the production environment, the log must be configured clearly to separate the log.

Location / {

Index index.html index.htm

}

}

After the above configuration, restart nginx or reload.

3. Summary

Many virtual hosts can be written in the same configuration file, but it will be inconvenient to maintain at a later stage (the author has a lot of experience, especially when connecting to platforms maintained by others). Write as many configuration files as possible so that they look short and easy to read.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report