Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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';
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.