在 nginx 中处理 OPTIONS 请求

发布于 2024-07-07 13:36:23 字数 445 浏览 7 评论 0原文

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

选项 /index.html HTTP/1.0

我正在将 nginx 设置为带有缓存的反向代理(使用 ncache)。 由于某种原因,当 OPTIONS 请求传入时,nginx 返回 405:

192.168.1.10 - - [2008年10月22日:16:36:21 -0700]“选项/index.html HTTP/1.0”405 325“-”“-”192.168.1.10

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

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:

OPTIONS /index.html HTTP/1.0

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尾戒 2024-07-14 13:36:23

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

第一种是欺骗 Nginx,让其知道 405 状态实际上是 200 OK,然后将其 proxy_pass 到您的 HAProxy,如下所示:

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

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

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

只需选择哪个更适合您。

我在 博客文章,您可以在其中找到更多详细信息。

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

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;
}

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.

初见终念 2024-07-14 13:36:23

在 httpchk 选项中,您可以像这样指定 HTTP 方法:

httpchk GET http://example.com/check.php

您还可以使用 POST 或像 / 这样的纯 URI。 我让它检查 PHP,因为 PHP 在 Nginx 外部运行。

In the httpchk option, you can specify the HTTP method like this:

httpchk GET http://example.com/check.php

You can also use POST, or a plain URI like /. I have it check PHP, since PHP runs external to Nginx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文