Any modules to handle HTTP OPTIONS requests in nginx?

2

1

I'm using nginx on Ubuntu 12.04, is there any module I can install via the Package Manager to make nginx magically support HTTP OPTIONS header requests instead of returning HTTP 405? I'd like to avoid additional compilation steps if that can be helped.

Aditya M P

Posted 2013-08-25T15:08:36.030

Reputation: 303

Answers

1

I'd still like a real solution to this, but for now, I have found this and I'm using the following solution:

location / {
    if ($request_method = OPTIONS ) {
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers 'origin, x-requested-with, content-type, accept';
        add_header Access-Control-Allow-Methods 'GET, POST';
        return 200;
    }
}

I'm allowing all CORS requests this being my localhost, but beware that this exists for your own security and hence don't be so liberal with Access-Control-Allow-Origin.

Additionally, I'm using angularjs, and the POST requests were getting cancelled even after the OPTIONS request got cleared with the proper permissions. Don't know what's the point of the whole preflight thing. Anyway, additng the Access-Control lines outside the location block fixed that. Like this:

location / {
                try_files $uri $uri/ /index.php?$query_string;
                if ($request_method = OPTIONS ) {
                        add_header Content-Length 0;
                        add_header Content-Type text/plain;
                        add_header Access-Control-Allow-Origin *;
                        add_header Access-Control-Allow-Headers 'origin, x-requested-with, content-type, accept';
                        add_header Access-Control-Allow-Methods 'GET, POST';
                        return 200;
                }
        }

        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers 'origin, x-requested-with, content-type, accept';
        add_header Access-Control-Allow-Methods 'GET, POST';

Aditya M P

Posted 2013-08-25T15:08:36.030

Reputation: 303