Nginx配置文件说明

来源:互联网 发布:在vb集成环境中要添加 编辑:程序博客网 时间:2024/06/05 00:23
Nginx可以基于server_name, port, ip来匹配虚拟主机。
首先通过server_name区分
以下三个服务器均监听80端口,依靠HTTP请求里的HOST来区分
{% highlight bash %}
server {
    listen 80;
    server_name yourname.com www.yourname.com;
    ...
}
{% endhighlight %}
如果HOST没有对应的服务器或者没有HOST,那么交给default server。
{% highlight bash %}
# 配置一个default server
server {
    listen 80 default_server;
    server_name yourname.com www.yourname;
    ...
}
{% endhighlight %}

同时使用ip和名字来定义虚拟主机
{% highlight bash %}
server {
    listen 172.16.96.195;
    server_name yourname.com www.yourname.com;
    ...
}
{% endhighlight %}

默认服务器也可以有多个,根据ip:port区分,例如:
{% highlight bash %}
server {
    listen 172.16.96.195:8080;
    server_name yourname.com www.yourname.com;
    ...
}
{% endhighlight %}

location指令,看一个例子
{% highlight bash %}
server {
    listen 80;
    server_name yourname.com www.yourname.com;
    root /data/htdoc/www;

    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;
    }
}
{% endhighlight %}

location使用<span style="color:red">pcre</span>的正则表达式引擎来匹配请求地址,并进行相应处理。
location \作为特殊的规则,任何请求的uri都匹配他,但只有在不匹配其他location的正则表达式时才会查看它里边的规则。
除此之外,其他的location匹配从上到下,有一个匹配符合之后会停止查找,nginx使用该location定义的规则进行处理。
location只会检查请求的uri部分,不会检查参数(请求中"?"及其之后的部分)。
例如:
请求“/logo.gif” 匹配了"\"和"\.(gif|jpg|png)$",它使用了第二个的规则处理,然后在“rootata/htdoc/www”影响下,映射到/dat/htdoc/www/logo.gif。此文件被返回到client。
请求“/index.php”匹配了“\”和“\.(php)$”,然后被传递给fastcgi localhost:9000。fastcgi_param指令将SCRIPT_FILENAME"/data/htdoc/www/index.php"
请求“/about.html”只匹配“\”,于是他被映射到/dhtdoc/ata/www/about.html,文件被发回给client。
请求“/”只匹配“\”,因为是目录,找到location \的index指令,映射为“/index.php”。然后再次匹配,依据“\.(php)$”的指令交给fastcgi。

OK,今天就到这里。
0 0
原创粉丝点击