深入浅出Nginx之三【虚拟目录与虚拟主机】

下面简单介绍Nginx的虚拟目录和虚拟主机的配置,通过虚拟目录的设置可以直接访问Linux系统下面的静态资源文件,通过虚拟主机的设置可以在一台机器上模拟出多个逻辑上互不相干的独立主机。吻

<一>. 虚拟目录:
通过使用index选项可以指定相对路径,root选项指定的路径和用户的请求路径拼接成一个完成的Linux文件路径。使用alias选项可以指定一个Linux系统的绝对路径。
1. root选项:静态文件的路径为/software/html/index.html
Shell代码 收藏代码
location /html {
root /software;
index index.html index.htm;
}
2. alias选项:静态文件的路径为/software/html/index.html
Shell代码 收藏代码
location /html {
alias /software/html;
index index.html index.htm;
}

<二>. 虚拟主机:Virtual Host大笑
1. 简介:虚拟主机是一种特殊的软硬件技术,允许将一台物理机器“虚拟”成多个完全独立的主机。
Nginx可以使用多种方式配置虚拟主机,下面仅介绍基于域名的配置方式。
需要进行DNS配置,一个物理主机设置多个域名,然后配置Nginx,让其识别不同的域名。
2. 进行DNS的设置:
Windows平台:C:\WINDOWS\system32\drivers\etc下面的hosts文件追加
Shell代码 收藏代码
192.168.142.56 www.excelsoft.com excelsoft.com blog.excelsoft.com www.blog.com image.excelsoft.com
Linux平台:编辑/etc/hosts文件,给本机追加多个域名。
Shell代码 收藏代码
127.0.0.1 localhost ... www.excelsoft.com excelsoft.com blog.excelsoft.com www.blog.com image.excelsoft.com
3. 给nginx.conf配置虚拟主机:一共设置了3台虚拟主机,都在80端口进行监听。
第一台虚拟主机:独立的图片image域名,关闭日志功能off
Shell代码 收藏代码
server {
listen 80;
server_name image.excelsoft.com;

location / {
root /software/image;
}

access_log off;
}
第二台虚拟主机:excelsoft域名,及其二级域名blog;但是不包括image.excelsoft.com域名,
因为二级域名image按照书写顺序和第一台虚拟主机相匹配。
Shell代码 收藏代码
server {
listen 80;
server_name www.excelsoft.com excelsoft.com *.excelsoft.com;

location / {
root /software/html;
index index.html index.htm;
}

access_log /opt/nginx/logs/access.excelsoft.log access;
}
第三台虚拟主机:
Shell代码 收藏代码
server {
listen 80;
server_name www.blog.com;

location / {
root /software/blog;
index index.html index.htm;
}

access_log /opt/nginx/logs/access.blog.log access;
}

<三>. 进行测试:su - root
配置校验:/usr/local/nginx/sbin/nginx -t
平滑重启:kill -HUP `cat /opt/nginx/logs/nginx.pid`
测试地址:皱眉
http://image.excelsoft.com/hello.gif
http://www.excelsoft.com
http://blog.excelsoft.com
http://www.blog.com
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值