nginx location
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 语法规则: 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.gif, http://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/ } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 三、ReWrite语法 last – 基本上都用这个Flag。 break – 中止Rewirte,不在继续匹配 redirect – 返回临时重定向的HTTP状态302 permanent – 返回永久重定向的HTTP状态301 1、下面是可以用来判断的表达式: -f和!-f用来判断是否存在文件 -d和!-d用来判断是否存在目录 -e和!-e用来判断是否存在文件或目录 -x和!-x用来判断文件是否可执行例如: if (!-f $request_filename){ proxy_pass http: //127.0.0.1; } 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.php3.用 if 进行条件匹配,语法规则类似 if ($http_user_agent~MSIE){ rewrite^(.*)$/msie/$1break; }4. if (!-e $request_filename){ rewrite^/(index|atom|rsd)\.xml$ http: //feed.shunz.net last; rewrite^([_0-9a-zA-Z-]+)?(/wp-.*)$2 last; rewrite^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last; rewrite^/index.php last; }if(!-f $request_filename){ rewrite^/archiver/((fid|tid)-[\w\-]+\.html)$/archiver/index.php?$1 last;rewrite^/forum-([0-9]+)-([0-9]+)\.html$/forumdisplay.php?fid=$1&page=$2 last; rewrite^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$/viewthread.php?tid=$1&extra=page%3D$3&page=$2 last; rewrite^/space-(username|uid)-(.+)\.html$/space.php?$1=$2 last; rewrite^/tag-(.+)\.html$/tag.php?name=$1 last; } 四、Redirect语法 server { listen 80; server_name start.igrow.cn; index index.html index.php; root html; if ($http_host !~ “^star\.igrow\.cn$" { 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; } ++ 一些可用的全局变量 $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里面配置 {m,n} 这样的正则规则的时候,条件必须加上双引号,否则总是报错无法通过,官方文档里面真是很难找到这些东西,很晕。
WordPress的重定向规 则:
Discuz!的 重定向规则:
首先Apache的 Rewite规则差别不是很大,但是Nginx的Rewrite规则比Apache的简单灵活多了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2015-03-04 kali安装vmware tools