Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26187629
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: 系统运维

2010-06-05 10:18:39

nginx配置虚拟主机

时间:2010-06-04

Nginx首先决定一个过来的请求由哪一个server来处理。

就是:我们打开HttpWatch看到的那个HOST值。

server {

        listen       80;

        server_name  nginx.org 

        ...

   }  

server {

        listen       80;

        server_name  nginx.net 

       ...

 }

server {

       listen       80;

       server_name  nginx.com 

       ...

 }

这样的话我们就可以配置三个域名。即同一个IP绑定三个域名。如果发现有一个域名均不匹配的话就定义出来一个默认的域名

server {

        listen       80 default_server;

        server_name  nginx.net  

       ...

 }

对于这种域名我们可以这样来处理

server {

        listen       80 default_server;

        server_name  ;     //这个值你得填写一个

        return      444;

 }

 

基于域名与IP混用的虚拟主机

server {

        listen       192.168.1.1:80;

        server_name  nginx.org 

        ...

   }  

server {

        listen       192.168.1.1:80;

        server_name  nginx.net 

       ...

 }

server {

       listen       192.168.1.2:80;

       server_name  nginx.com 

       ...

 }

 

至此配置完成了有关虚拟机的配置工作!

 

示例:

Server {

    Listen          80;

    Server_name     nginx.org   ;

    Root            /data/www;      //这个有点相当于resin里面的root目录

 

    Location    / {

        Index   index.html  index.php;

}

 

Location ~*\.(gif|jpg|png)$ {

    Expires 30d;

}

 

Location ~\.php$ {

    fastcgi_pass   localhost:9000;

       fastcgi_param  SCRIPT_FILENAME

$document_root$fastcgi_script_name;

include        fastcgi_params;

}

}

其中的location/ 表示的是它可以匹配任何请求的。

哦!原来location是用来检验URI的!

 

心得与笔记:

    我们的server是配置HOST的即主机。

    Location是配置URI的。

 

比如:http://www.sina.cn/blog/index.php  那这里面的HOST就是

URI就是我们的/blog/index.php值了。

 

一个“/logo.gif”请求会先和字符location“/”匹配,然后再和正则表达式“\.(gif|jpg|png)$”匹配, 因此,它是被字符location处理的。指令“root /data/www”会使该请求指向一个文件 “/data/www/logo.gif”,之后这个文件就会发送到客户端。

 

 

哦原来root的作用其实与resin里面的document-root是一个概念的!

 

 

一个 “/index.php”请求同样先被字符location “/” 匹配,然后才被正则表达式“\.(php)$”匹配。 所以, 它是被字符location所处理的,并且这请求是通过一个监听在localhost:9000FastCGI server被处理的. “fastcgi_param” 指令设置FastCGI的参数SCRIPT_FILENAME设置为“/data/www/index.php”, FastCGI server 执行这个文件. $document_root 变量的值等于 “root” 指令,$fastcgi_script_name 变量等于 URI 请求的值, 也就是 “/index.php”.

 

笔记:nginx是让客户端程序找到文件的目录位置。具体如何处理这个得让后端来处理的

 

 

一个 “/about.html”请求只被字符location“/”匹配, 所以,它被这个location处理。 使用“root /data/www” 指令的时候,该请求会被转到 “/data/www/about.html”, 并且文件会被发送到客户端。

 

 

明白了!

 

笔记:location是得讲个先后顺序才行的。即先由 location / 处理让客户端找到所需要的文件。然后再往下找看看是否还有匹配的location项如果像php文件就会有了!

丢给了一个FAST-CGI处理程序

 

 

 

 

总结:

心得与笔记:

    我们的server是配置HOST的即主机。多个域名就定义多个虚拟主机即可

    Location是配置URI的。

比如:http://www.sina.cn/blog/index.php  那这里面的HOST就是

URI就是我们的/blog/index.php值了。

 

 

Location是多方匹配的。示例:

Location    / {

        Index   index.html  index.php;

}

 

Location ~*\.(gif|jpg|png)$ {

    Expires 30d;

}

如果我请求一个abc.gif的话是先由第一个UIR定位找到图片位置再由第二个URI处理得到过期时间。

当然在location里面有以下几个选项。

1last 基本上用这个。表示已完成了rewrite不再匹配后面的规则了

2break    中止rewrite不再继续匹配

3redirect 返回临时重定向的HTTP状态302

4permanent    返回永久重定向的HTTP状态301

注意:原有的URL支持正则,重写的URL不支持正则

Location    / {

        Index   index.html  index.php;

        Break;

}

则后面的过期限制就不生效

 

手工测试一下:只处理静态文件的情况

站点目录:

虚拟主机1:目录放在D:\myweb\proj3 下面

虚拟主机2:目录放在D:\myweb\proj4 下面

server {

    listen 80; 

    server_name    

    root    D:\myweb\proj3;

    location / {       

       index index.html index.htm;

    }

    location ~*\.(gif|jpg|png)$ {

       expires  30d;

    }

    }

 server {

    listen 80; 

    server_name    

    root    D:\myweb\proj4;

    location / {       

       index index.html index.htm;

    }

    location ~*\.(gif|jpg|png)$ {

       expires  30d;

    }

}

OK!配置了两个虚拟主机了。到时只要域名一过来就可以解析。



补充

Vps 上安装了 nginx。用多个子域名,每个子域名到不同的目录。

如:

  1. http {  
  2.     server {  
  3.         listen 80;  
  4.         server_name a.chenlb.com;  
  5.         access_log logs/a.access.log main;  
  6.   
  7.         server_name_in_redirect off;  
  8.   
  9.         location / {  
  10.                 index index.html;  
  11.                 root /home/www/host_a/;  
  12.         }  
  13.     }  
  14.   
  15.     server {  
  16.         listen 80;  
  17.         server_name b.chenlb.com;  
  18.         access_log logs/b.access.log main;  
  19.   
  20.         server_name_in_redirect off;  
  21.   
  22.         location / {  
  23.                 index index.html;  
  24.                 root /home/www/host_b/;  
  25.         }  
  26.     }  
  27. }  

结果发现用 b.chenlb.com 还是指到 host_a 目录。后来看了官方示例:,提到有个 default 的匹配,如:

  1. http {  
  2.   server {  
  3.     listen          80 default;  
  4.     server_name     _;  
  5.     access_log      logs/default.access.log main;  
  6.   
  7.     server_name_in_redirect  off;  
  8.   
  9.     location / {  
  10.       index index.html;  
  11.       root  /var/www/default/htdocs;  
  12.     }  
  13.   }  
  14. }  

加上这个 default 就可使 a.chenlb.com 和 b.chenlb.com 正常工作了。

 

阅读(876) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~