文章目录
- Nginx 基于多网卡的主机实现
- 一、虚拟机前置环境准备
- `ifcfg-ens32`配置文件的内容参考
- `ifcfg-ens33`配置文件的内容
- 二、案例演示
- 修改nginx.conf配置文件
- 解决中文乱码
Nginx 基于多网卡的主机实现
一、虚拟机前置环境准备
点击虚拟机右下角的 红色标框按钮,然后右键设置,在弹框内点击添加按钮,选择网络适配器,点击完成,然后选择刚添加的网络适配器,右侧选择自定义然后选择net8网卡。我这里是额外添加了2块网卡。建议在操作这种方式之前,先给虚拟机拍摄一个镜像快照,好弄坏了之后可以快速恢复。
添加网卡后,可以通过
ip a
看到,多了ens 33 和 ens 34, ensXX 每个人机器可能会不太一样,我原来只有一个ens32。然后我们需要到 /etc/sysconfig/network-scripts 里重新配置一下 ip地址。这里可以直接copy ens32的配置文件,改下内容和ip即可。需要注意,**UUID,新添加的网卡里的文件需要删除。**修改完成后,再刷新一下网络systemctl restart network
(重启网络服务,包括网络接口和网络配置。执行此命令后,系统会停止并重新启动网络服务,使新的网络配置生效。)
[root@localhost network-scripts]#
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@localhost network-scripts]# 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: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:f0:d4:7e brd ff:ff:ff:ff:ff:ff
inet 192.168.125.100/24 brd 192.168.125.255 scope global noprefixroute ens32
valid_lft forever preferred_lft forever
inet6 fe80::3c1a:3ae7:b89f:526c/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:7c:bd:25 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:7c:bd:25 brd ff:ff:ff:ff:ff:ff
9: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:f0:d4:88 brd ff:ff:ff:ff:ff:ff
inet 192.168.125.3/24 brd 192.168.125.255 scope global noprefixroute dynamic ens33
valid_lft 1793sec preferred_lft 1793sec
inet6 fe80::1412:42a9:8970:c761/64 scope link noprefixroute
valid_lft forever preferred_lft forever
10: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:f0:d4:92 brd ff:ff:ff:ff:ff:ff
inet 192.168.125.4/24 brd 192.168.125.255 scope global noprefixroute dynamic ens34
valid_lft 1798sec preferred_lft 1798sec
inet6 fe80::a25d:4f31:41b2:d1bf/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost network-scripts]#
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@localhost network-scripts]# ls
ifcfg-ens32 ifdown-ippp ifdown-sit ifup-bnep ifup-plip ifup-Team network-functions-ipv6
ifcfg-lo ifdown-ipv6 ifdown-Team ifup-eth ifup-plusb ifup-TeamPort
ifdown ifdown-isdn ifdown-TeamPort ifup-ib ifup-post ifup-tunnel
ifdown-bnep ifdown-post ifdown-tunnel ifup-ippp ifup-ppp ifup-wireless
ifdown-eth ifdown-ppp ifup ifup-ipv6 ifup-routes init.ipv6-global
ifdown-ib ifdown-routes ifup-aliases ifup-isdn ifup-sit network-functions
[root@localhost network-scripts]#
[root@localhost network-scripts]#
[root@localhost network-scripts]# cp ifcfg-ens32 ifcfg-ens33
[root@localhost network-scripts]# cp ifcfg-ens32 ifcfg-ens34
[root@localhost network-scripts]# ls
ifcfg-ens32 ifdown-bnep ifdown-isdn ifdown-Team ifup-bnep ifup-isdn ifup-routes ifup-wireless
ifcfg-ens33 ifdown-eth ifdown-post ifdown-TeamPort ifup-eth ifup-plip ifup-sit init.ipv6-global
ifcfg-ens34 ifdown-ib ifdown-ppp ifdown-tunnel ifup-ib ifup-plusb ifup-Team network-functions
ifcfg-lo ifdown-ippp ifdown-routes ifup ifup-ippp ifup-post ifup-TeamPort network-functions-ipv6
ifdown ifdown-ipv6 ifdown-sit ifup-aliases ifup-ipv6 ifup-ppp ifup-tunnel
[root@localhost network-scripts]#
[root@localhost network-scripts]#
[root@localhost network-scripts]# vim ifcfg-ens33
[root@localhost network-scripts]# vim ifcfg-ens34
[root@localhost network-scripts]#
[root@localhost network-scripts]#
[root@localhost network-scripts]#
[root@localhost network-scripts]# systemctl restart network
ifcfg-ens32
配置文件的内容参考
复制 ifcfg-ens32 ,给 ifcfg-ens33, ifcfg-ens34. vim 修改里面的 NAME,DEVICE,IP为自己的name,device,ip.然后注意 UUID,新添加的网卡里的文件需要删除。
[root@localhost network-scripts]#
[root@localhost network-scripts]# cat ifcfg-ens32
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens32
UUID=1dd7cf75-792e-4eb6-b480-e47a69ea352b
DEVICE=ens32
EONBOOT=yes
IPADDR=192.168.125.100
NETMASK=255.255.225.0
GATEWAY=192.168.125.2
ifcfg-ens33
配置文件的内容
33 和 34的类似,这里就不给出34的了,需要删除 UUID
[root@localhost network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
EONBOOT=yes
IPADDR=192.168.125.101
NETMASK=255.255.225.0
GATEWAY=192.168.125.2
二、案例演示
修改nginx.conf配置文件
前面添加了 2个新的虚拟网卡,并配置了ip为 192.168.125.101,192.168.125.102. 这里就模拟了 在一个nginx服务里管理,3台不同的服务器。下面是分别监听这3台服务器的 80端口,访问的index页面。
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#在http段中添加chartset 设置字符集为 utf-8即可对下面的所有虚拟主机生效,不加会有中文乱码问题
charset utf-8;
server {
listen 192.168.125.100;
server_name localhost;
location / {
root html/virtual01;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.125.101;
server_name localhost;
location / {
root html/virtual01;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.125.102;
server_name localhost;
location / {
root html/virtual02;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
其实 上面的 server段的虚拟主机的location段 / 的路径是 指代 执行了
./sbin/nginx -V
下的--prefix=/opt/software/middleware/nginx-install-1.22.1
, 因此 root 路径里的就是指/opt/software/middleware/nginx-install-1.22.1/html/virtual01
, index 就是指 index首页。nginx默认的首页就是在html/index.html, 我这里为了更深刻的理解它的路径,更换成了自己的文件夹
/opt/software/middleware/nginx-install-1.22.1/html/virtual01/index.html
。根据上面的 conf文件里的配置,当我访问 192.168.125.100 和 192.168.125.101 时,它会监听到80端口被访问,然后返回
/opt/software/middleware/nginx-install-1.22.1/html/virtual01/index.html
也就是 返回: 你好我是虚拟主机1。访问 192.168.125.102时,返回/opt/software/middleware/nginx-install-1.22.1/html/virtual01/index.html
也就是 返回: 你好我是虚拟主机2
[root@localhost nginx-install-1.22.1]#
root@localhost nginx-install-1.22.1]# pwd
/opt/software/middleware/nginx-install-1.22.1
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# ./sbin/nginx -V
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/opt/software/middleware/nginx-install-1.22.1
[root@localhost nginx-install-1.22.1]#
[root@localhost nginx-install-1.22.1]# cat html/virtual0*/index.html
<h1> 你好我是虚拟主机1</h1>
<h1> 你好我是虚拟主机2</h1>
解决中文乱码
在http段中添加chartset 设置字符集为 utf-8即可对下面的所有虚拟主机生效