栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

特别实用的Nginx虚拟机

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

特别实用的Nginx虚拟机

特别实用的Nginx虚拟机
  • 前言
  • 一、Nginx虚拟主机
    • 1. 基于域名的nginx虚拟主机
      • 1.1 基于域名的nginx虚拟主机操作步骤
    • 2. 基于IP的nginx虚拟主机
      • 2.1 基于IP的nginx虚拟主机操作步骤
    • 3. 基于端口的nginx虚拟主机
      • 3.1 基于端口的nginx虚拟主机操作步骤
  • 总结


前言

通过配置虚拟主机可以充分地利用好现有资源,配置方式大致有基于域名、IP和端口三种配置方式。


一、Nginx虚拟主机 1. 基于域名的nginx虚拟主机 1.1 基于域名的nginx虚拟主机操作步骤

①为虚拟主机提供域名和IP的映射

echo "192.168.237.123 www.dj.com www.zs.com" >> /etc/hosts

②为虚拟主机准备网页文档

mkdir -p /var/www/html/dj/
mkdir -p /var/www/html/zs/
echo "www.dj.com" > /var/www/html/dj/index.html
echo "www.zs.com" > /var/www/html/zs/index.html

③修改nginx主配置文件

vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.zs.com;						#设置域名			
        charset utf-8;									#设置网页字符集
        access_log  logs/zs.com.access.log;			    #设置日志名
        location / {
            root   /var/www/html/zs;					#设置www.zs.com的工作目录
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;			
        server_name  www.dj.com;						#设置域名
        charset utf-8;									#设置网页字符集
        access_log  logs/dj.com.access.log;			#设置日志名
        location / {
            root   /var/www/html/dj;					#设置www.dj.com的工作目录
            index  index.html index.htm;
        }

④重启nginx后进行访问测试

systemctl restart nginx.service

浏览器访问http://www.dj.com
浏览器访问http://www.zs.com

2. 基于IP的nginx虚拟主机 2.1 基于IP的nginx虚拟主机操作步骤

①添加虚拟网卡,修改主配置文件

ifconfig ens33:0 192.168.237.124/24
vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       192.168.237.123:80;				#修改监听的为ip
        server_name  www.zs.com;
        charset utf-8;
        access_log  logs/zs.com.access.log;
        location / {
            root   /var/www/html/zs;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       192.168.237.124:80;				#修改监听的为ip
        server_name  www.dj.com;
        charset utf-8;
        access_log  logs/dj.com.access.log;
        location / {
            root   /var/www/html/dj;
            index  index.html index.htm;
        }

②检查配置文件,重启服务

nginx -t
systemctl restart nginx.service

③客户端上访问测试
浏览器访问http://192.168.237.123:80
浏览器访问http://192.168.237.124:80

3. 基于端口的nginx虚拟主机 3.1 基于端口的nginx虚拟主机操作步骤

①修改主配置文件

vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       192.168.237.123:666;				#修改监听的为ip的666端口
        server_name  www.zs.com;
        charset utf-8;
        access_log  logs/zs.com.access.log;
        location / {
            root   /var/www/html/zs;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       192.168.237.124:888;				#修改监听的为ip的888端口
        server_name  www.dj.com;
        charset utf-8;
        access_log  logs/dj.com.access.log;
        location / {
            root   /var/www/html/dj;
            index  index.html index.htm;
        }

②检查配置文件,重启服务

nginx -t
systemctl restart nginx.service

③客户端上访问测试
浏览器访问http://192.168.237.123:666
浏览器访问http://192.168.237.124:888


总结

虚拟主机的使用可以很好的节省资源

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/323673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号