nginx 常用配置说明

一、location 配置

1.1 语法规则: location [=|~|~*|^~] /uri/ { … }
= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
~ 开头表示区分大小写的正则匹配
~*  开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
例子,有如下匹配规则:

location = / {
   #规则A
}
location = /login {
   #规则B
}
location ^~ /static/ {
   #规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
   #规则D
}
location ~* \.png$ {
   #规则E
}
location !~ \.xhtml$ {
   #规则F
}
location !~* \.xhtml$ {
   #规则G
}
location / {
   #规则H
}

 


那么产生的效果如下:
访问根目录/, 比如http://localhost/ 将匹配规则A
访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
访问 http://localhost/static/a.html 将匹配规则C
访问 http://localhost/a.gifhttp://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C
访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。


所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则

location = / {
    proxy_pass http://tomcat:8080/index
}

# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项

# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用

location ^~ /static/ {
    root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}

 


#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
#非静态文件请求就默认是动态请求,自己根据实际把握
#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了
location / {
    proxy_pass http://tomcat:8080/
}

三、ReWrite语法

last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent –
返回永久重定向的HTTP状态301

1、下面是可以用来判断的表达式:
   -f和!-f用来判断是否存在文件
   -d和!-d用来判断是否存在目录
   -e和!-e用来判断是否存在文件或目录
    -x和!-x用来判断文件是否可执行
2、下面是可以用作判断的全局变量
    例:http://localhost:88/test1/test2/test.php

$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php

  3、按照匹配规则,将请求路径重定向。 

		
     	location /act/ {
		
            rewrite ^/act/(.*)$ /$1 break;
            proxy_set_header Host $host;  
            proxy_set_header X-Real-IP $remote_addr;   
            proxy_set_header X-Forwarded-For $remote_addr;#$proxy_add_x_forwarded_for;  
            proxy_pass http://rhythmk.com:7002;
            #proxy_redirect default;  
 
           
	  }

  

四、Redirect语法

server {
     listen 80;
     server_name start.igrow.cn;
     index index.html index.php;
     root html;
     if ($http_host !~ “^star\.igrow\.cn$&quot {
           rewrite ^(.*) http://star.igrow.cn$1 redirect;
    }
}

五、防盗链

location ~* \.(gif|jpg|swf)$ {
    valid_referers none blocked start.igrow.cn sta.igrow.cn;
    if ($invalid_referer) {
           rewrite ^/ http://$host/logo.png;
    }
}

六、根据文件类型设置过期时间

location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
    if (-f $request_filename) {
        expires 1h;
         break;
  }
}    

七、禁止访问某个目录

location ~* \.(txt|doc)${
      root /data/www/wwwroot/linuxtone/test;
      deny all;
}

 八、多个自动规则制定到同一代理:

    location ~*^(/wap|/w|/w1|/u|/get\-business\-scope)$ {
          proxy_pass http://127.0.0.1:8251;
    
   }

  /*

   等价于:
      = /wap
      = /w
     = /w1
     = /u
     = /get-business-scope
  */

  

二、常用全局变量

http://dwz.stamhe.com/index.php?_a=index&_m=show&count=10
remote_addr		客户端ip,如:192.168.4.2
binary_remote_addr	客户端ip(二进制)
remote_port		客户端port,如:50472
remote_user		已经经过Auth Basic Module验证的用户名
host			请求主机头字段,否则为服务器名称,如:dwz.stamhe.com
request			用户请求信息,如:GET /?_a=index&_m=show&count=10 HTTP/1.1
request_filename	当前请求的文件的路径名,由root或alias和URI request组合而成,如:/webserver/htdocs/dwz/index.php
status			请求的响应状态码,如:200
body_bytes_sent  	响应时送出的body字节数数量。即使连接中断,这个数据也是精确的,如:40
content_length	        请求头中的Content-length字段
content_type	        请求头中的Content-Type字段
http_referer	        引用地址
http_user_agent	        客户端agent信息,如:Mozilla/5.0 (Windows NT 6.1; WOW64)
AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11 args 如:_a=index&_m=show&count=10 document_uri 与$uri相同,如:/index.php document_root 针对当前请求的根路径设置值,如:/webserver/htdocs/dwz hostname 如:centos53.localdomain http_cookie 客户端cookie信息 cookie_COOKIE cookie COOKIE变量的值 is_args 如果有$args参数,这个变量等于”?”,否则等于”",空值,如? limit_rate 这个变量可以限制连接速率,0表示不限速 query_string 与$args相同,如:_a=index&_m=show&count=10 realpath_root 如:/webserver/htdocs/dwz request_body 记录POST过来的数据信息 request_body_file 客户端请求主体信息的临时文件名 request_method 客户端请求的动作,通常为GET或POST,如:GET request_uri 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。如:/index.php?_a=index&_m=show&count=10 scheme HTTP方法(如http,https),如:http uri 如:/index.php request_completion 如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty),如:OK server_protocol 请求使用的协议,通常是HTTP/1.0或HTTP/1.1,如:HTTP/1.1 server_addr 服务器地址,在完成一次系统调用后可以确定这个值,如:192.168.4.129 server_name 服务器名称,如:dwz.stamhe.com server_port 请求到达服务器的端口号,如:80

1、匹配URL路径规则说明

规则1:匹配请求地址以 /search 开头的URL。
location  ^~/search {
	    # 规则1
	 }

##### 规则2:匹配请求地址以 /search/ 开头的URL。

location  ^~/search/ {
	    # 规则2
	 }

2、代理站点的映射文件关系:

2.1 代理url ,不添加“/” ,如请求的路径http://127.0.0.1/search/index.php,将请求9108站点的文件路径: “{根目录}/search/index.php”。
location  ^~/search {
	   proxy_set_header Host $host;
	   # proxy_set_header ProxyAlias "search";
	   proxy_pass http://127.0.0.1:9108;
   }
2.2 代理url ,添加“/” ,如请求的路径http://127.0.0.1/search/index.php,将请求9108站点的文件路径:“{根目录}/index.php 文件,匹配最后一个“/”后的文件。
请求路径  http://127.0.0.1/search/abc/list  匹配 {根目录}/list

location ^~/search { proxy_set_header Host $host; # proxy_set_header ProxyAlias "search"; proxy_pass http://127.0.0.1:9108/; }
posted @ 2015-08-16 11:54  Rhythmk  阅读(453)  评论(0编辑  收藏  举报
Rhythmk 个人笔记