基于http方法的Nginx代理传递 [英] Nginx proxy pass based on http method

查看:159
本文介绍了基于http方法的Nginx代理传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以过滤来自locationOPTIONS呼叫并将其重定向到其他地方?尝试了此配置,但没有用:

Is it possible to filter OPTIONS calls from a location and redirect them somewhere else? Tried this configuration but didn't work:

  location /example/ {
    proxy_pass http://example.com;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass_request_headers  on;

    limit_except OPTIONS {
      proxy_pass http://anotherurl.com;
    }
  }

推荐答案

有几种方法可以实现.我认为最简单的方法是使用 map 指令:

There are several ways this can be achieved. The easiest one, in my opinion, is with the map directive:

upstream some_backend {
    server example.com;
}

upstream another_backend {
    server anotherurl.com;
}

map $request_method $upstream {
    default some_backend;
    OPTIONS another_backend;
}

server {
    ...
    location /example/ {
        ...
        proxy_pass http://$upstream;
        ...
    }
    ...
}

使用上游不是强制性的,但建议使用.在大多数情况下,它将使您的配置更易于阅读,维护和扩展.但是,如果您希望省略upstream块并将主机直接写在map块中,则该配置仍将起作用:

Using upstreams is not mandatory but recommended. In most cases it will make your configuration easier to read, maintain and extend. Nevertheless, if you prefer to omit the upstream blocks and write your hosts directly in the map block, the configuration will still work:

map $request_method $upstream {
    default example.com;
    OPTIONS anotherurl.com;
}

这篇关于基于http方法的Nginx代理传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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