03. Nginx入门-Nginx虚拟主机

Nginx虚拟主机简介

yum安装与源码安装一样,只是Nginx配置文件路径不一致,这里用的yum安装的配置文件路径。
利用虚拟主机的功能,可以在一台Nginx服务器上部署一个或多个虚拟主机。

虚拟主机主配置文件

注意:配置完成Nginx主配置文件以后,后续子配置文件只需要在指定的目录下添加子配置文件,且子配置文件不需要在写http标签。
路径:/etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

charset utf-8;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

单虚拟主机

只需要在http{}区域中设置一个server{}标签即可。

要求

  1. 部署一个WEB服务
  2. WEB服务域名为www.wangmingqu.com
  3. 访问域名可以查看/www/wangmingqu/index.html页面

准备数据

#编辑html文件
mkdir -p /www/wangmingqu/
echo "hello word,I am wangmingqu!" > /www/wangmingqu/index.html

#下载图片
cd /www/wangmingqu/
wget https://img1.bdstatic.com/static/searchdetail/widget/toolbar/imgs/toolbar_ic_2c80ef5.png

#编辑txt文件
cat > /www/wangmingqu/ai.txt << EOF
阿伦·艾弗森(Allen Iverson),全名阿伦·伊宰尔·艾弗森(Allen Ezail Iverson)

1975年6月7日出生于美国弗吉尼亚州汉普顿,美国篮球运动员,司职后卫,绰号“答案(The Answer)”

曾任美国男篮梦之队队长。
EOF

编辑Nginx配置文件

路径:/etc/nginx/conf.d/wangmingqu.conf

server {
  #监听的端口
  listen 80;
  #网站匹配的域名
  server_name www.wangmingqu.com;
  #网站的字符集设置
  charset utf-8;

  #匹配“根”目录,即访问http://www.wangmingqu.com:80时匹配一下信息
  location / {
    #匹配成功以后查找“/www/wangmingqu/”目录下的内容
    root /www/wangmingqu/;
    #找到匹配路径以后,
    index index.html;
  }
}

重载Nginx

nginx -t
systemctl reload nginx

本地host配置

路径:C:\Windows\System32\drivers\etc\hosts
添加:192.169.1.128 www.wangmingqu.com

验证虚拟主机

image.png

基于多IP的虚拟主机

配置多网卡

#查看IP
ifconfig ens32
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.169.1.128  netmask 255.255.255.0  broadcast 192.169.1.255
        inet6 fe80::de68:16bc:e4f9:132  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:40:02:e7  txqueuelen 1000  (Ethernet)
        RX packets 45956  bytes 12918483 (12.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7903  bytes 865117 (844.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

#增加IP
ip addr add 192.169.1.126/24 dev ens32
ip addr add 192.169.1.127/24 dev ens32

测试数据

mkdir -p /www/{126,127}
echo "此页面为192.169.1.126" > /www/126/index.html
echo "此页面为192.169.1.127" > /www/127/index.html

编辑配置文件

路径:/etc/nginx/conf.d/ip_config.conf

server {
  listen 80;
  server_name 192.169.1.126;
  charset utf-8;
  
  location / {
    root /www/126/;
    index index.html;
  }
}

server {
  listen 80;
  server_name 192.169.1.127;
  charset utf-8;
  
  location / {
    root /www/127/;
    index index.html;
  }
}

重载Nginx

nginx -t
systemctl reload nginx

验证Nginx

image.png
image.png

基于多端口的虚拟主机

测试数据

mkdir -p /www/{81,82,83}

echo "81" > /www/81/index.html
echo "82" > /www/82/index.html
echo "83" > /www/83/index.html

编辑配置文件

路径:/etc/nginx/conf.d/ports.conf

server {
  listen 81;
  server_name 192.169.1.126;
  charset utf-8;

  location / {
    root /www/81/;
    index index.html;
  }
}

server {
  listen 82;
  server_name 192.169.1.126;
  charset utf-8;

  location / {
    root /www/82/;
    index index.html;
  }
}

server {
  listen 83;
  server_name 192.169.1.126;
  charset utf-8;

  location / {
    root /www/83/;
    index index.html;
  }
}

重载Nginx

nginx -t
systemctl reload nginx

验证Nginx

image.png
image.png
image.png

基于多域名的虚拟主机

测试数据

mkdir -p /www/name/{wang,ming,qu}

echo "王茗渠的王" > /www/name/wang/index.html
echo "王茗渠的茗" > /www/name/ming/index.html
echo "王茗渠的渠" > /www/name/qu/index.html

编辑配置文件

路径:/etc/nginx/conf.d/domean_name.conf

server {
  listen 80;
  server_name wang.wangmingqu.com;
  charset utf-8;

  location / {
    root /www/name/wang/;
    index index.html;
  }
}

server {
  listen 80;
  server_name ming.wangmingqu.com;
  charset utf-8;

  location / {
    root /www/name/ming/;
    index index.html;
  }
}

server {
  listen 80;
  server_name qu.wangmingqu.com;
  charset utf-8;

  location / {
    root /www/name/qu/;
    index index.html;
  }
}

本地host配置

路径:C:\Windows\System32\drivers\etc\hosts
添加:192.169.1.128 wang.wangmingqu.com ming.wangmingqu.com qu.wangmingqu.com

重载Nginx

nginx -t
systemctl reload nginx

验证Nginx

image.png
image.png
image.png

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值