table of Contents
Foreword:
Nginx virtual host:
virtual host is a special software and hardware technology, it can divide each computer on the network into multiple virtual hosts, each virtual host can independently provide web services to the outside, so that one host can be realized Provide multiple web services externally. Each virtual host is an independent website and independent domain name. From the perspective of website visitors, each virtual host is exactly the same as an independent host and does not affect each other.
Nginx can implement virtual host configuration, nginx supports three types of virtual host configuration.
- Divide virtual hosts by port (application: company internal website, web site management background)
- Divided by domain name (application: external website)
- Divided by IP (a host is bound to multiple IPs, which is less used)
Ready to work
System: Centos7
Nginx version: 1.18.0
Address: 10.8.161.9
Test machine system: win10
Address: 10.8.161.45 (same LAN)
1. Port division virtual host
- Configure ports 81, 82, and 83 as virtual host ports. Take the configuration of port 81 as an example. 82 and 83 are similar.
vim /etc/nginx/conf.d/server1.conf # 在 子配置文件目录conf.d创建81端口的主机
####一下是配置信息
server {
listen 81; # 82、83更换端口即可
server_name localhost; #本机模式下
location / {
root /var/www/nginx/server1; #指定网页的目录
index index.html index.htm; #指定访问的主页
}
}
- Create 81 virtual host web pages and web files according to the directory in the configuration file
mkdir -p /var/www/nginx/server1 创建网页目录
vim /var/www/nginx/server1/index.html # 创建网页文件
显示内容:Server1 Host #创建内容
- Check whether the configuration is correct and restart the nginx service
[root@localhost ~]# nginx -t # 检测命令,显示以下两行输出信息则配置正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl restart nginx #重启nginx服务
- Open the browser, access the server address, followed by the port for configuring the virtual host.
2. Domain name division virtual host
- Configure local DNS resolution on the test machine to facilitate testing. (This test machine win10)
①
②
③The
test domain name can be pinged, and it is 10.8.161.9
- Configure virtual host 1, similar to 2, 3
vim /etc/nginx/conf.d/server1.conf # 在 子配置文件目录conf.d创建www.sercer1.com主机
####一下是配置信息
server {
listen 80;
server_name www.server1.com; #指定域名,2、3虚拟主机进行更换即可
location / {
root /var/www/nginx/server1; #指定网页的目录
index index.html index.htm; #指定访问的主页
}
}
- Check whether the configuration is correct and restart the nginx service
[root@localhost ~]# nginx -t # 检测命令,显示以下两行输出信息则配置正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl restart nginx #重启nginx服务
- Open browser test
Three. IP divides the virtual host
- Simulate multiple IP addresses with a network card on the server
①Check the current IP
ip -a #查看当前网卡的ip地址
②Create multiple virtual ip
ifconfig ens33:1 10.8.161.66/24
- Configure virtual host 1, similar to 2, 3
vim /etc/nginx/conf.d/server1.conf # 在 子配置文件目录conf.d创建www.sercer1.com主机
####一下是配置信息
server {
listen 80;
server_name 10.8.161.66; #指定虚拟的IP,2、3虚拟主机进行更换即可
location / {
root /var/www/nginx/server1; #指定网页的目录
index index.html index.htm; #指定访问的主页
}
}
- Check whether the configuration is correct and restart the nginx service
[root@localhost ~]# nginx -t # 检测命令,显示以下两行输出信息则配置正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl restart nginx #重启nginx服务
- Open browser test
================================================= ================================================= ================================================= ================================================= ================================================= =============================================
Hard browsing and watching, if right You are helpful, please like it (σ゚∀゚)σ…:*☆