前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx rewrite 获取问好“?”后面的参数

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

作者头像
netkiller old
发布2018-03-05 16:20:54
2.1K0
发布2018-03-05 16:20:54
举报
文章被收录于专栏:NetkillerNetkiller
节选自 《Netkiller Web 手札》
3.2.4.1. http get 参数处理

需求如下

代码语言:javascript
复制
原理地址:
http://www.netkiller.cn/redirect/index.html?skuid=133

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

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

下面的例子是行不通的

代码语言:javascript
复制
			rewrite ^/redirect/index\.html\?skuid=(\d+)$ /to/$1.html permanent ;			

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

代码语言:javascript
复制
			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;
        }
    }
}			

测试结果

代码语言:javascript
复制
			[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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-04-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Netkiller 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 节选自 《Netkiller Web 手札》
  • 3.2.4.1. http get 参数处理
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档