URL 重写 QUERY_STRING NGINX?
URL rewriting QUERY_STRING NGINX?
我想从最后一个地址有 x-oss-process 参数的主机下载任何文件。
i.php?path=$1&x-oss-process=%1
refer$1是被引用文件的地址,%1是x-oss-process参数的值
喜欢下面的link
https://dl.example.com/img/all/235868.jpg?x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
将此地址引用到以下地址后
i.php?path=img/all/235868.jpg&x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
现在我该如何编写 nginx 代码来执行此操作?谢谢你的帮助。
如果查询参数名称不包含任何短划线字符,例如 x_oss_process
而不是 x-oss-process
,解决方案会简单得多。在那种情况下,我们可以检查 $arg_x_oss_process
变量(参见 $arg_name
变量描述):
if ($arg_x_oss_process) {
rewrite ^ /i.php?path=$uri;
}
第一个参数^
是匹配任意字符串的正则表达式,$uri
is a normalized request URI (without query arguments). All the query arguments would be added to the rewrited URI (see the rewrite
指令说明):
If a replacement string includes the new request arguments, the previous request arguments are appended after them. If this is undesired, putting a question mark at the end of a replacement string avoids having them appended, for example:
rewrite ^/users/(.*)$ /show?user=? last;
然而,由于您的查询参数名称包含破折号,解决方案将更加复杂(请参阅 SO question for additional details). We would need to get the x-oss-process
query argument value from $args
variable using the map
指令,该指令应放置在 外部 server
块:
map $args $x_oss_process {
~(?:^|&)x-oss-process=([^&]*) ;
}
server {
...
if ($x_oss_process) {
rewrite ^ /i.php?path=$uri;
}
...
}
此重写规则将重写
/img/all/235868.jpg?x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
请求
/i.php?path=/img/all/235868.jpg&x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
这与您要求的略有不同(请注意 img/all/235868.jpg
URI 部分之前的斜杠)。我宁愿更改 i.php
脚本以正确处理它,但是如果你想用 nginx 本身去除这个斜线,你可以使用第二个 map
块:
map $args $x_oss_process {
~(?:^|&)x-oss-process=([^&]*) ;
}
map $uri $truncated_uri {
~/(.*) ;
}
server {
...
if ($x_oss_process) {
rewrite ^ /i.php?path=$truncated_uri;
}
...
}
我想从最后一个地址有 x-oss-process 参数的主机下载任何文件。 i.php?path=$1&x-oss-process=%1 refer$1是被引用文件的地址,%1是x-oss-process参数的值 喜欢下面的link
https://dl.example.com/img/all/235868.jpg?x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
将此地址引用到以下地址后
i.php?path=img/all/235868.jpg&x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
现在我该如何编写 nginx 代码来执行此操作?谢谢你的帮助。
如果查询参数名称不包含任何短划线字符,例如 x_oss_process
而不是 x-oss-process
,解决方案会简单得多。在那种情况下,我们可以检查 $arg_x_oss_process
变量(参见 $arg_name
变量描述):
if ($arg_x_oss_process) {
rewrite ^ /i.php?path=$uri;
}
第一个参数^
是匹配任意字符串的正则表达式,$uri
is a normalized request URI (without query arguments). All the query arguments would be added to the rewrited URI (see the rewrite
指令说明):
If a replacement string includes the new request arguments, the previous request arguments are appended after them. If this is undesired, putting a question mark at the end of a replacement string avoids having them appended, for example:
rewrite ^/users/(.*)$ /show?user=? last;
然而,由于您的查询参数名称包含破折号,解决方案将更加复杂(请参阅 x-oss-process
query argument value from $args
variable using the map
指令,该指令应放置在 外部 server
块:
map $args $x_oss_process {
~(?:^|&)x-oss-process=([^&]*) ;
}
server {
...
if ($x_oss_process) {
rewrite ^ /i.php?path=$uri;
}
...
}
此重写规则将重写
/img/all/235868.jpg?x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
请求
/i.php?path=/img/all/235868.jpg&x-oss-process=image/resize,m_lfit,h_200,w_200/quality,q_60
这与您要求的略有不同(请注意 img/all/235868.jpg
URI 部分之前的斜杠)。我宁愿更改 i.php
脚本以正确处理它,但是如果你想用 nginx 本身去除这个斜线,你可以使用第二个 map
块:
map $args $x_oss_process {
~(?:^|&)x-oss-process=([^&]*) ;
}
map $uri $truncated_uri {
~/(.*) ;
}
server {
...
if ($x_oss_process) {
rewrite ^ /i.php?path=$truncated_uri;
}
...
}