[原]Nginx重定向Rewrite分析
来源:互联网 发布:电子琴节奏软件 编辑:程序博客网 时间:2024/04/28 01:14
http://homeway.me
0x01.About
之前写过Nginx重定向了,http://homeway.me/2014/10/28/nginx-reverse-proxy-conf/,但当时比较模糊。
这里主要说两种常用的重定向,都是php中的重定向。
一种是typecho的带 index.php
例如 http://homeway.me/index.php/arg1/arg2
,另一种是隐藏 index.php
的 http://homeway.me/arg1/arg2
。
以下配置代码均亲测可用。
0x02.ngx_http_rewrite_module
首先还是按常理,先脑补下nginx地rewrite规则,http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
关于nginx重写的指令主要由这么一些:
1. break指令 2. if指令 3. return指令 4. rewrite指令 5. rewrite_log指令 6. set指令
break指令
停止执行当前虚拟主机的后续rewrite指令集if指令
对给定的条件condition进行判断。如果为真,大括号内的rewrite指令将被执行。
有几个要记住的操作符:
使用=,!= 比较的一个变量和字符串
是用~,~*与正则表达式匹配的变量,如果这个正则表达式中包含},;则整个表达式需要用” 或’ 包围
使用-f,!-f 检查一个文件是否存在
使用-d,!-d 检查一个目录是否存在
使用-e,!-e 检查一个文件、目录、符号链接是否存在
使用-x,!-x 检查一个文件是否可执行
详细中文看这里好了: http://www.nginx.cn/216.html
0x03.nginx.conf
首先要明白我们现在要做的事情是两类, /index.php/arg1/arg2
和 /arg1/arg2
1.类typecho的 /index.php/arg1/arg2
/index.php/arg1/arg2
跑的location是index.php文件,也就是说,我们要做一个location匹配.php
的正则,并且要让它分辨出uri中的 /arg1/arg2
这个正则有很多种写法,我用的是lnmp传统的写法~ [^/]\.php(/|$)
。
完整匹配如下:
location ~ [^/]\.php(/|$){ #try_files $uri =404; 住址扫描目录用的,这里我们都是虚假目录,删除。 fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #include fastcgi_params;}
我测了下,这样其实就搞定了,typecho重定向就好了,不要向网上说的3个if语句。
只要访问/index.php/arg1/arg2
就能访问到内容了。
这里有几个问题:
- fastcgi_split_path_info干嘛用的?
去查找官网文档吧, http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_split_path_info
Defines a regular expression that captures a value for the
也就是说,fastcgi_split_path_info
的作用就是把参数分割成 $fastcgi_script_name
和 $fastcgi_path_info
,分割方式是后面的正则表达式。
我用echo模块输出了这里的参数,得到下面的结果,上面的是不加fastcgi_split_path_info
,下面是加了fastcgi_split_path_info
的结果:
- 问题二,fastcgi.conf 和 fastcgi_params 是什么?
这里用到的这两个配置文件是fastcgi的配置文件,我查看了下,发现fastcgi.conf
和fastcgi_params
的差别就在fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
这句话,也就是说,随意选一个。
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;fastcgi_param DOCUMENT_URI $document_uri;fastcgi_param DOCUMENT_ROOT $document_root;fastcgi_param SERVER_PROTOCOL $server_protocol;fastcgi_param HTTPS $https if_not_empty;fastcgi_param GATEWAY_INTERFACE CGI/1.1;fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;fastcgi_param REMOTE_ADDR $remote_addr;fastcgi_param REMOTE_PORT $remote_port;fastcgi_param SERVER_ADDR $server_addr;fastcgi_param SERVER_PORT $server_port;fastcgi_param SERVER_NAME $server_name;fastcgi_param REDIRECT_STATUS 200;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
也就用到了之前的那个函数fastcgi_split_path_info
,如果不做分割,就会回调: No input file specified,没有指定文件,就是因为$fastcgi_script_name
的值找不到对应的文件。
2.类CodeIgniter的 /arg1/arg2
其实,我在conf里面添加了echo输出,发现/index.php/arg1/arg2
就只会访问.php那个location。
然后,/arg1/arg2
就用到了网上广为流传的那个typecho配置了:
location / { index index.html index.php; if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; }}
看看就明白,目录/arg1/arg2
,不会有有index.php可以匹配.php后缀,所以只能匹配到 /
里面。
同样地,这里我用echo模块把参数输出了下:
测试配置文件如下:
location / { index index.html index.php; if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ echo "request_filename -f index.php = $request_filename"; echo "request_filename -f index.php= $request_filename"; echo "fastcgi_path_info -f index.php = $fastcgi_path_info"; #rewrite (.*) $1/index.php; } if (!-f $request_filename){ echo "request_filename !-f index.php = $request_filename"; echo "fastcgi_script_name !-f index.php = $fastcgi_script_name"; echo "fastcgi_path_info !-f index.php = $fastcgi_path_info"; #rewrite (.*) /index.php; } }
测试的结果如下:
也就是访问了第3个if语句,发现没有/ajax这个文件,就重定向到/index.php
去了。
这里重定向到/index.php
后,解析.php的fastcgi的规则用的又是上面用到的,原理也一样,把uri分割,然后匹配。
配置代码如下:
location ~ [^/]\.php(/|$){ #try_files $uri =404; 住址扫描目录用的,这里我们都是虚假目录,删除。 fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #include fastcgi_params;}
0x04.About nginx
嗯…今天就算是,重新证明了下,网上流传的那种,3个if语句的typecho语句,其实并没有什么卵用。
最后进去的/index.php/2015/05/22/
并不会去查询 /
的location匹配,而是.php的正则匹配。
nginx的模块测试都比较麻烦,都要重新编译安装,关于echo模块调试起来比较方便。
http://wiki.nginx.org/HttpEchoModule
下面是一些if判断用得到的nginx参数,可以通过echo输出测试查看:
$uri
代码我保存了一份,可以点击链接下载:http://homeway.me/code/nginx-rewrite-conf.zip
本文出自 夏日小草,转载请注明出处:http://homeway.me/2015/05/22/nginx-rewrite-conf/
-by小草
2015-05-22 18:27:10
- [原]Nginx重定向Rewrite分析
- Nginx重定向[Rewrite]配置
- nginx rewrite 相关、重定向
- Nginx重定向[Rewrite]配置及示例
- Nginx rewrite 301 永久重定向跳转
- nginx服务器 301重定向主页 rewrite配置
- Rewrite 重定向详解
- apache域名重定向rewrite
- nignx URL 重定向-rewrite
- nginx rewrite arg 带问号的地址转发参数处理?Nginx重定向的参数问题
- [nginx] 怎么配置nginx rewrite,才不会引起浏览器url地址重定向?
- Nginx泛域名解析及Rewrite重定向普通页面及带参数的页面
- Nginx泛域名解析及Rewrite重定向普通页面及带参数的页面
- Laravel Nginx下配置路由 404 重定向 去index.php的rewrite的规则
- nginx 域名301重定向 rewrite跳转 无www跳转到有www
- 带问号的url重定向和参数保留--nginx rewrite
- nginx rewrite(301)重定向之后无法正常请求的解决方案
- nginx重定向规则
- 自定义控件 防腾讯视频分类视图(1)
- RS232 RS422 RS485的本质区别
- spoj 839 OPTM - Optimal Marks(最小割应用)
- createFCK函数,FCKEditor编辑器在轻开平台中的使用例子二
- hdu 1588 线段相交+并查集
- [原]Nginx重定向Rewrite分析
- Oracle 添加用户并赋权,修改密码,解锁,删除用户的方法
- python - scapy - arpscanner
- 简单的四则运算计算器
- 《JVM故障诊断指南》之5 —— HPROF :使用Eclipse Memory Analyzer 工具 (MAT)进行内存泄露分析
- 对C++派生类的一个简单介绍
- AJAX传参,JS获取当前URL参数(超级实用)
- 黑马程序员_java基础之反射及高新技术
- cleditorList c\