Nginx#Nginx划分虚拟主机

=====================================================

Nginx虚拟主机基础知识

虚拟主机
  虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响
nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。
  1、基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)
  2、基于ip的虚拟主机 (一台主机绑定多个ip地址)
  3、基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)
在这里插入图片描述

基于一个,另外两个就要一样,甚至不写

在这里插入图片描述

补充:如果配置不能正常访问,
  问题描述: 配置完 nginx 两个虚拟机后,客户端能够访问原始的server ,新增加的 server 虚拟机 不能够访问,报错页面403拒绝访问,排错思路

查看报错日志(找到错误日志)
在这里插入图片描述

检查权限
在这里插入图片描述

检查nginx启动进程
在这里插入图片描述

修改 nginx.conf 文件,重新 reload nginx进程

nginx虚拟主机实验操作

[root@sun conf.d]# nginx -v
nginx version: nginx/1.18.0

先做简单的端口的,然后ip、域名
拷贝default.conf做实验,做完然后恢复,自己指定网站家目录的时候不要指定/mnt /opt等的系统目录,防止出错
三个虚拟主机页面,web、doc、download

1、基于端口划分的虚拟主机,80、81、82

[root@sun ~]# cd /etc/nginx/conf.d/
[root@sun conf.d]# ls
default.conf
[root@sun conf.d]# cp default.conf default.conf.bak
[root@sun conf.d]# ls
default.conf.bak
[root@sun conf.d]# mv default.conf port.conf
[root@sun conf.d]# ls
default.conf.bak  port.conf
[root@sun conf.d]# vim port.conf

子配置文件,只留干货

server {
    
    
    listen       80;

    location / {
    
    
    ¦   root   /mnt/web;
    ¦   index  index.html index.htm;
    }
}

server {
    
    
    listen       81;

    location / {
    
    
    ¦   root   /mnt/doc;
    ¦   index  index.html index.htm;
    }
}

server {
    
    
    listen       82;

    location / {
    
    
    ¦   root   /mnt/download;
    ¦   index  index.html index.htm;
    }
}

[root@sun conf.d]# mkdir /mnt/web
[root@sun conf.d]# touch /mnt/web/index.html
[root@sun conf.d]# echo "<p>web界面</p>" > /mnt/web/index.html  
[root@sun conf.d]# cat /mnt/web/index.html 
<p>web界面</p>
[root@sun conf.d]# mkdir /mnt/doc
[root@sun conf.d]# touch /mnt/doc/index.html
[root@sun conf.d]# echo "<p>doc界面</p>" > /mnt/doc/index.html
[root@sun conf.d]# cat /mnt/doc/index.html 
<p>doc界面</p>
[root@sun conf.d]# mkdir /mnt/download
[root@sun conf.d]# touch /mnt/download/index.html
[root@sun conf.d]# echo "<p>download界面</p>" > /mnt/download/index.html
[root@sun conf.d]# cat /mnt/download/index.html 
<p>download界面</p>
[root@sun conf.d]# nginx -s reload
[root@sun conf.d]# ss -antl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port              
LISTEN     0      128                               *:80                                            *:*                  
LISTEN     0      128                               *:81                                            *:*                  
LISTEN     0      128                               *:82                                            *:*                  
LISTEN     0      5                     192.168.122.1:53                                            *:*                  
LISTEN     0      128                               *:22                                            *:*                  
LISTEN     0      128                       127.0.0.1:631                                           *:*                  
LISTEN     0      100                       127.0.0.1:25                                            *:*                  
LISTEN     0      32                             [::]:21                                         [::]:*                  
LISTEN     0      128                            [::]:22                                         [::]:*                  
LISTEN     0      128                           [::1]:631                                        [::]:*                  
LISTEN     0      100                           [::1]:25                                         [::]:*      

浏览器访问验证(不要在意中文乱码)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、基于ip划分虚拟主机

有多个网卡用多个王网卡,没有就给多加ip

[root@sun conf.d]# ifconfig enp0s25:0 10.11.67.219/24
[root@sun conf.d]# 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: enp0s25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:21:cc:61:f9:57 brd ff:ff:ff:ff:ff:ff
    inet 10.11.67.119/24 brd 10.11.67.255 scope global noprefixroute dynamic enp0s25
       valid_lft 385sec preferred_lft 385sec
    inet 10.11.67.31/24 brd 10.11.67.255 scope global secondary noprefixroute enp0s25
       valid_lft forever preferred_lft forever
    inet 10.11.67.219/24 brd 10.11.67.255 scope global secondary enp0s25:0
       valid_lft forever preferred_lft forever
    inet6 fe80::221:ccff:fe61:f957/64 scope link 
       valid_lft forever preferred_lft forever
临时加ip不要重启网络

我的三个ip
10.11.67.31
10.11.67.119
10.11.67.219

一定要做的是,修完成子配置文件,释放端口
子配置文件

[root@sun conf.d]# mv port.conf ip.conf
[root@sun conf.d]# vim ip.conf
server {
    
    
    listen       10.11.67.31:80;

    location / {
    
    
    ¦   root   /mnt/web;
    ¦   index  index.html index.htm;
    }
}

server {
    
    
    listen       10.11.67.119:80;

    location / {
    
    
    ¦   root   /mnt/doc;
    ¦   index  index.html index.htm;
    }
}

server {
    
    
    listen       10.11.67.219:80;

    location / {
    
    
    ¦   root   /mnt/download;
    ¦   index  index.html index.htm;
    }
}

[root@sun conf.d]# ss -natl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port              
LISTEN     0      128                               *:80                                            *:*                  
LISTEN     0      128                               *:81                                            *:*                  
LISTEN     0      128                               *:82                                            *:*                  
LISTEN     0      5                     192.168.122.1:53                                            *:*                  
LISTEN     0      128                               *:22                                            *:*                  
LISTEN     0      128                       127.0.0.1:631                                           *:*                  
LISTEN     0      100                       127.0.0.1:25                                            *:*                  
LISTEN     0      32                             [::]:21                                         [::]:*                  
LISTEN     0      128                            [::]:22                                         [::]:*                  
LISTEN     0      128                           [::1]:631                                        [::]:*                  
LISTEN     0      100                           [::1]:25                                         [::]:*  

[root@sun conf.d]# nginx -s reload
[root@sun conf.d]# ss -natl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port              
LISTEN     0      128                               *:80                                            *:*                  
LISTEN     0      128                               *:81                                            *:*                  
LISTEN     0      128                               *:82                                            *:*                  
LISTEN     0      5                     192.168.122.1:53                                            *:*                  
LISTEN     0      128                               *:22                                            *:*                  
LISTEN     0      128                       127.0.0.1:631                                           *:*                  
LISTEN     0      100                       127.0.0.1:25                                            *:*                  
LISTEN     0      32                             [::]:21                                         [::]:*                  
LISTEN     0      128                            [::]:22                                         [::]:*                  
LISTEN     0      128                           [::1]:631                                        [::]:*                  
LISTEN     0      100                           [::1]:25                                         [::]:*           
重新加载配置文件,不能释放端口
nginx -s reopen
nginx s stop 
都失败
只能来点狠招数
[root@sun conf.d]# killall -9 nginx
[root@sun conf.d]# ss -antl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port              
LISTEN     0      5                     192.168.122.1:53                                            *:*                  
LISTEN     0      128                               *:22                                            *:*                  
LISTEN     0      128                       127.0.0.1:631                                           *:*                  
LISTEN     0      100                       127.0.0.1:25                                            *:*                  
LISTEN     0      32                             [::]:21                                         [::]:*                  
LISTEN     0      128                            [::]:22                                         [::]:*                  
LISTEN     0      128                           [::1]:631                                        [::]:*                  
LISTEN     0      100                           [::1]:25                                         [::]:*                  
[root@sun conf.d]# nginx
[root@sun conf.d]# ss -antl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port              
LISTEN     0      128                    10.11.67.219:80                                            *:*                  
LISTEN     0      128                    10.11.67.119:80                                            *:*                  
LISTEN     0      128                     10.11.67.31:80                                            *:*                  
LISTEN     0      5                     192.168.122.1:53                                            *:*                  
LISTEN     0      128                               *:22                                            *:*                  
LISTEN     0      128                       127.0.0.1:631                                           *:*                  
LISTEN     0      100                       127.0.0.1:25                                            *:*                  
LISTEN     0      32                             [::]:21                                         [::]:*                  
LISTEN     0      128                            [::]:22                                         [::]:*                  
LISTEN     0      128                           [::1]:631                                        [::]:*                  
LISTEN     0      100                           [::1]:25                                         [::]:*    

浏览器访问验证

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3、基于域名划分虚拟主机

客户端做域名解析(liunx为例)
web.com
doc.com
download.com

vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

10.11.67.31 web.com doc.com download.com

子配置文件

[root@sun conf.d]# mv ip.conf download.conf
[root@sun conf.d]# ls
default.conf.bak  download.conf   
[root@sun conf.d]# vim download.conf
server {
    
    
    listen       80;
    ¦   server_name  web.com;

    location / {
    
    
    ¦   root   /mnt/web;
    ¦   index  index.html index.htm;
    }
}

server {
    
    
    listen       80;
    ¦   server_name  doc.com;

    location / {
    
    
    ¦   root   /mnt/doc;
    ¦   index  index.html index.htm;
    }
}

server {
    
    
    listen       80;
    ¦   server_name  download.com;

    location / {
    
    
    ¦   root   /mnt/download;
    ¦   index  index.html index.htm;
    }
}
[root@sun conf.d]# nginx -s reload
[root@sun conf.d]# ss -antl
State      Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN     0      128                               10.11.67.219:80                                                       *:*                  
LISTEN     0      128                               10.11.67.119:80                                                       *:*                  
LISTEN     0      128                                10.11.67.31:80                                                       *:*                  
LISTEN     0      5                                192.168.122.1:53                                                       *:*                  
LISTEN     0      128                                          *:22                                                       *:*                  
LISTEN     0      128                                  127.0.0.1:631                                                      *:*                  
LISTEN     0      100                                  127.0.0.1:25                                                       *:*                  
LISTEN     0      32                                        [::]:21                                                    [::]:*                  
LISTEN     0      128                                       [::]:22                                                    [::]:*                  
LISTEN     0      128                                      [::1]:631                                                   [::]:*                  
LISTEN     0      100                                      [::1]:25                                                    [::]:*                  
[root@sun conf.d]# killall -9 nginx
[root@sun conf.d]# ss -antl
State      Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN     0      5                                192.168.122.1:53                                                       *:*                  
LISTEN     0      128                                          *:22                                                       *:*                  
LISTEN     0      128                                  127.0.0.1:631                                                      *:*                  
LISTEN     0      100                                  127.0.0.1:25                                                       *:*                  
LISTEN     0      32                                        [::]:21                                                    [::]:*                  
LISTEN     0      128                                       [::]:22                                                    [::]:*                  
LISTEN     0      128                                      [::1]:631                                                   [::]:*                  
LISTEN     0      100                                      [::1]:25                                                    [::]:*                  
[root@sun conf.d]# nginx
[root@sun conf.d]# ss -antl
State      Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN     0      128                                          *:80                                                       *:*                  
LISTEN     0      5                                192.168.122.1:53                                                       *:*                  
LISTEN     0      128                                          *:22                                                       *:*                  
LISTEN     0      128                                  127.0.0.1:631                                                      *:*                  
LISTEN     0      100                                  127.0.0.1:25                                                       *:*                  
LISTEN     0      32                                        [::]:21                                                    [::]:*                  
LISTEN     0      128                                       [::]:22                                                    [::]:*                  
LISTEN     0      128                                      [::1]:631                                                   [::]:*                  
LISTEN     0      100                                      [::1]:25                                                    [::]:*        

浏览器访问验证
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

清理环境

[root@sun conf.d]# ls
default.conf.bak  download.conf
[root@sun conf.d]# rm -rvf download.conf 
已删除"download.conf"
[root@sun conf.d]# mv default.conf.bak default.conf
[root@sun conf.d]# killall -9 nginx
[root@sun conf.d]# nginx
[root@sun conf.d]# ss -antl
State      Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN     0      128                                          *:80                                                       *:*                  
LISTEN     0      5                                192.168.122.1:53                                                       *:*                  
LISTEN     0      128                                          *:22                                                       *:*                  
LISTEN     0      128                                  127.0.0.1:631                                                      *:*                  
LISTEN     0      100                                  127.0.0.1:25                                                       *:*                  
LISTEN     0      32                                        [::]:21                                                    [::]:*                  
LISTEN     0      128                                       [::]:22                                                    [::]:*                  
LISTEN     0      128                                      [::1]:631                                                   [::]:*                  
LISTEN     0      100                                      [::1]:25                                                    [::]:*                 

猜你喜欢

转载自blog.csdn.net/kakaops_qing/article/details/109014111