在 nginx 中处理 OPTIONS 请求 [英] Handling OPTIONS request in nginx

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

问题描述

我们目前使用 HAProxy 作为负载均衡器,它会定期向下游框发出请求,以确保它们使用 OPTIONS 请求处于活动状态:

We're using HAProxy as a load balancer at the moment, and it regularly makes requests to the downstream boxes to make sure they're alive using an OPTIONS request:

选项/index.html HTTP/1.0

OPTIONS /index.html HTTP/1.0

我正在将 nginx 设置为具有缓存的反向代理(使用 ncache).出于某种原因,当 OPTIONS 请求进来时,nginx 会返回 405:

I'm working with getting nginx set up as a reverse proxy with caching (using ncache). For some reason, nginx is returning a 405 when an OPTIONS request comes in:

192.168.1.10 - - [22/Oct/2008:16:36:21 -0700] "OPTIONS/index.html HTTP/1.0" 405 325 "-" "-" 192.168.1.10

192.168.1.10 - - [22/Oct/2008:16:36:21 -0700] "OPTIONS /index.html HTTP/1.0" 405 325 "-" "-" 192.168.1.10

直接访问下游网络服务器时,我得到了正确的 200 响应.我的问题是:如何让 nginx 将该响应传递给 HAProxy,或者如何在 nginx.conf 中设置响应?

When hitting the downstream webserver directly, I get a proper 200 response. My question is: how to you make nginx pass that response along to HAProxy, or, how can I set the response in the nginx.conf?

推荐答案

我可能迟到了,但我遇到了同样的问题,并找到了两个解决方案.

I'm probably late, but I had the same problem, and found two solutions to it.

首先欺骗 Nginx 405 状态实际上是 200 OK,然后 proxy_pass 到你的 HAProxy 像这样:

First is tricking Nginx that a 405 status is actually a 200 OK and then proxy_pass it to your HAProxy like this:

error_page 405 =200 @405;
location @405 {
    root /;
    proxy_pass http://yourproxy:8080;
}

第二种解决方案只是捕获 OPTIONS 请求并为这些请求构建响应:

The second solution is just to catch the OPTIONS request and build a response for those requests:

location / {
    if ($request_method = OPTIONS ) {
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        return 200;
    }
}

只需选择更适合您的那一款即可.

Just choose which one suits you better.

我在博客文章中写了这个,您可以在其中找到更多详细信息.

I wrote this in a blog post where you can find more details.

这篇关于在 nginx 中处理 OPTIONS 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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