暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Nginx rewrite 获取问好“?”后面的参数

netkiller 2020-03-24
747

节选自 《Netkiller Web 手札》


3.2.4.1. http get 参数处理

需求如下

原理地址:
http://www.netkiller.cn/redirect/index.html?skuid=133

目的地址:
http://www.netkiller.cn/to/133.html

注意:nginx rewrite 并不支持http get 参数处理,也就是说“?”之后的内容rewrite根部获取不到。

下面的例子是行不通的

			rewrite ^/redirect/index\.html\?skuid=(\d+)$ /to/$1.html permanent ;			

我们需要通过正在查出参数,然后赋值一个变量,再将变量传递给rewrite。具体做法是:

			server {
listen 80;
server_name www.netkiller.cn;

#charset koi8-r;
access_log /var/log/nginx/test.access.log main;

location / {
root /www/test;
index index.html;

if ($request_uri ~* "^/redirect/index\.html\?skuid=([0-9]+)$") {
set $argv1 $1;
rewrite .* /to/$argv1.html? permanent;
}
}
}

测试结果

			[neo@netkiller conf.d]$ curl -I http://www.netkiller.cn/redirect/index.html?skuid=133
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 12 Apr 2016 06:59:33 GMT
Content-Type: text/html
Content-Length: 178
Location: http://www.netkiller.cn/to/133.html
Connection: keep-alive



1.3.6. rewrite

		
Rewrite Flags
last - 基本上都用这个Flag。
break - 中止Rewirte,不在继续匹配
redirect - 返回临时重定向的HTTP状态302
permanent - 返回永久重定向的HTTP状态301

文件及目录匹配,其中:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行

正则表达式全部符号解释
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~和!~* 分别为区分大小写不匹配及不区分大小写不匹配
(pattern) 匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 ‘\(’ 或 ‘\)’。
^ 匹配输入字符串的开始位置。
$ 匹配输入字符串的结束位置。

		
server {
listen 80;
server_name www.example.com example.com ;
if ($host = "example.com" )
{
rewrite ^/(.*)$ http://www.example.com/$1 permanent;
}
if ($host != "www.example.com" )
{
rewrite ^/(.*)$ http://www.example.com/$1 permanent;
}
}


1.3.6.1. 处理泛解析

			
if ($host ~ '(.*)\.example\.com' ) {
set $subdomain $1;
rewrite "^/(.*)$" /$subdomain/$1;
}

1.3.6.2. 处理扩展名

			
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (!-f $request_filename){
rewrite /(.*) http://images.example.com/$1;
}
}


1.3.6.3. http get 参数处理

需求如下

			
原理地址:
http://www.netkiller.cn/redirect/index.html?skuid=133

目的地址:
http://www.netkiller.cn/to/133.html

注意:nginx rewrite 并不支持http get 参数处理,也就是说“?”之后的内容rewrite根部获取不到。

下面的例子是行不通的

			
rewrite ^/redirect/index\.html\?skuid=(\d+)$ /to/$1.html permanent ;

我们需要通过正在查出参数,然后赋值一个变量,再将变量传递给rewrite。具体做法是:

			
server {
listen 80;
server_name www.netkiller.cn;

#charset koi8-r;
access_log /var/log/nginx/test.access.log main;

location / {
root /www/test;
index index.html;

if ($request_uri ~* "^/redirect/index\.html\?skuid=([0-9]+)$") {
set $argv1 $1;
rewrite .* /to/$argv1.html? permanent;
}
}
}

测试结果

			
[neo@netkiller conf.d]$ curl -I http://www.netkiller.cn/redirect/index.html?skuid=133
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 12 Apr 2016 06:59:33 GMT
Content-Type: text/html
Content-Length: 178
Location: http://www.netkiller.cn/to/133.html
Connection: keep-alive

1.3.6.4. 正则取非

需求如下,除了2015年保留,其他所有页面重定向到新页面

				rewrite ^/promotion/(?!2015\/)(.*) https://www.netkiller.cn/promotion.html permanent;

1.3.6.5. 去掉扩展名

需求

			
http://www.example.com/article/10 => http://www.example.com/article/10.html

			
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /$1.html last;
break;
}
}

1.3.6.6. 添加扩展名


			
原地址 http://ipfs.netkiller.cn/ipfs/QmcA1Fsrt6jGTVqAUNZBqaprMEdFaFkmkzA5s2M6mF85UC
目标地址:http://ipfs.netkiller.cn/ipfs/QmcA1Fsrt6jGTVqAUNZBqaprMEdFaFkmkzA5s2M6mF85UC.mp4

			
location / {
rewrite ^/(.*)\.mp4$ /$1 last;
proxy_pass http://127.0.0.1:8080;
}



文章转载自netkiller,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论