1. Preface
First you need to make sure that Nginx has been installed correctly in your Linux system. Of course, if Nginx is not installed, please refer to
System environment:
Linux environment : centos-7.2
Nginx environment: nginx-1.9.9
2. About Nginx
Nginx is a high-performance http server/reverse proxy server and email (IMAP/POP3) proxy server. Developed by Russian programmer Igor Sysoev, official testing of nginx can support 50,000 concurrent connections, consumes very little resources such as CPU and memory, and runs very stably. Open source and free.
3. What can you do with Nginx?
1. http server: Nginx is an http service that can provide http services independently. Can be used as a static web server.
2. Virtual host: multiple websites can be virtualized on one server. For example, a virtual host used by a personal website.
3. Reverse proxy/load balancing: When the number of visits to the website reaches a certain level and a single server cannot satisfy user requests, multiple server clusters are needed and nginx can be used as a reverse proxy. And multiple servers can share the load evenly, and there will be no downtime due to a high load on a certain server and a certain server will not be idle.
4. Use Nginx to implement virtual host
Here we need to know why we need to create a virtual host. In the actual production environment, our business is accessed through the public network.
When I build a cloud server, one cloud server corresponds to a public IP, so public IP is a very scarce resource for ordinary companies.
For some large companies, such as BAT, it may not matter. .
So using Nginx to implement a virtual host here can run multiple websites on the same service without interfering with each other.
The same server may have an IP, and the website needs to use port 80, but the domain name of the website is different.
There are three ways to distinguish different websites:
1, http service
2, virtual machine implementation
1) IP-based virtual machine
2) Port-based Virtual machine
3) Domain name-based virtual machine
3, reverse proxy, load balancing
5. IP-differentiated virtual host
Bind multiple ones on one server IP address.
Method 1:
Use standard network configuration tools (such as ifconfig and route commands) to add IP aliases,
Enter the command "ifconfig" to view the current ip configuration, as shown below:

Bind another IP to the ens33 network card: 192.168.78.142, and name the network card ens33:1
/sbin/ifconfig ens33:1 192.168.78.142 broadcast 192.168.78.255 netmask 255.255.255.0 up /sbin/route add -host 192.168.78.142 dev ens33:1
After the virtual network card is created, as shown below:

Method 2:
1. Change /etc/sysconfig/network-scripts/ifcfg- Copy the ens33 file,
Enter the directory, enter the command "cp ifcfg-ens33 ifcfg-ens33:1 -r" and name it ifcfg-ens33:1, as shown below:

2. Modify the configuration file, enter the command "vi ifcfg-ens33:1", modify the following content,
NAME=ens33:1 DEVICE=ens33:1 IPADDR=192.168.78.142
No need to modify other items, after the modification is completed , as shown below:

After the creation is completed, test whether the new IP is successfully bound, enter the command "ping 192.168.78.142" in the DOS window, as shown below :

#Note: The IP bound using method one will be automatically unbound after the system restarts and needs to be re-bound. Method two is permanent. , this is a practical conclusion.
3. Restart the system,
Enter the command "reboot", restart the system and then enter the command "ifconfig", you can see that a new network card is created normally,
as shown below: 
6. nginx implements virtual machine
1) Configure nginx virtual host based on IP address
Prepare two HTMLs that identify nginx for easy differentiation during testing : Go to the /usr/local/nginx directory and copy the html into two copies respectively.
Modify the content of index.html below. It’s simpler so I won’t write it here. If you don’t know, please leave a message. Or send a private message as shown below:

server {
listen 80;
server_name 192.168.78.141;
location / {
root html-141;
index index.html index.htm;
}
}
server {
listen 80;
server_name 192.168.78.142;
location / {
root html-142;
index index.html index.htm;
}
}As shown below:

测试nginx 虚拟主机是否可以正常访问,
测试 192.168.78.141 虚拟主机,如下图:

测试 192.168.78.142 虚拟主机,如下图:
2)、配置 nginx 基于端口的虚拟主机
还是老规矩,准备两个标识 nginx 的 HTML,用于在测试时好区别:进入到 /usr/local/nginx 目录下,将 html 分别复制两份,
在修改下面 index.html 的内容,这儿较简单就不在写了,如果不知道请留言或私信,如下图:

修改 nginx 的配置文件,输入命令 “ vi conf/nginx.config ”
方便读者的复制,内容如下:
server {
listen 81;
server_name 192.168.78.141;
location / {
root html-81;
index index.html index.htm;
}
}
server {
listen 82;
server_name 192.168.78.141;
location / {
root html-82;
index index.html index.htm;
}
}
如下图所示:

重启Nginx 后,测试nginx 虚拟主机是否可以正常访问,
测试 81端口的 虚拟主机,如下图:

测试 82端口的 虚拟主机,如下图:

3)、基于域名的虚拟主机
基于域名的虚拟主机是最有用的虚拟主机配置方式。
即一个域名只能绑定一个ip地址,一个ip地址可以被多个域名绑定。
实现基于域名的虚拟主机,在这儿还需要修改 Linux 的 HostName,当然还可以通过 这个工具进行修改 ,对于互联网开发的人来说,经常变更 host 必不可免。每次我们都一遍一遍的去修改hosts文件真是很累,如果能更快速的修改成不同hosts,这儿为大家推荐一个好用的软件 SwitchHosts ,轻松一键切换。
以管理员身份打开,然后就可以设置域名和ip的映射关系,新增一个本地解决方案,键入如下内容,
192.168.78.141 www.12345.com 192.168.78.141 register.12345.com 192.168.78.141 login.12345.com
如下图:

注:修改window的hosts文件:(C:\Windows\System32\drivers\etc)
基于 Nginx 域名的虚拟主机配置,修改内容如下图:
server {
listen 80;
server_name www.12345.com;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 80;
server_name register.12345.com;
location / {
root html-81;
index index.html index.htm;
}
}
server {
listen 80;
server_name login.12345.com;
location / {
root html-82;
index index.html index.htm;
}
}
如下图所示:

修改配置文件后,需要 nginx 重新加载配置文件。
测试 www.12345.com,如下图:

测试 register.12345.com,如下图:

测试 login.12345.com,如下图:

相关推荐:
win10 apache配置虚拟主机后localhost无法使用
The above is the detailed content of Using Nginx to implement virtual host under Centos7.2. For more information, please follow other related articles on the PHP Chinese website!
The Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AMWhat’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.
PHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AMPHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.
PHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AMPHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
PHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AMPHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.
Choosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AMPHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.
PHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AMPHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.
PHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AMPHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AMPHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.






