What is Web Hosting?
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 www services to the outside world, so that one host can provide multiple web externally. Services are independent of each virtual host and do not affect each other.
Nginx can implement virtual host configuration, nginx supports three types of virtual host configuration.
1. Virtual host based on domain name (server_name to distinguish virtual host-application: external website)
2. Virtual host based on ip (a host is bound to multiple ip addresses)
3. Virtual host based on port (port to distinguish virtual Host-application: the company's internal website, external website management background)
1. Virtual hosting based on domain name
1. Configure virtual machines distinguished by domain names
[root@localhost ~]# cat /etc/nginx/nginx.conf
worker_processes 4;
#error_log logs/error.log;
worker_rlimit_nofile 102400;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name web.testpm.com;
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k;
}
}
server {
listen 80;
server_name web.1000phone.com;
location / {
root /1000phone/html;
index index.html index.htm;
}
}
}
2. Create an index file for the virtual machine whose domain name is web.1000phone.com
[root@localhost ~]# mkdir -p /1000phone/html
[root@localhost ~]# vim /1000phone/html/index.html
<html>
<p>
this is my 1000phone
</p>
</html>
3. Reload the configuration file
# 如果编译安装的执行
[root@nginx]# /usr/local/nginx/sbin/nginx -s reload
# 如果 yum 安装的执行
[root@nginx]# nginx -s reload
4. Configure the route mapping
on the client side . Add two lines to the C:\Windows\System32\drivers\etc\hosts file (linux:/etc/hosts)
10.0.105.199 web.testpm.com
10.0.105.199 web.1000phone.com
5. Test visit
Browser input: http://web.testpm.com/
Browser input: http://web.1000phone.com/
2. IP-based virtual host
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:17:f1:af brd ff:ff:ff:ff:ff:ff
inet 10.0.105.199/24 brd 10.0.105.255 scope global dynamic ens33
valid_lft 81438sec preferred_lft 81438sec
inet6 fe80::9d26:f3f0:db9c:c9be/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ifconfig ens33:1 10.0.105.201/24 #要保证添加的IP没有被使用(给网卡再添加一个ip)
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.105.199 netmask 255.255.255.0 broadcast 10.0.105.255
inet6 fe80::9d26:f3f0:db9c:c9be prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:17:f1:af txqueuelen 1000 (Ethernet)
RX packets 9844 bytes 1052722 (1.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5567 bytes 886269 (865.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.105.201 netmask 255.255.255.0 broadcast 10.0.105.255
ether 00:0c:29:17:f1:af txqueuelen 1000 (Ethernet)
2、配置通过ip区分的虚拟机
[root@localhost ~]# cat /etc/nginx/nginx.conf
user root;
worker_processes 4;
#error_log logs/error.log;
worker_rlimit_nofile 102400;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 10.0.105.199:80;
server_name web.testpm.com;
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k;
}
server {
listen 10.0.105.201:80;
server_name web.testpm.com;
location / {
root /1000phone/html/;
index index.html index.htm;
}
}
}
3、重新加载配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
4、 测试访问
浏览器输入:http://10.0.105.199
浏览器输入:http://10.0.105.201
5、补充
-- 删除绑定的vip
[root@localhost ~]# ifconfig ens33:1 10.0.105.201/24 down
重启一下nginx
[root@localhost ~]# systemctl restart nginx
1. Check the IP and find the network segment
2. Ifconfig ens33:1 192.168.49.148/24 Add ip to the network
card
3. Ifconfig view the network card 4. Edit the configuration file /etc/nginx/nginx.conf Enter two ip addresses before the listening port
5. Reload the nginx configuration file (restart nginx if there is a cache (systemctl restart nginx))
6. Visit ip curl 192.168.49.144
curl 192.168.49.148 (added IP)
3. Port-based virtual host
An IP
[root@localhost ~]# cat /etc/nginx/nginx.conf
user root;
worker_processes 4;
worker_rlimit_nofile 102400;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name web.testpm.com;
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k;
}
server {
listen 8080;
server_name web.1000phone.com;
location / {
root /1000phone/html/;
index index.html index.htm;
}
}
}
Reload the configuration file:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
Test visit:
browser input: http://web.testpm.com/
browser input: http://web.1000phone.com:8080