wheat 发表于 2016-12-25 09:21:59

nginx配置虚拟主机[整理]超详细

  Nginx首先决定一个过来的请求由哪一个server来处理。
就是:我们打开HttpWatch看到的那个HOST值。
server {
        listen       80;
        server_name  nginx.org  www.nginx.org;
        ...
   }  
server {
        listen       80;
        server_name  nginx.net  www.nginx.net;
       ...
 }
server {
       listen       80;
       server_name  nginx.com  www.nginx.com;
       ...
 }
这样的话我们就可以配置三个域名。即同一个IP绑定三个域名。如果发现有一个域名均不匹配的话就定义出来一个默认的域名
server {
        listen       80 default_server;
        server_name  nginx.net  www.nginx.net;
       ...
 }
对于这种域名我们可以这样来处理
server {
        listen       80 default_server;
        server_name  www.nginx.net;     //这个值你得填写一个
        return      444;
 }
 
基于域名与IP混用的虚拟主机
server {
        listen       192.168.1.1:80;
        server_name  nginx.org  www.nginx.org;
        ...
   }  
server {
        listen       192.168.1.1:80;
        server_name  nginx.net  www.nginx.net;
       ...
 }
server {
       listen       192.168.1.2:80;
       server_name  nginx.com  www.nginx.com;
       ...
 }
 
至此配置完成了有关虚拟机的配置工作!
 
示例:
Server {
    Listen          80;
    Server_name     nginx.org   www.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就是www.sina.cn
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:9000的FastCGI 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就是www.sina.cn
URI就是我们的/blog/index.php值了。
 
 
Location是多方匹配的。示例:
Location    / {
        Index   index.html  index.php;
}
 
Location ~*\.(gif|jpg|png)$ {
    Expires 30d;
}
如果我请求一个abc.gif的话是先由第一个UIR定位找到图片位置再由第二个URI处理得到过期时间。
当然在location里面有以下几个选项。
1、last 基本上用这个。表示已完成了rewrite不再匹配后面的规则了
2、break    中止rewrite不再继续匹配
3、redirect 返回临时重定向的HTTP状态302
4、permanent    返回永久重定向的HTTP状态301
注意:原有的URL支持正则,重写的URL不支持正则
Location    / {
        Index   index.html  index.php;
        Break;
}
则后面的过期限制就不生效
 
手工测试一下:只处理静态文件的情况
站点目录:
虚拟主机1:目录放在D:\myweb\proj3 下面
虚拟主机2:目录放在D:\myweb\proj4 下面
server {
    listen 80; 
    server_name www.aaa.com;   
    root    D:\myweb\proj3;
    location / {       
       index index.html index.htm;
    }
    location ~*\.(gif|jpg|png)$ {
       expires  30d;
    }
    }
 server {
    listen 80; 
    server_name www.bbb.com;   
    root    D:\myweb\proj4;
    location / {       
       index index.html index.htm;
    }
    location ~*\.(gif|jpg|png)$ {
       expires  30d;
    }
}
OK!配置了两个虚拟主机了。到时只要域名一过来就可以解析。
 
页: [1]
查看完整版本: nginx配置虚拟主机[整理]超详细