Handling OPTIONS request in nginx

前端 未结 2 1196
走了就别回头了
走了就别回头了 2020-12-08 13:58

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:

相关标签:
2条回答
  • 2020-12-08 14:59

    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.

    0 讨论(0)
  • 2020-12-08 15:00

    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.

    0 讨论(0)
提交回复
热议问题