Handling OPTIONS request in nginx
Asked Answered
C

2

38

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?

Chromo answered 22/10, 2008 at 23:42 Comment(1)
Is this module available yet by any chance?Poppas
H
55

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.

Hanky answered 8/2, 2012 at 16:14 Comment(2)
suppose you have proxy_pass already set up - is there a reason why nginx doesn't just forward the OPTIONS request without having to do the first trick? basically a way of saying 'don't you go interpreting the HTTP verb just forward it like i already said'Teferi
I'm trying this but if ($request_method = OPTIONS) does not seem to be working, the request is sent to my backend. Is there a new syntax?Waken
R
2

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.

Reiner answered 11/1, 2011 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.