Nginx虚拟机主机
简述
虚拟主机是一种特殊软硬件技术,将网络上每一台计算机分成多个虚拟主机,每个虚拟主机可独立对外提供www服务,实现一台主机对外提供多个web服务,每个虚拟主机之间独立,互不影响。
配置位置
既可以在七层代理层配置,又可以在业务逻辑层配置。不同层需要配置不同模块。
支持的虚拟主机配置类型
- 基于域名(server_name区分虚拟主机-应用:外部网站
- 基于IP(一块主机绑定多个IP)
- 基于端口(端口来区分虚拟主机-应用:公司内部网站,外部网站的管理后台
基于域名
#查看初始nginx配置
[root@node2 nginx]# pwd
/etc/nginx
[root@node2 nginx]# cat nginx.conf | grep include
include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;
#所有配置文件在 /etc/nginx/conf.d/*.conf;
#切换文件夹
[root@node2 conf.d]# ll
总用量 4
-rw-r--r-- 1 root root 1072 4月 11 13:21 default.conf
[root@node2 conf.d]# pwd
/etc/nginx/conf.d
#做备份
[root@node2 conf.d]# mv default.conf default.conf.bak
[root@node2 conf.d]# vi test.conf
配置文件
# 标准 location
server {
listen 80;
server_name www.rkun01.com;
root /usr/share/nginx/web;
access_log /var/log/www.rkun01.com.log main;
error_log /var/log/www.rkun01.com.error.log;
location / {
index index.html;
}
}
# 多个 location
server {
listen 80;
server_name www.rkun02.com;
root /usr/share/nginx/html;
access_log /var/log/www.rkun02.com.log main;
error_log /var/log/www.rkun02.com.error.log;
location / {
index index.html;
}
location /test {
index index.html;
}
}
# 模板配置
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
创建所需文件
#根据配置创建文件
[root@node2 nginx]# pwd
/usr/share/nginx
[root@node2 nginx]# mkdir web
[root@node2 nginx]# cd html/
[root@node2 html]# ls
50x.html index.html
[root@node2 html]# cp index.html /usr/share/nginx/web/
[root@node2 html]# ll /usr/share/nginx/web/
总用量 4
-rw-r--r-- 1 root root 615 6月 19 05:36 index.html
#简单修改不同index页面
[root@node2 html]# mkdir test
[root@node2 html]# ls
50x.html index.html test
[root@node2 html]# cp index.html test/
[root@node2 html]# cd test/
[root@node2 test]# ll
总用量 4
-rw-r--r-- 1 root root 622 6月 19 05:40 index.html
加载配置,重启
[root@node2 ~]# nginx -s reload
[root@node2 ~]# systemctl restart nginx
在文件C:\Windows\System32\drivers\etc\hosts
配置
192.168.1.129 www.rkun01.com
192.168.1.129 www.rkun02.com
测试访问
http://www.rkun01.com/
http://www.rkun2.com/
http://www.rkun02.com/test/
基于IP
添加网卡
-
查看当前系统可用网络接口
[root@node2 ~]# ip addr show 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:58:e5:d1 brd ff:ff:ff:ff:ff:ff inet 192.168.1.129/24 brd 192.168.1.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::ad87:aa33:46:de0e/64 scope link noprefixroute valid_lft forever preferred_lft forever #在VMware中为虚拟机添加一张网卡nat模式 [root@node2 network-scripts]# ip a | grep mtu 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 #新网卡为ens36
-
编辑配置文件,创建新网卡配置文件
[root@node2 ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens36 #获取新uuid [root@node2 ~]# uuidgen a108b19f-b8d8-4bdc-be20-13f614128ac9 [root@node2 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens36 [root@node2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens36 [root@node2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens36 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="ens36" UUID="a108b19f-b8d8-4bdc-be20-13f614128ac9" DEVICE="ens36" ONBOOT="yes" IPADDR=192.168.1.136 NETMASK=255.255.255.0 GATEWAY=192.168.1.2 DNS1=8.8.8.8 DNS2=114.114.114.114 [root@node2 network-scripts]# service network restart Restarting network (via systemctl): [ OK ] [root@node2 ~]# ip a | grep "ens3\|inet" inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 192.168.1.129/24 brd 192.168.1.255 scope global noprefixroute ens33 inet6 fe80::ad87:aa33:46:de0e/64 scope link noprefixroute 3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 192.168.1.136/24 brd 192.168.1.255 scope global noprefixroute ens36 inet6 fe80::92c0:bbef:8d35:8ccd/64 scope link noprefixroute
配置通过IP区分的虚拟机
[root@node2 ~]# vim /etc/nginx/conf.d/test.conf
[root@node2 ~]# cat /etc/nginx/conf.d/test.conf
# 标准 location
server {
listen 192.168.1.129:80;
server_name www.rkun01.com;
root /usr/share/nginx/web;
access_log /var/log/www.rkun01.com.log main;
error_log /var/log/www.rkun01.com.error.log;
location / {
index index.html;
}
}
# 多个 location
server {
listen 192.168.1.136:80;
server_name www.rkun02.com;
root /usr/share/nginx/html;
access_log /var/log/www.rkun02.com.log main;
error_log /var/log/www.rkun02.com.error.log;
location / {
index index.html;
}
location /test {
index index.html;
}
}
# 模板配置
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
-
重启服务
nginx -s reload
-
测试访问
http://192.168.1.129 http://192.168.1.136
- 最后去除网卡
基于端口
修改配置文件
[root@node2 ~]# vim /etc/nginx/conf.d/test.conf
[root@node2 ~]# cat /etc/nginx/conf.d/test.conf
# 标准 location
server {
listen 8000;
server_name www.rkun01.com;
root /usr/share/nginx/web;
access_log /var/log/www.rkun01.com.log main;
error_log /var/log/www.rkun01.com.error.log;
location / {
index index.html;
}
}
# 多个 location
server {
listen 8080;
server_name www.rkun02.com;
root /usr/share/nginx/html;
access_log /var/log/www.rkun02.com.log main;
error_log /var/log/www.rkun02.com.error.log;
location / {
index index.html;
}
location /test {
index index.html;
}
}
# 模板配置
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
访问端口,测试
nginx -s reload