nginxlocation与变量.doc_第1页
nginxlocation与变量.doc_第2页
nginxlocation与变量.doc_第3页
nginxlocation与变量.doc_第4页
nginxlocation与变量.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

nginx location与变量2010年6月23日 caixi 发表评论 阅读评论 Nginx Location基本语法location =|*| /uri/ = 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。 为区分大小写匹配* 为不区分大小写匹配!和!*分别为区分大小写不匹配及不区分大小写不匹配 如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式。例:location = / # 只匹配 / 查询。location / # 匹配任何查询,因为所有请求都已 / 开头。但正则表达式规则和长的块规则将被优先和查询匹配。location /images/ # 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。location * .(gif|jpg|jpeg)$ # 匹配任何已 gif、jpg 或 jpeg 结尾的请求。+ 文件及目录匹配* -f和!-f用来判断是否存在文件* -d和!-d用来判断是否存在目录* -e和!-e用来判断是否存在文件或目录* -x和!-x用来判断文件是否可执行+ 一些可用的全局变量$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$uriVariablesThe core module supports built-in variables, whose names correspond with the names of variables in Apache.First of all, there are the variables, which represent the lines of the title of the client request, for example, $http_user_agent, $http_cookie, and so forth.Furthermore, there are other variables:Edit section: $arg_PARAMETER $arg_PARAMETERThis variable contains the value of the GET request variable PARAMETER if present in the query stringEdit section: $args $argsThis variable is equal to arguments in the line of request;Edit section: $binary_remote_addr $binary_remote_addrThe address of the client in binary form;Edit section: $body_bytes_sent $body_bytes_sent(undocumented)Edit section: $content_length $content_lengthThis variable is equal to line Content-Length in the header of request;Edit section: $content_type $content_typeThis variable is equal to line Content-Type in the header of request;Edit section: $cookie_COOKIE $cookie_COOKIEThe value of the cookie COOKIE;Edit section: $document_root $document_rootThis variable is equal to the value of directive root for the current request;Edit section: $document_uri $document_uriThe same as $uri.Edit section: $host $hostThis variable is equal to line Host in the header of request or name of the server processing the request if the Host header is not available.This variable may have a different value from $http_host when the Host input header is absent or has an empty value.Edit section: $http_HEADER $http_HEADERThe value of the HTTP header HEADER when converted to lowercase and with dashes converted to underscores, e.g. $http_user_agent, $http_referer;Edit section: $is_args $is_argsEvaluates to “?” if $args is set, “” otherwise.Edit section: $limit_rate $limit_rateThis variable allows limiting the connection rate.Edit section: $query_string $query_stringThe same as $args.Edit section: $remote_addr $remote_addrThe address of the client.Edit section: $remote_port $remote_portThe port of the client;Edit section: $remote_user $remote_userThis variable is equal to the name of user, authenticated by the Auth Basic Module;Edit section: $request_filename $request_filenameThis variable is equal to path to the file for the current request, formed from directives root or alias and URI request;Edit section: $request_body $request_bodyThis variable(0.7.58+) contains the body of the request. The significance of this variable appears in locations with directives proxy_pass or fastcgi_pass.Edit section: $request_body_file $request_body_fileClient request body temporary filename;Edit section: $request_completion $request_completion(undocumented)Edit section: $request_method $request_methodThis variable is equal to the method of request, usually GET or POST.Before and including 0.8.20, this variable always evaluates to the method name of the main request, not the current request if the current request is a subrequest.Edit section: $request_uri $request_uriThis variable is equal to the complete initial URI together with the arguments;Edit section: $scheme $schemeThe HTTP scheme (i.e. http, https). Evaluated only on demand, for example:rewrite (.+)$ $scheme:/$1 redirect;Edit section: $server_addr $server_addrEqual to the server address. As a rule, for obtaining the value of this variable is done one system call. In order to avoid system call, it is necessary to indicate addresses in directives listen and to use parameter bind.Edit section: $server_name $server_nameThe name of the server.Edit section: $server_port $server_portThis variable is equal to the port of the server, to which the request arrived;Edit section: $server_protocol $server_protocolThis variable is equal to the protocol of request, usually this HTTP/1.0 or HTTP/1.1.Edit section: $uri $uriThis variable is equal to current URI in the request, it can differ from initial, for example by internal redirects, or with the use of index it is file with internal redirects.参考:/entry/Nginx-Location%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95/NginxHttpCoreModule#Variables个人中心我的主页好友消息 站内消息 :无新消息提醒互动请求 :无新消息提醒系统通知 :无新消息提醒sias00|装扮|设置| 退出蜂巢are you ok ?主页博客相册|个人档案|好友|i贴吧 查看文章Nginx 常见应用技术指南(Nginx Tips)2008-12-17 15:01目录:一. Nginx基础知识二. Nginx安装配置三. Nginx Rewrite四. Nginx Redirect五. Nginx 目录自动加斜线六. Nginx 防盗链七. Nginx expires八. Nginx 访问控制九. Nginx Location十. Nginx 日志处理十一. Nginx Cache服务配置十二. Nginx 负载均衡十三. Nginx 优化十四. Nginx 相关参考文档【前言】:编写此技术指南在于推广普及NGINX在国内的使用,更方便的帮助大家了解和掌握NGINX的一些使用技巧。本指南很多技巧来自于网络在此对网络上愿意分享的朋友们表示感谢和致意!欢迎大家和我一起丰富本技术指南并提出更好的建议!一Nginx 基础知识1.简介Nginx (engine x) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。更多的请见官方wiki: /NginxChs2.Nginx的优点 nginx做为HTTP服务器,有以下几项基本特性: 1.处理静态文件,索引文件以及自动索引;打开文件描述符缓冲 2.无缓存的反向代理加速,简单的负载均衡和容错3.FastCGI,简单的负载均衡和容错4.模块化的结构。包括gzipping, byte ranges, chunked responses, 以及 SSI-filter等filter。如果由FastCGI或其它代理服务器处理单页中存在的多个SSI,则这项处理可以并行运行,而不需要相互等待。5.支持SSL 和 TLS SNINginx专为性能优化而开发,性能是其最重要的考量, 实现上非常注重效率 。它支持内核Poll模型,能经受高负载的考验, 有报告表明能支持高达 50,000 个并发连接数。Nginx具有很高的稳定性。其它HTTP服务器,当遇到访问的峰值,或者有人恶意发起慢速连接时,也很可能会导致服务器物理内存耗尽频繁交换,失去响应,只能重启服务器。例如当前apache一旦上到200个以上进程,web响应速度就明显非常缓慢了。而Nginx采取了分阶段资源分配技术,使得它的CPU与内存占用率非常低。nginx官方表示保持10,000个没有活动的连接,它只占2.5M内存,所以类似DOS这样的攻击对nginx来说基本上是毫无用处的。就稳定性而言, nginx比lighthttpd更胜一筹。Nginx支持热部署。它的启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在不间断服务的情况下,对软件版本进行进行升级。Nginx采用master-slave模型, 能够充分利用SMP的优势,且能够减少工作进程在磁盘I/O的阻塞延迟。当采用select()/poll()调用时,还可以限制每个进程的连接数。Nginx代码质量非常高,代码很规范, 手法成熟, 模块扩展也很容易。特别值得一提的是强大的Upstream与Filter链。 Upstream为诸如reverse proxy,与其他服务器通信模块的编写奠定了很好的基础。而Filter链最酷的部分就是各个filter不必等待前一个filter执行完毕。它可以把前一个filter的输出做为当前filter的输入,这有点像Unix的管线。这意味着,一个模块可以开始压缩从后端服务器发送过来的请求,且可以在模块接收完后端服务器的整个请求之前把压缩流转向客户端。Nginx采用了一些os提供的最新特性如对sendfile (Linux 2.2+),accept-filter (FreeBSD 4.1+),TCP_DEFER_ACCEPT (Linux 2.4+) 的支持,从而大大提高了性能。二. Nginx 安装配置1.安装pcre代码:Copy to clipboard - CODE:./configure make & make install cd ./3.nginx 编译安装代码:Copy to clipboard - CODE:./configure -user=www -group=www -prefix=/usr/local/nginx/ -with-http_stub_status_module -with-openssl=/usr/local/openssl make & make install更详细的模块定制与安装请参照官方wiki.三. Nginx Rewrite1.Nginx Rewrite 基本标记(flags)复制内容到剪贴板代码:last - 基本上都用这个Flag。break - 中止Rewirte,不在继续匹配redirect - 返回临时重定向的HTTP状态302permanent - 返回永久重定向的HTTP状态3012. 正则表达式匹配,其中:代码:Copy to clipboard - CODE:* 为区分大小写匹配 * * 为不区分大小写匹配* !和!*分别为区分大小写不匹配及不区分大小写不匹配3. 文件及目录匹配,其中:代码:Copy to clipboard - CODE:* -f和!-f用来判断是否存在文件 * -d和!-d用来判断是否存在目录* -e和!-e用来判断是否存在文件或目录* -x和!-x用来判断文件是否可执行4.Nginx 的一些可用的全局变量,可用做条件判断:代码:Copy to clipboard - CODE:$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四Nginx Redirect将所有与域名全部自跳转到代码:Copy to clipboard - CODE:server listen 80; server_name ; index index.html index.php; root /data/www/wwwroot; if ($http_host ! $) rewrite (.*) url/url$1 redirect; . 五Nginx 目录自动加斜线:代码:Copy to clipboard - CODE:if (-d $request_filename) rewrite /(.*)(/)$ http:/$host/$1$2/ permanent; 六Nginx 防盗链代码:Copy to clipboard - CODE:#Preventing hot linking of images and other file types location * .+.(gif|jpg|png|swf|flv|rar|zip)$ valid_referers none blocked server_names *. http:/localhost ;if ($invalid_referer) rewrite / img/images/default/logo.gif/img; # return 403; 七Nginx expires1 根据文件类型expires代码:Copy to clipboard - CODE:# Add expires header for static content location * .(js|css|jpg|jpeg|gif|png|swf)$ if (-f $request_filename) root /data/www/wwwroot/bbs; expires 1d; break;2根据判断某个目录代码:Copy to clipboard - CODE:# serve static files location /(images|javascript|js|css|flash|media|static)/ root /data/www/wwwroot/down; expires 30d; 八Nginx 访问控制1.Nginx 身份证验证代码:Copy to clipboard - CODE:#cd /usr/local/nginx/conf #mkdir htpasswd/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji linuxtone #添加用户名为linuxtoneNew password: (此处输入你的密码)Re-type new password: (再次输入你的密码)Adding password for userurl/tongji/data/index.html/url(目录存在/data/www/wwwroot/tongji/data/目录下)将下段配置放到虚拟主机目录,当访问urlhttp:/count.linuxtone/tongji/url即提示要密验证:location /(tongji)/ root /data/www/wwwroot/count; auth_basic LT-COUNT-TongJi; auth_basic_user_file /usr/local/nginx/conf/htpasswd/tongji; 2.Nginx 禁止访问某类型的文件.如,Nginx下禁止访问*.txt文件,配置方法如下.代码:Copy to clipboard - CODE:location * .(txt|doc)$ if (-f $request_filename) root /data/www/wwwroot/linuxtone/test;break;方法2:代码:Copy to clipboard - CODE:location * .(txt|doc)$ root /data/www/wwwroot/linuxtone/test; deny all;禁止访问某个目录代码:Copy to clipboard - CODE:location /(WEB-INF)/ deny all;3.使用ngx_http_access_module限制ip访问代码:Copy to clipboard - CODE:location / deny ;allow /24;allow /16;deny all;详细参见wiki: /NginxHttpAccessModule#allow4.Nginx 下载限制并发和速率代码:Copy to clipboard - CODE:limit_zone one $binary_remote_addr 10m; server listen 80; server_name ; index index.html index.htm index.php; root /data/www/wwwroot/down; #Zone limit location / limit_conn one 1; limit_rate 20k; . 5. Nginx 实现Apache一样目录列表代码:Copy to clipboard - CODE:location / autoindex on;九Nginx Location 1基本语法:和上面rewrite正则匹配语法基本一致代码:Copy to clipboard - CODE:location =|*| /uri/ * 为区分大小写匹配* * 为不区分大小写匹配* !和!*分别为区分大小写不匹配及不区分大小写不匹配示例1:代码:Copy to clipboard - CODE:location = / # matches the query / only.# 只匹配 / 查询。匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配示例2:代码:Copy to clipboard - CODE:location /images/ # matches any query beginning with /images/ and halts searching,# so regular expressions will not be checked.# 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。示例3:代码:Copy to clipboard - CODE:location * .(gif|jpg|jpeg)$ # matches any request ending in gif, jpg, or jpeg. However, all# requests to the /images/ directory will be handled by# 匹配任何已 gif、jpg 或 jpeg 结尾的请求。十Nginx 日志处理1.Nginx 日志切割代码:Copy to clipboard - CODE:#contab -e 59 23 * * * /usr/local/sbin/logcron.sh /dev/null 2&1rootcount # cat /usr/local/sbin/logcron.sh代码:Copy to clipboard - CODE:#!/bin/bash log_dir=/data/logstime=date +%Y%m%d/bin/mv $log_dir/access_.log $log_dir/access_.$time.logkill -USR1 cat /var/run/nginx.pid更多的日志分析与处理就关注(同时欢迎你参加讨论):/forum-8-1.html2.Nginx 如何不记录部分日志日志太多,每天好几个G,少记录一些,下面的配置写到server段中就可以了代码:Copy to clipboard - CODE:location .*.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ access_log off;十一Nginx Cache服务配置如果需要将文件缓存到本地,则需要增加如下几个子参数:代码:Copy to clipboard - CODE:proxy_store on; proxy_store_access user:rw group:rw all:rw;proxy_temp_path 缓存目录;其中,proxy_store on用来启用缓存到本地的功能,proxy_temp_path用来指定缓存在哪个目录下,如:proxy_temp_path html;在经过上一步配置之后,虽然文件被缓存到了本地磁盘上,但每次请求仍会向远端拉取文件,为了避免去远端拉取文件,必须修改proxy_pass:代码:Copy to clipboard - CODE:if ( !-e $request_filename) proxy_pass http:/mysvr;即改成有条件地去执行proxy_pass,这个条件就是当请求的文件在本地的proxy_temp_path指定的目录下不存在时,再向后端拉取。十二Nginx 负载均衡1. Nginx 基础知识nginx的upstream目前支持4种方式的分配1)、轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。2)、weight指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。2)、ip_hash每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。3)、fair(第三方)按后端服务器的响应时间来分配请求,响应时间短的优先分配。4)、url_hash(第三方)3.Nginx 负载均衡实例1代码:Copy to clipboard - CODE:upstream #定义负载均衡设备的Ip及设备状态 server :9090 down;server :8080 weight=2;server :6060;server :7070 backup;在需要使用负载均衡的server中增加代码:Copy to clipboard - CODE:proxy_pass url/url;每个设备的状态设置为:代码:Copy to clipboard - CODE:1.down 表示单前的server暂时不参与负载 2.weight 默认为1.weight越大,负载的权重就越大。3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误4.fail_timeout:max_fails次失败后,暂停的时间。5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。nginx支持同时设置多组的负载均衡,用来给不用的server来使用。client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debugclient_body_temp_path 设置记录文件的目录 可以设置最多3层目录location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡4.Nginx 负载均衡实例 2按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效,也可以用作提高Squid缓存命中率.简单的负载均等实例:#vi nginx.conf /nginx主配置文件核心配置代码:Copy to clipboard - CODE:. #loadblance upstream ip_hash; server :8080; server 36:8080; server 38:8080; server 17; server 18; server 19; .include vhosts/linuxtone_lb.conf;#vi proxy.confproxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;client_max_body_size 50m;client_body_buffer_size 256k;proxy_connect_timeout 30;proxy_send_timeout 30;proxy_read_timeout 60;proxy_buffer_size 4k;proxy_buffers 4 32k;proxy_busy_buffers_size 64

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论