本篇是实现在 安装nginx 的基础上实现基于端口,基于IP,基于域名的三种反虚拟主机。
一、基于端口的虚拟主机
修改nginx配置文件
[root@localhost ~]# vim /etc/nginx/nginx.conf
server {
listen 90; #端口1
root /usr/share/nginx/html1; #端口1的根目录
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80; #端口2
listen [::]:80;
server_name _;
root /usr/share/nginx/html; #端口2的根目录
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
[root@localhost ~]# cat /usr/share/nginx/html/index.html
page1
[root@localhost ~]# cat /usr/share/nginx/html1/index.html
page2
重启nginx并通过浏览器进行访问
[root@localhost ~]# nginx -s reload
二、基于域名的虚拟主机
修改配置文件
[root@localhost ~]# vim /etc/nginx/nginx.conf
server {
listen 80;
server_name test1.com; #域名1
root /usr/share/nginx/html1; #域名1的根目录
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
listen [::]:80;
server_name test2.com; #域名2
root /usr/share/nginx/html; #域名2的根目录
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
配置主机文件(域名IP)
[root@test1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.74.128 test1.com
192.168.74.128 test2.com
重启nginx并验证(浏览器不可以)
[root@test1 ~]# nginx -s reload
[root@test1 ~]# curl http://test1.com
page2
[root@test1 ~]# curl http://test2.com
page1
三、基于IP的虚拟主机
基于IP的虚拟主机需要先配置一块虚拟网卡
[root@test1 ~]# ifconfig ens33:1 192.168.74.182 netmask 255.255.255.0 up
通过ip a来查看是否配置成功
[root@test1 ~]# 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:dc:0b:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.74.128/24 brd 192.168.74.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.74.182/24 brd 192.168.74.255 scope global secondary ens33:1
valid_lft forever preferred_lft forever
inet6 fe80::dad0:e690:cce2:ecf0/64 scope link noprefixroute
valid_lft forever preferred_lft forever
可以看到虚拟网卡已经配置成功
配置nginx配置文件
[root@test1 ~]# vim /etc/nginx/nginx.conf
server {
listen 80;
server_name 192.168.74.128; #IP1
root /usr/share/nginx/html1; #IP1的根目录
index index.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
listen [::]:80;
server_name 192.168.74.182; #IP2
root /usr/share/nginx/html; #IP2的根目录
index index.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
重启nginx并通过浏览器访问
[root@test1 ~]# nginx -s reload