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!

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


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

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools