标签:linux运维
nginx 的功能主配置文件(全局配置)
GeoIP的用法
虚拟主机
基于用户的访问控制
例4:
upstream模块也能为非http类的应用实现负载均衡,如下面的示例定义了nginx为memcached服务实现负载均衡
之目的。
传送client IP
Rewrite相关指令
Nginx Rewrite相关指令有if、rewrite、set、return等。 1、if指令 if 的语法 if (condition) { ... } 应用于server和
location环境内(if 与条件之间必须有空格) if 可以支持如下条件判断匹配符号 ~ 为区分大小写匹配 ~ 为不区分大
小写匹配 !~和!~ 分别为区分大小写不匹配及不区分大小写不匹配 -f 和!-f 用来判断是否存在文件 -d 和!-d 用来判断
是否存在目录 -e 和!-e 用来判断是否存在文件或目录 -x 和!-x 用来判断文件是否可执行
在匹配过程中可以引用一些Nginx的全局变量,更多的变量请参考 http://wiki.nginx.org/NginxHttpCoreModule
的 Variables 部分 $args 请求中的参数; $document_root 针对当前请求的根路径设置值; $host 请求信息中
的"Host",如果请求中没有Host行,则等于设置的服务器名; $limit_rate 对连接速率的限制; $request_method 请
求的方法,比如"GET"、"POST"等; $remote_addr 客户端地址; $remote_port 客户端端口号; $remote_user 客户
端用户名,认证用; $request_filename 当前请求的文件路径名(带root指定的路径,即网站的主目录)
$request_uri 当前请求的文件路径名(不带root指定的路径)
与 args相同;
$scheme 所用的协议,比
如http或者是https $server_protocol 请求的协议版本,"HTTP/1.0"或"HTTP/1.1"; $server_addr 服务器地址,如
果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费); $server_name 请
求到达的服务器名;$server_port 请求到达的服务器端口号;
与 uri一样,URI地址;
例1 匹配访问的url地址是否是个目录
if (-d $request_filename) {
...;
}
例2 匹配访问的地址是否以www开头
if ($host ~ ^www) {
...;
}
例3 防盗链
referer指令 Syntax: valid_referers none | blocked | server_names | string ...; Default: — Context: server,
location none the“Referer” field is missing in the request header; (即直接在客户端浏览器地址栏中输入的)
blocked the“Referer” field is present in the request header, but its value has been deleted by a firewall or
proxy server; such values are strings that do not start with “http://” or “https://”; server_names the “Referer”
request header field contains one of the server names; arbitrary string defines a server name and an optional
URI prefix. A server name can have an “” at the beginning or end. During the checking, the server’s port in the
“Referer” field is ignored; regular expression the first symbol should be a “~”. It should be noted that an
expression will be matched against the text starting after the “http://” or “https://”
Rewrite 指令 Syntax: rewrite regex replacement [flag]; Default: — Context: server, location, if flag: last stops
processing the current set of ngx_http_rewrite_module directives and starts a search for a new location
matching the changed URI; 相当于Apache里的[L]标记,表示完成rewrite break stops processing the current set
of ngx_http_rewrite_module directives as with the break directive; 本条规则匹配完成后,终止匹配,不再匹配后
面的规则 redirect returns a temporary redirect with the 302 code; used if a replacement string does not start
with “http://” or “https://”; 返回302临时重定向,浏览器地址会显示跳转后的URL地址 permanent returns a
permanent redirect with the 301 code.返回301永久重定向,浏览器地址会显示跳转后URL地址
last和break标记的区别在于,last标记在本条rewrite规则执行完后,会对其所在的server { ... } 标签重新发起请
求,而break标记则在本条规则匹配完成后,停止匹配,不再做后续的匹配。另有些时候必须使用last,比如在使用
alias指令时,而使用proxy_pass指令时则必须使用break。
例1:
例2:
例3:
标签:linux运维
原文地址:http://blog.51cto.com/13575680/2096225