`

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

阅读更多

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

 

<一>. 虚拟目录:

  通过使用index选项可以指定相对路径,root选项指定的路径和用户的请求路径拼接成一个完成的Linux文件路径。使用alias选项可以指定一个Linux系统的绝对路径。

 1. root选项:静态文件的路径为/software/html/index.html  

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

  2. alias选项:静态文件的路径为/software/html/index.html  

    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文件追加   

  192.168.142.56  www.excelsoft.com excelsoft.com blog.excelsoft.com www.blog.com image.excelsoft.com

    Linux平台:编辑/etc/hosts文件,给本机追加多个域名。 

  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

   server {
       listen       80;
       server_name  image.excelsoft.com;

       location / {
           root  /software/image;
       }

       access_log  off;
   }

            第二台虚拟主机:excelsoft域名,及其二级域名blog;但是不包括image.excelsoft.com域名,

 因为二级域名image按照书写顺序和第一台虚拟主机相匹配。  

 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;
   }

         第三台虚拟主机: 

  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

 

 

2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics