三、nginx配置虚拟主机

 什么是虚拟主机:

答:虚拟主机指一台物理服务器划分为多个“虚拟”的服务器,这样就可以将一台物理服务器当做多个服务器来使用,从而可以配置多个网站。

nginx虚拟主机分为三种:

1. 基于域名的虚拟主机

2. 基于端口的虚拟主机

3. 基于ip的虚拟主机

一、配置基于域名的虚拟主机

1. 创建nginx发布路径/nginx

mkdir /nginx

2. 修改目录的属主属组

chown nginx:nginx /nginx

3. 创建一个html文件用于测试

echo "<h1>this is nginx yuming test </h1>" > /nginx/nginxyuming.html

4. 修改winds的hosts解析文件

#hosts位置
C:\Windows\System32\drivers\etc\hosts
#添加解析
192.168.245.66 www.nginxyuming.com

5. 修改nginx配置文件

vim /usr/local/nginx/conf/nginx.conf
#修http段种的server段内容   
    server {
        listen       80;
        server_name  www.nginxyuming.com;

        charset utf-8;

        access_log  /usr/local/nginx/logs/nginxyuming.log  ;

        location / {
            root   /nginx/ ;
            index  nginxyuming.html ;
        }

二、配置基于ip的虚拟主机

1. 修改html文件,便于确认

echo "<h1>this is nginx ip test </h1>" > /nginx/nginxyuming.html

2. 编辑配置文件 

vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       192.168.245.66:80;
        server_name  192.168.245.66;

        charset utf-8;

        access_log  /usr/local/nginx/logs/nginxyuming.log  ;

        location / {
            root   /nginx/ ;
            index  nginxyuming.html ;
        }

三、配置基于端口的虚拟主机

1. 修改html文件,便于确认

echo "<h1>this is nginx port 1234 test </h1>" > /nginx/nginxyuming.html

2. 编辑配置文件 

vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       192.168.245.66:1234;
        server_name  192.168.245.66;

        charset utf-8;

        access_log  /usr/local/nginx/logs/nginxyuming.log  ;

        location / {
            root   /nginx/ ;
            index  nginxyuming.html ;
        }

注:可以写多个虚拟主机,在http段种添加server段和location段

四、多台虚拟主机配置

注:本文演示,基于ip的多台虚拟主机

1. 配置网卡临时接口

ifconfig ens33:0 192.168.245.88/24

​​​​​​​

 2.创建/web目录并在/nginx和/web目录中添加html文件来区分两个主机

mkdir /web
chown nginx:nginx /web
echo "<h1>this is nginx ip 192.168.245.66 test </h1>" > /nginx/nginxyuming.html
echo "<h1>this is nginx ip 192.168.245.88 test </h1>" > /web/web.html

 3. 编辑配置文件

vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       192.168.245.66:80;
        server_name  192.168.245.66;

        charset utf-8;

        access_log  /usr/local/nginx/logs/nginxyuming-66.log  ;

        location / {
            root   /nginx/ ;
            index  nginxyuming.html ;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

    server {
        listen       192.168.245.88:80;
        server_name  192.168.245.88;

        charset utf-8;

        access_log  /usr/local/nginx/logs/nginxyuming-88.log  ;

        location / {
            root   /web/ ;
            index  web.html ;
        }
        #error_page  404              /404.html;
        #
        #        # redirect server error pages to the static page /50x.html
        #                
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

​​​​​​​

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微风◝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值