Nginx - 虚拟主机

1. 虚拟主机

​ 虚拟主机,在Web 服务中就是一个独立的网站,这个站点对应独立的域名(也可能是IP或者端口),具有独立的程序以及资源,可以独立的对外提供服务供用户访问

  • 在Nginx 中,使用一个server { } 标签来标识一个虚拟主机,一个web服务里可以有多个虚拟主机 标签时,也就是可以同时支持多个虚拟主机站点
  • 虚拟主机有三种类型:
    • 基于域名的主机
    • 基于端口的主机
    • 基于IP的虚拟主机

2. Nginx 虚拟主机——基于域名

1) 修改配置文件

  • domain-name: www.name1.com
  • domain-name: www.name2.com
  [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
   server {
        listen  80;
        server_name     www.name1.com;
        location / {
                root    /var/www/name1;
                index   index.html index.htm;
        }
    }
      server {
        listen  80;
        server_name     www.name2.com;
        location / {
                root    /var/www/name2;
                index   index.html index.htm;
        }
      }

2) 创建域名相关的网页根目录,以及编写网页

  • 创建网页根目录
[root@localhost ~]# mkdir /var/www/name1
[root@localhost ~]# mkdir /var/www/name2
  • 编写测试网页
[root@localhost ~]# echo " vhost www.name1.com " > /var/www/name1/index.html
[root@localhost ~]# echo " vhost www.name2.com " > /var/www/name2/index.html

3) 配置DNS

  • 因没有搭建DNS服务,所以使用本地Hosts 文件进行解析
[root@localhost ~]# echo "1.1.1.101 www.name1.com
1.1.1.101 www.name2.com" >> /etc/hosts

4) 检测Nginx配置,并重新启动

[root@localhost ~]# nginx  -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload

5) 访问域名进行访问

[root@localhost ~]# curl www.name1.com

 vhost www.name1.com 

[root@localhost ~]# curl www.name2.com

 vhost www.name2.com 

3. Nginx 虚拟主机——基于端口

1) 修改配置文件

  • port-1: 80
  • port-2: 8888
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

   server {

​        listen  80;

​        server_name     1.1.1.1;

​        location / {

​                root    /var/www/html;

​                index   index.html index.htm;}

   }

   server {

​        listen  8888;					# 修改为不同的端口

​        server_name     1.1.1.1;

​        location / {

​                root    /var/www/html_1;		# 修改加目录为 html_1

​                index   index.html index.htm;}
    }

2) 创建html_1目录以及编写网页

  • 创建网页根目录
mkdir /var/www/html_1
  • 编写测试内容
echo " vpors  1.1.1.1/8  post:8888 " > /var/www/html_1/index.html

3) 检查语法

[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

4) 重载Nginx

nginx -s reload

5) 验证测试

[root@localhost ~]# curl 1.1.1.1:8888
 vpors  1.1.1.1/8  post:8888 

4. Nginx 虚拟主机——基于IP

1) 修改配置文件

[root@localhost ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

​    server {

​        listen       80;					

​        server_name  localhost;

​        \#charset koi8-r;

​        \#access_log  logs/host.access.log  main;

​        location / {

​            root   html;

​            index  index.html index.htm;}

   }    								# 添加括号结束以上 server 模块

   server {								# 编写以下内容

​        listen  80;

​        server_name     1.1.1.1;

​        location / {

​                root    /var/www/html;

​                index   index.html index.htm;}

2) 创建网页根目录

mkdir -p /var/www/html
  • 编写测试网页

 echo " vhost 1.1.1.1/8" > /var/www/html/index.html

3) 检测配置文件语法

[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

4) 重载nginx

nginx -s reload

5) 测试验证

[root@localhost ~]# curl 1.1.1.1
 vhost 1.1.1.1/8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值