nginx重定向POST请求 [英] nginx redirect POST requests

查看:1771
本文介绍了nginx重定向POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受https://www.example.com/API/ooo/xxxhttps://www.example.com/API/aaa/bbbhttps://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/xxxhttps://www.example.com/aaa/bbbhttps://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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆