- 虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务
- 它允许在一台物理服务器上运行多个域名或多个网站,并将它们分别作为独立的虚拟主机来处理请求。虚拟主机可以使得多个网站运行在同一台服务器上,从而节省硬件资源和成本。同时,虚拟主机也提供了更灵活的配置方式,可以针对不同的网站设置不同的Nginx选项和限制。
- 每个虚拟主机之间是独立的,互不影响。
nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。
以下操作可以直接在主配置文件/etc/nginx/nginx.conf
中修改;
也可以在/etc/nginx/conf.d/
下添加一个*.conf格式的子配置文件;注意移除default.conf默认子配置文件,以免端口冲突,并且保证主配置文件得到http模块中有include /etc/nginx/conf.d/*.conf;
[root@localhost ~]# vim /etc/nginx/nginx.conf
worker_processes 4;
#error_log logs/error.log;
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/html;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.testpm.com;
location / {
root /data/www/nginx/html;
index index.html index.htm;
}
}
}
//web.testpm.com的html文件
[root@localhost ~]# mkdir -p /var/www/nginx/html
[root@localhost ~]# vim /var/www/nginx/html/index.html
<html>
<p>
this is web.testpm.com
</p>
</html>
[root@localhost ~]# chown -R nginx:nginx /var/www && chmod 755 -R /var/www
//www.testpm.com的html文件
[root@localhost ~]# mkdir -p /data/www/nginx/html
[root@localhost ~]# vim /data/www/nginx/html/index.html
<html>
<p>
this is www.testpm.com
</p>
</html>
[root@localhost ~]# chown -R nginx:nginx /data/www && chmod 755 -R /data/www
// 如果编译安装的执行
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
// 如果 yum 安装的执行
[root@localhost ~]# nginx -s reload
在 C:\Windows\System32\drivers\etc\hosts 文件中添加两行(linux:/etc/hosts)
192.168.17.128 web.testpm.com
192.168.17.128 www.testpm.com
浏览器输入:http://web.testpm.com/
浏览器输入:http://www.testpm.com/
//如果浏览器打不开页面,可以再开一个新的虚拟机,在域名解析中添加两行
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.17.128 www.testpm.com
192.168.17.128 web.testpm.com
?
[root@localhost ~]# curl www.testpm.com
<html>
<p>
this is www.testpm.com
</p>
</html>
[root@localhost ~]# curl web.testpm.com
<html>
<p>
this is web.testpm.com
</p>
</html>
[root@localhost nginx]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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 group default qlen 1000
link/ether 00:0c:29:42:a3:61 brd ff:ff:ff:ff:ff:ff
inet 192.168.17.128/24 brd 192.168.17.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe42:a361/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ifconfig ens33:1 192.168.17.201/24
?
[root@localhost nginx]# hostname -I
192.168.17.128 192.168.17.201
[root@localhost ~]# cat /etc/nginx/nginx.conf
user root;
worker_processes 4;
#error_log logs/error.log;?
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 192.168.17.128:80;
server_name web.testpm.com;
location / {
root /var/www/nginx/html;
index index.html index.htm;
}
}
server {
listen 192.168.17.201:80;
server_name www.testpm.com;
location / {
root /data/www/nginx/html;
index index.html index.htm;
}
}
}
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
浏览器输入:http://192.168.17.128/
浏览器输入:http://192.168.17.201/
//删除绑定的vip
[root@localhost ~]# ifconfig ens33:1 192.168.17.201/24 down
?
//重启一下nginx
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# cat /etc/nginx/nginx.conf
user root;
worker_processes 4;
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 8080;
server_name web.testpm.com;
location / {
root /var/www/nginx/html;
index index.html index.htm;
}
}
server {
listen 8081;
server_name www.testpm.com;
location / {
root /data/www/nginx/html;
index index.html index.htm;
}
}
}
//重新加载配置文件:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
测试访问:
浏览器输入:http://192.168.17.128:8080/
浏览器输入:http://192.168.17.128:8081/
本地host文件做了解析的话,也可以通过域名:port的方式来访问