配置 示列

server {
        listen       80;
        server_name test.sohu.com;
        location / {
            root /opt/www/momentplus;
            index index.html;
        }
        location /momentplus {
            alias /opt/www/momentplus;
        }
        location /weixin {
            proxy_pass http://localhost:8081;
        }
    if ($http_user_agent ~ "(Android|UC|iPhone|Opera\sMini|MIDP-[0-9]|MIDP[0-9]|SymbianOS|Symbian\sOS|Windows\sCE|BlackBerry|^SonyEricsson|^Nokia|^NOKIA|UP\.Link|^SAMSUNG|^MOT-|^DoCoMo|UP\.Browser|^Mitsu|UCWEB|MAUI|^Palm|^Blazer|^BIRD\.E868)"){
        set $ismobile 2;
    }
    if ($http_accept ~ "(/vnd\.wap)"){
        set $ismobile 2;
    }
    if ($http_x_up_bearer_type ~ "(.+)"){
        set $ismobile 2;
    }
    if ($http_x_source_id ~ "(.+)"){
        set $ismobile 2;
    }
    if ($http_encryptmdn ~ "(.+)"){
        set $ismobile 2;
    }
    if ($http_via ~ "(wap.+WISG)"){
        set $ismobile 2;
    }
            #set plat
    if ($http_user_agent ~* "iPhone") {
        set $plat iphone;
    }
            if ($http_user_agent ~* "Android") {
        set $plat android;
    }
    #ad
    location ^~ /w/l {
        if ($query_string ~* "f=([\w]+)([&|]$)"){
                set $f $1;
        }
        #wap
        if ($ismobile = 2){
            rewrite ^ http://w.sohu.com/t2/download.do? redirect;
        }
        #pc
        rewrite ^ http://t.sohu.com/mphone/client?key=phone_search redirect;   
        #rewrite "^.*$" http://cam.sohu.com?abc=abc redirect;   
    }
    #dw
    location ^~ /w/d {
                #first from http_user_agent
        if ($plat = iphone){
            rewrite ^ http://itunes.apple.com/us/app/id378043162?mt=8 last;
        }
        set $t 41003;
        if ($query_string ~* "t=([\d]+)"){
                set $t $1;
        }
        if ($plat = android){
            rewrite ^ http://pp.mtc.sohu.com/mt/client/android/sohumblog_$t.apk? redirect;
        } 
                #secode from query_url
                if ( $query_string ~* "p=(iphone)(.*)$") {
                    rewrite ^ http://itunes.apple.com/us/app/id378043162?mt=8 redirect;
                }
                rewrite ^ http://pp.mtc.sohu.com/mt/client/android/sohumblog_$t.apk? redirect;
    }
}

一、nginx的Location可以有以下几个匹配:
1. =   严格匹配这个查询。如果找到,停止搜索。
2. ^~ 匹配路径的前缀,如果找到,停止搜索。
3. ~   为区分大小写的正则匹配  
4. ~* 为不区分大小写匹配

例子:

1、
location = / {

# 只匹配 / 查询。
[ configuration A ]

}

2、
location / {

# 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。
[ configuration B ]

}

3、

location ^~ /images/ {

# 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
[ configuration C ]

}

4、
location ~* \.(gif|jpg|jpeg)$ {
# 匹配任何已 gif、jpg 或 jpeg 结尾的请求。然而所有 /images/ 目录的请求将使用 Configuration C。
[ configuration D ]
}
例子请求:

1、/ -> configuration A

2、/documents/document.html -> configuration B

3、/images/1.gif -> configuration C

4、/documents/1.jpg -> configuration D
注意:按任意顺序定义这4个配置结果将仍然一样。
二、nginx alias
nginx貌似没有虚拟目录的说法,因为它本来就是完完全全根据目录来设计并工作的。

如果非要给nginx安上一个虚拟目录的说法,那就只有alias标签比较“像”,干脆来说说alias标签和root标签的区别吧。

最基本的区别:alias指定的目录是准确的,

root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录。另外,根据前文所述,使用alias标签的目录块中不能使用rewrite的break。
列:

location /abc/ {
alias /home/html/abc/;
}

在这段配置下,http://test/abc/a.html就指定的是/home/html/abc/a.html。这段配置亦可改成

location /abc/ {
root /home/html/;
}

这样,nginx就会去找/home/html/目录下的abc目录了,得到的结果是相同的。

但是,如果我把alias的配置改成:

location /abc/ {
alias /home/html/def/;
}

那么nginx将会从/home/html/def/取数据,这段配置还不能直接使用root配置,如果非要配置,只有在/home/html/下建立一个 def->abc的软link(快捷方式)了。

一般情况下
1、在location /中配置root,在location /other中配置alias是一个好习惯。
2、使用alias时目录名后面一定要加“/”

三、nginx配置文件中的参数变量及条件判断
正则表达式匹配,其中:
* ~ 为区分大小写匹配
* ~* 为不区分大小写匹配
* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配

文件及目录匹配,其中:
* -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行

flag标记有:
* last 相当于Apache里的[L]标记,表示完成rewrite
* break 终止匹配, 不再匹配后面的规则
* redirect 返回302临时重定向
* permanent 返回301永久重定向

一些可用的全局变量有,可以用做条件判断(待补全)
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
四、


猜你喜欢

转载自wanxw2001.iteye.com/blog/1665464