Nginx location匹配顺序如下:

  1. 用前缀字符串定义的location规则对URI进行匹配测试。
  2. =号定义了精确的前缀字符串匹配,如果发现精确匹配则使用当前规则。否则继续下一步匹配。
  3. 匹配其它普通字符串,并存储最长匹配。如果匹配以^~开始的规则,则使用当前匹配,否则继续下一步匹配。
  4. 按顺序对URI进行正则规则匹配,发现匹配后停止并使用当前匹配。若所有正则都不匹配,则使用第3步存储的最长匹配规则。
  • ~ 开头表示区分大小写的正则匹配;
  • ~* 开头表示不区分大小写的正则匹配

整体匹配优先级 =精确匹配 >  ^~前缀匹配 > 正则匹配 > 普通前缀字符串匹配

rewrite块可直接放在server段内,也可置于location段内。请求到达nginx后,URI会进行如下处理:

URI->server rewrite->new URI->location匹配

在location规则匹配过程中若对url进行了重写,则要重新开始规则匹配。若循环10次后仍没有找到真实存在的文件,服务器会返回500错误。

rewrite指令可以附带一个标志位 last/break;对此,我的理解是两者都会终止rewrite的执行,last一般用在server段,break一般用在location内。last执行完还要进行location匹配,而break则不再进行location匹配。

基于CI框架的nginx配置(windows环境):

  1. #user nobody;
  2. worker_processes 1;
  3.  
  4. #error_log logs/error.log;
  5. #error_log logs/error.log notice;
  6. #error_log logs/error.log info;
  7.  
  8. #pid logs/nginx.pid;
  9.  
  10. events {
  11. worker_connections 1024;
  12. }
  13.  
  14. http {
  15. include mime.types;
  16. default_type application/octet-stream;
  17.  
  18. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  19. '$status $body_bytes_sent "$http_referer" '
  20. '"$http_user_agent" "$http_x_forwarded_for"';
  21.  
  22. #access_log logs/access.log main;
  23. #开启rewrite日志 测试用 rewrite日志写在error_log里
  24. rewrite_log on;
  25. error_log logs/error.log notice;
  26. sendfile on;
  27. #tcp_nopush on;
  28.  
  29. #keepalive_timeout 0;
  30. keepalive_timeout 65;
  31. #tcp_nodelay on;
  32. fastcgi_connect_timeout 300;
  33. fastcgi_send_timeout 300;
  34. fastcgi_read_timeout 300;
  35. fastcgi_buffer_size 128k;
  36. fastcgi_buffers 4 128k;
  37. fastcgi_busy_buffers_size 256k;
  38. fastcgi_temp_file_write_size 256k;
  39.  
  40. #gzip on;
  41. gzip on;
  42. gzip_min_length 1k;
  43. gzip_buffers 4 32k;
  44. gzip_http_version 1.1;
  45. gzip_comp_level 2;
  46. gzip_types text/plain application/x-javascript text/css application/xml;
  47. gzip_vary on;
  48. gzip_disable "MSIE [1-6].";
  49.  
  50. server_names_hash_bucket_size 128;
  51. client_max_body_size 100m;
  52. client_header_buffer_size 256k;
  53. large_client_header_buffers 4 256k;
  54.  
  55. server {
  56. listen 80;
  57. server_name citest.com;
  58. index index.html index.php;
  59. root "d:/www/ci";
  60. access_log logs/ciaccess.log main;
  61. if (!-e $request_filename) {
  62. rewrite ^/(.*)$ /index.php/$1 last;
  63. }
  64.  
  65. location ~ \.php(.*)$ {
  66. fastcgi_pass 127.0.0.1:9000;
  67. fastcgi_index index.php;
  68. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  69. fastcgi_param PATH_INFO $fastcgi_path_info;
  70. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  71. include fastcgi_params;
  72. }
  73.  
  74. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
  75. expires 30d;
  76. }
  77.  
  78. location ~ .*\.(js|css)?$ {
  79. expires 12h;
  80. }
  81. }
  82.  
  83. #include vhosts.conf;
  84. }

nginx location匹配顺序及CI框架的nginx配置的更多相关文章

  1. Nginx location 匹配顺序整理

    Nginx location模块整理 具体的Nginx安装就不在这里描述了,这里只是为了对location的描述 Nginx环境 a. 查看当前系统cat /etc/redhat-release [r ...

  2. Nginx Location匹配顺序

    理论部分 文字释义匹配规则如下: 略述: 1.nginx服务器首先在server块的多个location块中搜索是否有标准的uri和请求字符串匹配.如果有多个标准uri可以匹配,就匹配其中匹配度最高的 ...

  3. nginx location 匹配顺序

    location 匹配的原型是这样的:location [=|~|~*|^~|@] /uri/ { … } “=”是精确匹配“@”是命名的location ,在正常的location 匹配中不会使用, ...

  4. CI框架伪静态化配置

    CI框架伪静态化配置 伪静态化,即:去掉入口的index.php, 在url后面加上 .html 后缀 CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/ ...

  5. nginx中location匹配顺序

    一.location语法 语法: Syntax: location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } Default: - ...

  6. Nginx location 匹配规则详解

    语法规则 location [=|~|~*|^~] /uri/ { … } 模式 含义 location = /uri = 表示精确匹配,只有完全匹配上才能生效 location ^~ /uri ^~ ...

  7. nginx location匹配规则

    谢谢作者的分享精神,原文地址:http://www.nginx.cn/115.html location匹配命令 ~      #波浪线表示执行一个正则匹配,区分大小写~*    #表示执行一个正则匹 ...

  8. 转:nginx location匹配规则

    location匹配命令 ~      #波浪线表示执行一个正则匹配,区分大小写~*    #表示执行一个正则匹配,不区分大小写^~    #^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配 ...

  9. Nginx Location 匹配

    location匹配命令 ~      #波浪线表示执行一个正则匹配,区分大小写~*    #表示执行一个正则匹配,不区分大小写^~    #^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配 ...

随机推荐

  1. hdu1242 Rescue bfs+优先队列

    直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的.遇到Angle的朋友就退出.只需15ms A ...

  2. mysql无法启动的结果问题解决

    mac 上homebrew 安装的mysql,已经用了很长时间都没什么问题,今天 ERROR! The server quit without updating PID file (/usr/loca ...

  3. layui的几个简单使用(简单弹窗,加载效果,移除加载效果)

    1.加载效果和移除加载效果 function layuiLoading(msg){ layui.use(['layer', 'form'], function(){ index = layer.loa ...

  4. Hbase配置java客户端

    1.修改windows配置文件 C:\WINDOWS\system32\drivers\etc\hosts 将远程hbase和zookeeper主机的IP地址加进去 54.0.88.53      H ...

  5. Django里使用open函数

    Django里使用open函数 前言 在Django里使用open函数打开一个文件的时候,常常会遇到路径错误的问题.我在Django APP里写了一个爬虫用于为网站提供数据,但是需要打开文件,也就是在 ...

  6. javascript对象(简略)

    javascript对象有着自有的属性,对象可以从一个称为原型的对象继承属性,对象的方法通常是继承的属性,原型式继承是javascript的核心特征.

  7. 浏览器通过Scheme协议启动APP中的页面

    在APP开发过程中,通过外部浏览器调起APP页面的场景也很普遍使用.下面就介绍一下通过外部H5页面唤起APP中页面的通用方法. 1.首先需要在AndroidMainifest.xml中对你要启动的那个 ...

  8. APACHE服务器出现No input file specified.的完美解决方案

    启用REWRITE的伪静态功能的时候,首页可以访问,而访问内页的时候,就提示:“No input file specified.” 原因在于使用的PHP是fast_cgi模式,而在某些情况下,不能正确 ...

  9. Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'L

    1.错误描述 [ERROR:]2015-06-08 09:49:42,523 [异常拦截] org.hibernate.exception.DataException: error executing ...

  10. poi 导入导出的api说明(大全)

    原文链接:http://www.cnblogs.com/qingruihappy/p/8443101.html poi 导入导出的api说明(大全) 一. POI简介 ApachePOI是Apache ...