nginx重定向POST请求 [英] nginx redirect POST requests
问题描述
我有一个接受https://www.example.com/API/ooo/xxx
,https://www.example.com/API/aaa/bbb
和https://www.example.com/API/xxx/yyy
...等的Web服务器.
I have a web server which accepts https://www.example.com/API/ooo/xxx
, https://www.example.com/API/aaa/bbb
, and https://www.example.com/API/xxx/yyy
... etc.
现在,我们要将上述任务重定向到https://www.example.com/ooo/xxx
,https://www.example.com/aaa/bbb
和https://www.example.com/xxx/yyy
...等.
Now we want to redirect the above quests to https://www.example.com/ooo/xxx
, https://www.example.com/aaa/bbb
, and https://www.example.com/xxx/yyy
... etc.
我尝试在nginx中使用rewrite
关键字:
I've tried using rewrite
key word in nginx:
location /API/ {
rewrite ^/API(.*)$ https://$host$1 redirect;
}
这适用于GET请求.但是它将POST请求转换为GET请求.这是我不想要的东西.
This works for GET requests. But it turns POST requests to GET requests. This is something I don't want.
在将/API/*
重定向到/*
时,如何保留http方法?
How can I preserve the http method while redirecting /API/*
to /*
?
此帖子说,我可以使用307
重定向.但是rewrite
似乎不支持307
重定向.而且我不知道如何在return
中使用$1
正则表达式属性.
This post says that I can use 307
redirect. But rewrite
doesn't seem to support 307
redirect. And I can't figure out how to use $1
regular expression property in return
.
推荐答案
使用return
语句.您可以使用正则表达式location
块来捕获要返回的URI部分.
Use a return
statement. You can use a regular expression location
block to capture the part of the URI to return.
例如:
location ~ ^/API(/.*)$ {
return 307 $1$is_args$args;
}
请注意,使用正则表达式location
指令,其在配置中的顺序很重要,因此您可能需要移动它.有关详细信息,请参见本文档.
Note that with the regular expression location
directive, its order within the configuration is significant, so you may need to move it. See this document for details.
这篇关于nginx重定向POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!