在nginx中处理OPTIONS请求 [英] Handling OPTIONS request in nginx
问题描述
我们目前正在使用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:
OPTIONS /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]选项/index.html HTTP / 1.0405 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;
}
}
选择哪一种更适合你。
I wrote this in a blog post where you can find more details.
这篇关于在nginx中处理OPTIONS请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!